The Power of CSS Variables for Your Website
Websites that switch to dark mode, adapt to your preferred theme or change their look with the seasons often rely on CSS variables. This guide explains what they are, how to use them well and where they fit in modern front-end development.
What are CSS variables?
CSS variables, formally called custom properties, let you store a value such as a color, font size or spacing unit under a name, then reuse it anywhere in your stylesheet. Instead of repeating the same color code in dozens of rules, you define it once and reference it.
For instance, you could create a variable for your primary color called --primary-color. Instead of typing #ff4500 in every rule, you’d write var(--primary-color). If the brand color changes, you update one line and the change cascades across the site.
Unlike variables in preprocessors such as Sass, custom properties are live in the browser. They follow the cascade and inheritance, can be scoped to any element, can be redefined inside media queries and can be read or changed with JavaScript. That is what makes theming, dark mode and component-level customization so much simpler.
Best practices for working with CSS variables
Used well, CSS variables keep your styles scalable and maintainable. Here are the rules we follow.
Define global tokens on :root
Declare site-wide values at the root so they are available everywhere and form a single source of truth for colors, type and spacing:
:root {
--primary-color: #ff4500;
--secondary-color: #ffd700;
--font-size-base: 1rem;
--spacing: 1rem;
}
.button {
background-color: var(--primary-color);
padding: var(--spacing) calc(var(--spacing) * 2);
font-size: var(--font-size-base);
}
The var() function also accepts a fallback, which is useful in components that may be used outside your main theme: color: var(--link-color, #0050b3);
Name variables by purpose, not by value
A name like --color-text or --color-surface keeps working when the value changes. A name like --dark-gray stops making sense the moment you switch themes. Purpose-based names are what make dark mode and rebrands painless.
Don’t turn everything into a variable
Not every value needs to be a variable. Focus on values you expect to change or reuse, such as colors, font sizes, spacing and layout widths. One-off values that never change are clearer as plain declarations.
Redefine variables inside media queries
Because custom properties are resolved at runtime, you can change their values at a breakpoint and every rule that uses them updates automatically:
@media (max-width: 600px) {
:root {
--font-size-base: 0.9375rem;
--spacing: 0.75rem;
}
}
Keep text sizes in rem rather than px so they still respect the visitor’s browser font-size setting.
Practical applications of CSS variables
Here’s how CSS variables make web projects easier to theme, customize and maintain.
Dark mode and theming
The cleanest way to build dark mode is to define one set of purpose-named variables, use them everywhere, and then redefine the same variables for the dark theme. Your component styles never change; only the values do.
/* Light theme (default) */
:root {
--color-bg: #ffffff;
--color-text: #1a1a1a;
--color-surface: #f4f4f5;
--color-accent: #0050b3;
}
/* Follow the operating system setting, unless the visitor chose light */
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--color-bg: #121212;
--color-text: #f5f5f5;
--color-surface: #1e1e1e;
--color-accent: #6cb4ff;
}
}
/* Explicit choice from a theme toggle */
:root[data-theme="dark"] {
--color-bg: #121212;
--color-text: #f5f5f5;
--color-surface: #1e1e1e;
--color-accent: #6cb4ff;
}
body {
background-color: var(--color-bg);
color: var(--color-text);
}
a {
color: var(--color-accent);
}
A theme toggle then only needs to set one attribute, and can remember the choice:
const toggle = document.querySelector('#theme-toggle');
toggle.addEventListener('click', () => {
const root = document.documentElement;
const next = root.dataset.theme === 'dark' ? 'light' : 'dark';
root.dataset.theme = next;
localStorage.setItem('theme', next);
});
Check contrast in both themes. Each text and background pair needs to meet WCAG contrast ratios on its own, so test the dark values rather than assuming they work. Our guide to dark mode web design covers color choices and accessibility in more detail.
Real-time changes with JavaScript
Because custom properties live in the browser, JavaScript can read and update them. That makes features like a brand color picker or a user-adjustable text size straightforward:
function updateThemeColor(color) {
document.documentElement.style.setProperty('--color-accent', color);
}
// Read the current value
const accent = getComputedStyle(document.documentElement)
.getPropertyValue('--color-accent')
.trim();
Component-level customization
Variables can be scoped to any selector, which is useful for reusable components. Set defaults on the component and override them for a variation, without touching the component’s internal rules:
.card {
--card-bg: var(--color-surface);
--card-padding: 1.5rem;
background: var(--card-bg);
padding: var(--card-padding);
}
.card--featured {
--card-bg: var(--color-accent);
--card-padding: 2rem;
}
This pattern also works across component libraries and web components, because custom properties inherit through the shadow DOM boundary while ordinary selectors don’t.
Design tokens and WordPress theme.json
In WordPress block themes, colors, font sizes and spacing defined in theme.json are output as CSS custom properties such as --wp--preset--color--primary. Using those variables in your theme’s CSS keeps custom styles in sync with the choices editors see in the block editor.

Common questions about CSS variables
Do CSS variables affect performance?
In normal use, the effect is negligible. Browsers handle custom properties efficiently. The one thing to watch is changing a variable on :root many times per second, for example on every scroll or mouse move, because every element that uses it has to be recalculated. Scope frequently changing variables to the element that needs them.
Can you override CSS variables?
Yes. Custom properties follow the normal cascade, so a value set on a more specific selector or a nested element overrides the global one for that part of the page. That is how the dark theme and the featured card examples above work.
Are CSS variables supported in all browsers?
Yes. Custom properties have been supported in every major browser for years. The @property rule, which lets you register a variable with a type so it can be animated smoothly, is also supported in current versions of Chrome, Edge, Firefox and Safari.
When shouldn’t you use CSS variables?
Skip them for one-off values that are used once and never change, such as a specific offset on a single decorative element. A variable there adds indirection without any benefit.
Where to learn more
MDN Web Docs has thorough reference pages for custom properties, var() and @property. The fastest way to build fluency is to convert the colors and spacing in an existing stylesheet to variables, then add a dark theme by redefining them.
Conclusion
CSS variables give you one place to manage the values that define a site’s look. Define purpose-named tokens on :root, redefine them for themes and breakpoints, and keep component styles pointed at the variables. The result is a stylesheet that is easier to maintain and a site that can adapt to dark mode, rebrands and user preferences without rewriting rules.
Want a maintainable variable system across your theme? Our front-end development team can build it into your site. Get a proposal.
Get started now
Start with a risk-free, no obligation proposal delivered to your inbox in one business day or less.
Get Started- 5-Star Rated Technical Partner
- 100% White-Label - Sign our NDA
- 90-Day Code Guarantee