CSS Variables

CSS Variables (Custom Properties): Reusable Styles

CSS variables (officially known as Custom Properties) are reusable values defined with two dashes (--) that make your CSS code incredibly efficient and much easier to maintain.


Syntax and Parameters

Syntax:

property: var(--custom-name, fallback-value);

Parameters

Parameter Description
--custom-name (Required) The name of the custom property. It must start with two dashes (--). It is case-sensitive!
fallback-value (Optional) A backup value used if the custom property is not defined or is invalid.

Basic CSS Variable Example

Variables are usually declared inside the :root pseudo-class. The :root selector matches the document's root element (the <html> tag), meaning variables defined here are "global" and can be accessed by any element on the page.

Global Variables Example:

<style>
    /* Step 1: Define the variables globally */
    :root {
        --main-bg-color: lightblue;
        --main-text-color: navy;
    }
    /* Step 2: Use the variables */
    body {
        background-color: var(--main-bg-color);
        color: var(--main-text-color);
    }
</style>

<h1>IntricateDevo Variables</h1> <p>Powered by CSS variables!</p>


More Examples of CSS Variables

1. Themed Button Using CSS Variables

Variables are perfect for styling components like buttons. By assigning variables for background, text, and padding, you can easily change the theme of your entire website simply by modifying the :root block.

Themed Button Example:

<style>
    :root {
        --button-bg: navy;
        --button-text: white;
        --button-padding: 15px 30px;
        --button-radius: 8px;
        --hover-bg: lightblue;
        --hover-text: navy;
    }
    .btn {
        background-color: var(--button-bg);
        color: var(--button-text);
        padding: var(--button-padding);
        border-radius: var(--button-radius);
        transition: 0.3s;
    }
    .btn:hover {
        background-color: var(--hover-bg);
        color: var(--hover-text);
    }
</style>

<button class="btn">Primary Button</button>

In this example:

2. Dynamic Spacing Using CSS Variables

You can also store measurements (like pixels or percentages) in variables to enforce a consistent spacing system across your design.

Dynamic Spacing Example:

<style>
    :root {
        /* Centralized spacing variable */
        --spacing: 20px;
    }
    .box {
        /* Applying the spacing variable to margin */
        margin: var(--spacing);
    }
</style>

<div class="container"> <div class="box">Box 1</div> <div class="box">Box 2</div> </div>

In this example:


Best Practices for CSS Variables

  1. Define Variables in :root: Declare your global variables within the :root selector. This ensures they are accessible to every element throughout your entire stylesheet.
  2. Use Descriptive Naming: Choose clear, semantic names for variables (like --primary-color or --base-padding) to greatly enhance readability and maintainability for other developers.
  3. Leverage the Cascade: CSS variables obey standard cascading rules. You can override a global variable locally by redefining it inside a specific class selector, allowing for incredibly flexible theming (like toggling a "Dark Mode" class on the body).

Local Override Example:

:root {
    --text-color: black;
}

/* Overrides the variable ONLY inside the .dark-mode container */ .dark-mode { --text-color: white; }


Exercise

?

Which selector is best practice for defining global CSS variables that you want to be accessible anywhere in your document?