CSS Custom Properties for Theme Variables
Theme variables without a preprocessor
By Naeem Noor on Updated on
Sass and LESS variables compile to static values. Custom properties live in the browser and can change at runtime.
The old hack vs the modern fix
View demoOld wayPreprocessor
// Sass/LESS: compile-time only
$primary: #7c3aed;
$spacing: 16px;
.btn {
background: $primary;
padding: $spacing;
}Modern4 lines
:root {
--primary: #7c3aed;
--spacing: 16px;
}
.btn {
background: var(--primary);
}Custom properties browser support
BaselineWidely availableThis feature is well established and works across many devices and browser versions. It has been available across browsers since 2017.Safe to use without fallbacks.
Usage97%of people can use this3% use a browser without it.
Browsers
49+
31+
9.1+
15+Last checked 2 Nov 2026 · web-features 3.45.0
Live demo
pick a colour and the card follows one variable
Why the modern way wins
Runtime updates
Change --primary in JS or a class and every use updates. No rebuild.
No build step
Plain CSS. Works in any environment, no Sass or LESS required.
Cascade and override
Set on :root, override in .dark or a component. Inherits like normal CSS.
- Lines Saved
- N/A
- No preprocessor
- Old Approach
- Sass/LESS vars
- Compile to static values
- Modern Approach
- Custom properties
- Update at runtime
How it works
Preprocessor variables like $primary: #7c3aed are replaced at compile time. The output is plain hex. To switch themes you recompile or generate multiple stylesheets.
Custom properties (--primary: #7c3aed) are real CSS. You read and set them with var(--primary). Toggle a class on the root or use JS to change --primary and every reference updates. No build, no duplicate CSS.