What are CSS variables?

CSS variables (also called custom properties) are named values that you define once and reuse throughout your stylesheet. Instead of hardcoding colors, sizes, or other values, you store them in variables and reference them with the var() function.

This is powerful because:

  • Less repetition: Define --primary-color: #6D28D9 once, use it 50 times.
  • Easier maintenance: Change the color in one place, updates everywhere.
  • Runtime changes: JavaScript can update variables on the fly โ€” preprocessors like Sass cannot.
  • Responsive design: Change variables at different breakpoints or states.
  • Theming: Build light and dark themes by swapping variable values.
Key point:
CSS variables are not the same as Sass variables. CSS variables are part of the CSS spec itself โ€” they're dynamic, live in the cascade, and can be accessed/changed by JavaScript.

Declaring and using CSS variables

To declare a CSS variable, use two dashes (--) followed by the name. You can declare it anywhere, but the most common place is in the :root selector for global scope.

styles.css Declaring and using CSS variables

The syntax is simple:

  • Declaration: --variable-name: value; (inside any selector)
  • Usage: var(--variable-name) (replaces the variable with its value)
  • Scope: Variables are inherited by child elements, just like normal CSS properties.

Scope and fallback values

CSS variables follow the cascade. If you declare a variable in :root, it's available everywhere. If you declare it on a specific element, only that element and its children can access it.

scope.css Variable scope and fallback values

Fallback values are defaults used if a variable isn't defined. The syntax is var(--variable, fallback). You can chain multiple fallbacks: var(--primary, var(--color, blue)).

Tip:
Always declare global variables in :root for clarity. Local variables on specific elements are useful for component-level overrides.

Try it yourself: Theme system

Edit the CSS variables and watch the entire page update. Change the primary color, background, or spacing โ€” all at once, just by editing the variables.

โœ๏ธ Try It Yourself โ€” Edit & Run
Preview

Dark mode with CSS variables

One of the most powerful uses of CSS variables is building light and dark themes. Define one set of variables, then override them when a .dark class is added to the body.

darkmode.css Dark mode with variable swapping

The trick is that all your styling uses variables, and when the class changes, all variables are re-evaluated. No need to write separate CSS rules for light and dark modes.

Try it yourself: Dark mode toggle

Click the checkbox to toggle between light and dark mode. Notice how the entire page changes instantly โ€” just by swapping CSS variable values.

โœ๏ธ Try It Yourself โ€” Dark Mode Toggle
Preview

Why CSS variables beat preprocessor variables

You might be familiar with Sass or Less variables. CSS custom properties are better because:

comparison.txt CSS variables vs preprocessor variables

CSS variables are also part of the CSS specification itself, so they're officially part of the language โ€” not a preprocessing step.

Changing variables with JavaScript

This is where CSS variables shine. You can read and write CSS variables from JavaScript, enabling dynamic theming, user preferences, and responsive behavior.

script.js Reading and setting CSS variables
Pro tip:
Use getComputedStyle(element).getPropertyValue('--name') to read a variable. Note the trim() method may be needed to remove whitespace: .getPropertyValue('--name').trim()
Build a Theme Switcher

Task: Create a complete theme system with variables and a switcher:

  • Define at least 5 color variables in :root (primary, secondary, background, text, accent)
  • Create a light theme (default values)
  • Create a dark theme by overriding variables in body.dark
  • Build a page with multiple components (cards, buttons, headings) that use the variables
  • Add a checkbox that toggles the .dark class on the body with JavaScript
  • Bonus: Add smooth transitions when switching themes

Start with one of the editors above and expand it!

Hint: Structure: 1) Declare light variables in :root, 2) Override them in body.dark, 3) Use var() in all CSS properties, 4) Add a checkbox with onchange listener that calls document.body.classList.toggle('dark').

Key takeaways

  • CSS variables are declared with --name: value; and used with var(--name).
  • Declare global variables in :root; they're inherited by all elements.
  • Fallback values provide defaults: var(--color, blue) uses blue if --color is undefined.
  • CSS variables are dynamic โ€” JavaScript can read and modify them at runtime.
  • Build themes by defining variables once and overriding them with different classes or media queries.
  • CSS variables are part of the CSS spec itself, not a preprocessing step โ€” they respect the cascade and inheritance.

Test your knowledge

Question 1 of 5
How do you declare a CSS variable?
Question 2 of 5
How do you use a CSS variable in a style?
Question 3 of 5
Where should global CSS variables be declared?
Question 4 of 5
What does var(--color, blue) do?
Question 5 of 5
Can CSS variables be changed by JavaScript at runtime?
0
/5
Excellent! You understand CSS variables.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered CSS variables.
Build themeable designs next.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’