CSS Custom Properties vs Sass Variables
Sass variables ($brand) and CSS custom properties (--brand) look similar but live in completely different worlds. Knowing which to use — and how to combine them — is one of the most useful skills in modern Sass.
The key difference
Sass variable $brand |
CSS custom property --brand |
|
|---|---|---|
| Exists | Only while compiling | In the browser, at runtime |
| Changeable by JavaScript | No | Yes (el.style.setProperty) |
Can change inside @media, :hover, .dark |
No — value is fixed in the output | Yes — follows the cascade |
| Inherited by child elements | No (no such concept) | Yes |
| Usable in selectors, media queries, loops, math | Yes | No (only in property values) |
| Shows up in dev tools | Only its final value | As a live, editable variable |
Sass variables are baked in
$brand: #cc6699;
.btn {
background: $brand;
}
.dark .btn {
$brand: #f48fb1; // local variable: only affects this block
background: $brand;
}.btn {
background: #cc6699;
}
.dark .btn {
background: #f48fb1;
}
To support a dark variant with Sass variables alone, you must write a separate rule for every component that uses the colour.
Custom properties follow the cascade
:root {
--brand: #cc6699;
}
.dark {
--brand: #f48fb1;
}
.btn {
background: var(--brand);
}:root {
--brand: #cc6699;
}
.dark {
--brand: #f48fb1;
}
.btn {
background: var(--brand);
}
One .btn rule now works in both themes, because var(--brand) is resolved by the browser for each element.
Best of both: generate custom properties from Sass
Keep your tokens in a Sass map (so you can loop, compute and validate), then output them as custom properties. Sass values must be interpolated with #{} when assigned to a custom property:
@use "sass:color";
$tokens: (
brand: #cc6699,
ink: #2d2a32,
surface: #ffffff,
radius: 10px,
);
:root {
@each $name, $value in $tokens {
--#{$name}: #{$value};
}
--brand-soft: #{color.scale(#cc6699, $lightness: 75%)};
}
.card {
background: var(--surface);
color: var(--ink);
border: 2px solid var(--brand);
border-radius: var(--radius);
}:root {
--brand: #cc6699;
--ink: #2d2a32;
--surface: #ffffff;
--radius: 10px;
--brand-soft: rgb(95%, 85%, 90%);
}
.card {
background: var(--surface);
color: var(--ink);
border: 2px solid var(--brand);
border-radius: var(--radius);
}
Note: The value of a custom property is mostly treated as raw text, which is why
--x: $value;(without#{}) outputs the literal text$value. Always interpolate.
When to use which
| Use a Sass variable when… | Use a custom property when… |
|---|---|
The value is needed at compile time (loops, math, selectors, @media breakpoints) |
The value changes at runtime (themes, user settings, JavaScript) |
You compute other values from it (color.scale($brand, …)) |
It should differ by context (component variants, dark mode) |
| It's a private implementation detail | It's part of a public API others may override |
Breakpoints can't be custom properties
@media (min-width: var(--md)) does not work — media queries can't read custom properties. Breakpoints are a classic case for Sass variables.
Tip: A good pattern: Sass maps hold the raw design tokens, a
:rootblock publishes them as custom properties, and components usevar(--…). You get compile-time checks and runtime theming.
Test yourself
Which can be changed by JavaScript after the page loads?
- $brand
- --brand
- Both
- Neither
How do you set a custom property from a Sass variable?
- --brand: $brand;
- --brand: #{$brand};
- $--brand: $brand;
- --brand: var($brand);
Why are breakpoints usually Sass variables, not custom properties?
- Custom properties can't hold pixel values
- Media queries can't use var()
- Sass is faster
- Custom properties aren't inherited