Skip to main content

Introduction to CSS Variables

CSS variables, officially known as CSS custom properties, allow you to store specific values to be reused throughout your stylesheet. They help create more maintainable, flexible, and dynamic CSS code. Introduced as part of CSS3, variables are now supported in all modern browsers, making them a powerful tool for modern web development.

Basic Syntax

CSS variables are defined using a double hyphen (--) prefix, and are accessed using the var() function.

Defining Variables

Variables are typically defined in the :root selector to make them globally available, but they can be defined in any selector:

Using Variables

To use a variable, you use the var() function:

Variable Scope

CSS variables follow the standard CSS cascading rules and inherit their values from their parent elements.

Global Variables

Variables defined in the :root selector are globally available throughout the document:

Local Variables

Variables can also be defined within specific selectors, limiting their scope:

Variable Inheritance

Variables are inherited by child elements:

Fallback Values

The var() function accepts a second parameter that serves as a fallback value if the variable is not defined:
You can also chain fallbacks:

Updating Variables with JavaScript

One of the most powerful features of CSS variables is the ability to update them using JavaScript, enabling dynamic styling without inline styles.
This capability makes CSS variables perfect for themes, dark mode toggles, and other dynamic styling needs.

Example: Theme Switcher

Responsive Design with CSS Variables

CSS variables can be redefined within media queries, making responsive design more maintainable:
This approach centralizes your responsive adjustments, making them easier to maintain.

Calculating with CSS Variables

CSS variables can be used with the calc() function for dynamic calculations:
This makes your spacing system more consistent and easier to adjust globally.

Color Manipulation with CSS Variables

CSS variables are particularly useful for creating color systems:
By manipulating the HSL values, you can create an entire color system from a few base variables.

Organizing CSS Variables

As your project grows, organizing your variables becomes important. Here’s a recommended approach:
This organization makes it easy to find and update variables as needed.

Component-Specific Variables

For larger applications, you might want to create component-specific variables that reference global variables:
This approach creates a clear separation between global design tokens and component-specific variables.

CSS Variables vs. Preprocessor Variables

CSS variables offer several advantages over preprocessor variables (like those in Sass or Less): However, preprocessor variables still have some advantages:
  • Better browser compatibility (after compilation)
  • More advanced features like mixins and functions
  • Can be used in more contexts (like media query definitions)
Many developers use both: preprocessor variables for static values and build-time features, and CSS variables for values that might change at runtime.

Best Practices

  1. Use meaningful names: Choose descriptive names that indicate the purpose, not just the value.
  2. Create a system: Develop a systematic approach to naming and organizing variables.
  3. Document your variables: Add comments to explain the purpose and usage of variables.
  4. Use variables for repeated values: Any value used more than once is a good candidate for a variable.
  5. Create relationships between variables: Define base variables and derive others from them.
  6. Avoid excessive nesting: While variables can be scoped to selectors, too much nesting can make it hard to track variable values.
  7. Provide fallbacks: Always include fallbacks for older browsers.

Browser Support and Fallbacks

CSS variables are supported in all modern browsers, but if you need to support older browsers (particularly IE11), you’ll need fallbacks:
For more complex scenarios, you can use feature detection:
Or use a JavaScript-based solution like a polyfill for older browsers.

Practical Examples

Theme Switching

Dynamic Form Validation

User Preference-Based Styling

Conclusion

CSS variables (custom properties) provide a powerful way to create more maintainable, flexible, and dynamic stylesheets. They enable you to:
  1. Define values once and reuse them throughout your CSS
  2. Create relationships between different values
  3. Update styles dynamically with JavaScript
  4. Implement theming and user preferences
  5. Simplify responsive design
  6. Create more organized and systematic design systems
By leveraging CSS variables effectively, you can write more maintainable CSS code and create more dynamic user experiences without relying heavily on JavaScript for style manipulation.