close
The Wayback Machine - https://web.archive.org/web/20250130113342/https://www.geeksforgeeks.org/css-variables/
Open In App

CSS Variables

Last Updated : 04 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CSS variables (custom properties) are reusable values defined with two dashes (–) that make your CSS code efficient and easier to maintain.

  • Store values like colors, sizes, or fonts in one place for centralized updates.
  • Use var() to apply these variables anywhere in your CSS.
  • Improve code readability and consistency across your styles.

Syntax

var( --custom-name, value );

Parameters

  • –custom-name: (Required) The name of the custom property, starting with two dashes (–).
  • value: (Optional) A value used if the custom property is not defined or is invalid.
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        :root {
            --main-bg-color: lightblue;
            --main-text-color: darkblue;
        }
        body {
            background-color: var(--main-bg-color);
            color: var(--main-text-color);
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <h1>Welcome to My Website</h1>
    <p>This is a sample paragraph demonstrating CSS variables.</p>
</body>
</html>
<!--Driver Code Ends }-->

More Example of CSS Variable

1. Themed Button Using CSS Variables

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        :root {
            --button-bg: #4CAF50;
            --button-text: white;
            --button-padding: 10px 20px;
        }
        .btn {
            background-color: var(--button-bg);
            color: var(--button-text);
            padding: var(--button-padding);
            border: none;
            cursor: pointer;
        }
        .btn:hover {
            background-color: darkgreen;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <button class="btn">Click Me</button>
</body>
</html>

<!--Driver Code Ends }-->

In this example

  • :root defines –btn-bg for button background and –btn-text for button text color.
  • These variables are applied to style a button, making theme updates easy by changing values in :root.

2. Dynamic Spacing Using CSS Variables

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        :root {
            --spacing: 20px;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: lightcoral;
            margin: var(--spacing);
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <div class="box"></div>
    <div class="box"></div>
</body>
</html>

<!--Driver Code Ends }-->


In this example:

  • :root defines –spacing to set uniform spacing between boxes.
  • Changing the –spacing value in :root adjusts the margin for all .box elements.

Best Practices for CSS Variables

  • Define Variables in :root: Declare global variables within the :root selector to ensure they are accessible throughout your stylesheet.
  • Use Descriptive Naming: Choose clear and descriptive names for variables to enhance readability and maintainability.
  • Leverage the Cascade: Take advantage of CSS variables’ ability to inherit and be overridden within specific scopes, allowing for flexible theming and component styling.

CSS Variables – FAQs

Can CSS variables be used in media queries?

Yes, CSS variables can be utilized within media queries to create responsive designs that adapt to different screen sizes.

How do CSS variables differ from preprocessor variables like those in Sass or Less?

Unlike preprocessor variables, CSS variables are dynamic and can be manipulated in real-time using JavaScript, offering greater flexibility.

Are CSS variables supported in all browsers?

CSS variables have broad support in modern browsers, but it’s advisable to check compatibility for older versions to ensure consistent behavior.

Can I use CSS variables for animations and transitions?

Yes, CSS variables can be used in animations and transitions, allowing for dynamic and reusable animation values.

How do I provide fallback values for CSS variables?

Use the var() function’s second parameter to specify a fallback value in case the variable is not defined or is invalid.



Next Article

Similar Reads

three90RightbarBannerImg