CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

External Styles


External stylesheets are the most recommended way to apply CSS to your web pages. They separate your content (HTML) from your styling (CSS), making your code cleaner, more maintainable, and improving website performance. This separation is crucial for efficient web development.

Example 1: Linking a basic external stylesheet

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>External Stylesheet Example</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>Welcome to My Website</h1>
    <p>This paragraph will be styled by the external stylesheet.</p>
</body>
</html>

Explanation: This HTML code links to an external CSS file named style.css using the <link> tag within the <head> section. The rel="stylesheet" attribute specifies the relationship as a stylesheet, and href="style.css" points to the location of the CSS file.


Example 2: Specifying a media type for the stylesheet

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Print Stylesheet Example</title>
    <link rel="stylesheet" href="print.css" media="print">
    <link rel="stylesheet" href="screen.css" media="screen">
</head>
<body>
    <h1>Check out our print-friendly page!</h1>
    <p>This content has different styles for screen and print.</p>
</body>
</html>

Explanation: Here, two external stylesheets are linked. The media attribute is used to apply print.css only when the page is printed and screen.css only when viewed on a screen, optimizing user experience across devices.


Example 3: Linking a stylesheet from a different directory

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Directory Stylesheet Example</title>
    <link rel="stylesheet" href="css/main.css">
</head>
<body>
    <h2>Styling from a separate folder</h2>
    <p>Keeping CSS organized in a dedicated folder is good practice.</p>
</body>
</html>

Explanation: This example demonstrates linking a stylesheet (main.css) that resides within a css subdirectory. This practice helps in organizing project files and maintaining a clean file structure.


Example 4: Using an absolute URL for an external stylesheet

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Absolute URL Stylesheet</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
</head>
<body>
    <h3>Normalized Styles</h3>
    <p>This page uses a CSS reset from a CDN to ensure consistent rendering across browsers.</p>
</body>
</html>

Explanation: This code links to an external stylesheet using an absolute URL, typically from a Content Delivery Network (CDN). This is common for popular libraries like CSS resets or frameworks, leveraging faster loading times.


Example 5: Linking multiple external stylesheets

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multiple Stylesheets</title>
    <link rel="stylesheet" href="base.css">
    <link rel="stylesheet" href="theme.css">
</head>
<body>
    <h1>Styled with multiple CSS files</h1>
    <p>Layering stylesheets allows for modular and scalable designs.</p>
</body>
</html>

Explanation: This demonstrates linking multiple external stylesheets. The browser applies styles in the order they appear, allowing for modular design where theme.css can override or extend styles from base.css. This improves maintainability and scalability for complex projects.