Table of contents
1.
Introduction
2.
The Traditional Way
3.
Syntax
3.1.
Declaring a CSS Variable
3.2.
Using a CSS Variable
4.
Parameters
4.1.
Example with a Fallback:
5.
Working of CSS var() Function
5.1.
Steps
5.2.
Example
6.
CSS Variables Examples:
6.1.
Example 1: Theming with Variables
6.2.
Example 2: Responsive Design with Variables
7.
Supported Browsers
7.1.
Note
8.
Advantages of CSS Variables
9.
Disadvantages of CSS Variables
10.
Frequently Asked Questions
10.1.
What is the purpose of CSS variables?
10.2.
Can I use CSS variables with JavaScript?
10.3.
What is the difference between CSS variables and SASS variables?
11.
Conclusion
Last Updated: Jan 3, 2025
Easy

CSS Variables

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

CSS Variables, also known as custom properties, allow you to define reusable values in your CSS. CSS Variables are special properties in CSS that store reusable values, such as colors, fonts, or spacing. They make it easy to apply consistent styles across a website and update them in one place if needed. CSS variables are flexible, can be changed dynamically with JavaScript, and are supported by most modern browsers.

CSS Variables

In this article, we will discuss the syntax, parameters, how CSS variables work, practical examples, and browser compatibility.

The Traditional Way

Before CSS variables were introduced, web developers had to repeat the same values multiple times throughout a stylesheet when a repeated value was used. For example, if your website used the same color in multiple places, you had to manually write out that color value each time it was needed:

.button {
  background-color: #007bff;
}

.header {
  color: #007bff; 
}

.footer {
  border-color: #007bff;
}


As you can see, the same blue color (#007bff) is repeated several times. If you later decide to change this color, you have to manually update it in each spot, which is time-consuming & error-prone, especially in large stylesheets.

CSS preprocessors like Sass & Less addressed this by allowing variables, but those required a preprocessing step & couldn't be used in plain CSS. Native browser support for CSS variables solves this problem.

Syntax

The syntax for CSS variables is simple. They are declared within a CSS rule, prefixed with -- and accessed using the var() function.

Declaring a CSS Variable

:root {
  --primary-color: #3498db;
  --font-size: 16px;
}

Using a CSS Variable

body {
  color: var(--primary-color);
  font-size: var(--font-size);
}


In the example above:

  • --primary-color and --font-size are variables.
     
  • :root is a special pseudo-class that represents the root element, ensuring global accessibility.

Parameters

CSS variables work with two main parameters when used in the var() function:

  1. Variable Name: The name of the variable you want to use, prefixed with --.
     
  2. Fallback Value: An optional value that will be used if the variable is not defined.

Example with a Fallback:

p {
  color: var(--secondary-color, #2ecc71);
}

In this example, if --secondary-color is not defined, the paragraph text will default to the color #2ecc71.

Working of CSS var() Function

The var() function dynamically inserts the value of a variable wherever it’s used. This function makes it easier to update styles across your project by changing a single variable's value.

Steps

  1. Declare variables: Typically done in the :root pseudo-class for global access.
     
  2. Use variables: Apply them to any CSS property using var(--variable-name).
     
  3. Leverage fallback values: Provide default values for robust styling.

Example

:root {
  --main-bg-color: #f4f4f4;
  --text-color: #333;
}


body {
  background-color: var(--main-bg-color);
  color: var(--text-color);
}


button {
  background-color: var(--main-bg-color);
  color: var(--text-color);
  border: 1px solid var(--text-color);
  padding: 10px 20px;
  cursor: pointer;
}

Output

  • The body and button elements use the same background and text color.
     
  • Changing the value of --main-bg-color or --text-color in :root will update all occurrences instantly.

CSS Variables Examples:

Example 1: Theming with Variables

:root {
  --theme-light-bg: #ffffff;
  --theme-light-text: #000000;
  --theme-dark-bg: #000000;
  --theme-dark-text: #ffffff;
}


body.light {
  background-color: var(--theme-light-bg);
  color: var(--theme-light-text);
}


body.dark {
  background-color: var(--theme-dark-bg);
  color: var(--theme-dark-text);
}

Output

  • Toggle between the .light and .dark classes on the <body> to switch themes dynamically.

Example 2: Responsive Design with Variables

:root {
  --font-size-small: 12px;
  --font-size-medium: 16px;
  --font-size-large: 20px;
}


body {
  font-size: var(--font-size-medium);
}


@media (max-width: 600px) {
  body {
    font-size: var(--font-size-small);
  }
}


@media (min-width: 1200px) {
  body {
    font-size: var(--font-size-large);
  }
}

Output:

  • The font size adapts based on the screen width, showcasing the power of variables in responsive design.

Supported Browsers

CSS variables are widely supported by modern browsers. However, understanding compatibility is essential for ensuring your styles work as expected.

BrowserVersion Supported
Google Chrome49+
Mozilla Firefox31+
Microsoft Edge15+
Safari9.1+
Opera36+

Note

  • If you need to support older browsers, consider using polyfills or fallback CSS.

Advantages of CSS Variables

  1. Reusability: Define once, use anywhere.
     
  2. Maintainability: Update values in a single location to reflect changes across the project.
     
  3. Dynamic Styling: Easily implement themes or adapt styles to different conditions.
     
  4. Fallback Support: Ensures robust designs even if variables are missing.

Disadvantages of CSS Variables

  1. Browser Compatibility: Not supported in older browsers without polyfills.
     
  2. Complex Debugging: Large projects with multiple variables can become difficult to debug.

Frequently Asked Questions

What is the purpose of CSS variables?

CSS variables allow you to store and reuse values across your stylesheets, making your CSS more maintainable and dynamic.

Can I use CSS variables with JavaScript?

Yes, CSS variables can be accessed and modified dynamically using JavaScript. 

What is the difference between CSS variables and SASS variables?

CSS variables are native to CSS and can be manipulated dynamically. SASS variables are static and processed during compilation, offering less flexibility.

Conclusion

CSS variables are a powerful tool for managing styles efficiently. By using them, you can create reusable, dynamic, and maintainable CSS, making your projects easier to manage and adapt to changing requirements. This article covered the syntax, working of the var() function, practical examples, and browser support for CSS variables. 

You can also check out our other blogs on Code360.

Live masterclass