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: #6D28D9once, 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.
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.
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.
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)).
: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.
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.
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.
Why CSS variables beat preprocessor variables
You might be familiar with Sass or Less variables. CSS custom properties are better because:
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.
getComputedStyle(element).getPropertyValue('--name') to read a variable. Note the trim() method may be needed to remove whitespace: .getPropertyValue('--name').trim()
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
.darkclass 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 withvar(--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
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 โ