A website repeats the same values everywhere: its brand colour, its corner radius, its spacing. CSS custom properties, usually called CSS variables, let you define each value once and reuse it by name. Change the variable and every place that uses it updates. Because variables follow the cascade, you can also override them for one component or switch a whole page to a dark theme by changing a handful of values.
In this lesson you will learn
- Declaring variables with
--name: value - Using them with
var()and providing fallbacks - Global variables on
:root - Overriding variables for one component
- Building light and dark themes
Declaring and Using Variables
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CSS variables</title>
<style>
:root {
--brand: #00695c;
--radius: 8px;
--space: 12px;
}
body { font-family: sans-serif; margin: var(--space); }
.btn { background: var(--brand); color: white; border: 0; padding: 8px 14px;
border-radius: var(--radius); margin-right: 6px; }
.btn.danger { --brand: #c62828; } /* override for one component */
.card { border: 2px solid var(--brand); border-radius: var(--radius);
padding: var(--space); margin-top: var(--space); }
.card h3 { margin: 0; color: var(--brand); }
.muted { color: var(--text-muted, #757575); } /* fallback when not defined */
.wide { --space: 24px; }
</style>
</head>
<body>
<button class="btn">Save</button><button class="btn danger">Delete</button>
<div class="card"><h3>Card</h3><p class="muted">Uses --brand, --radius and --space.</p></div>
<div class="card wide"><h3>Wide card</h3><p class="muted">Same CSS, --space changed to 24px.</p></div>
</body>
</html>
Result
- A custom property name must start with two dashes:
--brand: #00695c;. Names are case-sensitive. - Declaring variables on
:root(the<html>element) makes them available everywhere on the page. var(--brand)reads the value.var(--text-muted, #757575)supplies a fallback for when the variable is not defined.- Variables are inherited and follow the cascade.
.btn.danger { --brand: #c62828; }changes the value only inside that button, so the same button CSS produces a red version. .wide { --space: 24px; }changes the padding of one card without touching its other rules.
Themes With Variables
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Theme switch</title>
<style>
body { font-family: sans-serif; margin: 0; }
.theme { --bg: #ffffff; --text: #212121; --accent: #1565c0; --card: #f5f5f5; }
.theme.dark { --bg: #121212; --text: #e0e0e0; --accent: #90caf9; --card: #1e1e1e; }
.panel { background: var(--bg); color: var(--text); padding: 12px; transition: background .3s, color .3s; }
.panel a { color: var(--accent); }
.panel .card { background: var(--card); padding: 10px; border-radius: 6px; margin-top: 8px; }
.row { display: flex; }
.row > * { flex: 1; }
</style>
</head>
<body>
<div class="row">
<div class="theme panel"><strong>Light theme</strong><div class="card">Same HTML, <a href="#top">a link</a></div></div>
<div class="theme dark panel"><strong>Dark theme</strong><div class="card">Same HTML, <a href="#top">a link</a></div></div>
</div>
</body>
</html>
Result
- Both panels use exactly the same rules, written with variables. Only the variable values differ between
.themeand.theme.dark. - On a real site you would put the dark values in
@media (prefers-color-scheme: dark) { :root { ... } }to follow the user’s system setting, or switch adarkclass on<html>with JavaScript:document.documentElement.classList.toggle("dark"). - Variables can be animated with transitions only through the properties that use them, as the
backgroundandcolortransitions show.
CSS Variables vs Preprocessor Variables
| CSS custom properties | Sass/LESS variables | |
|---|---|---|
| Syntax | --brand / var(--brand) | $brand / @brand |
| Exists in the browser | Yes | No, replaced at build time |
| Can change at run time | Yes (media queries, classes, JavaScript) | No |
| Follows the cascade | Yes | No |
| Needs a build tool | No | Yes |
JavaScript can read and write variables: getComputedStyle(el).getPropertyValue("--brand") and el.style.setProperty("--brand", "#6a1b9a").
Common Mistakes
- Forgetting the two dashes or the
var()wrapper:color: --branddoes nothing. - Using a variable in a media query condition, such as
@media (min-width: var(--bp)), which does not work. - Building values from pieces like
var(--size)px; store the unit in the variable or usecalc(var(--size) * 1px). - Hard-coding colours in new rules after creating variables, which defeats the purpose.
Practice Exercises
Exercise 1. Define --gap as 16px and use it for the gap of a flex container and the padding of its items.
Show answer
:root { --gap: 16px; }
.row { display: flex; gap: var(--gap); }
.row > * { padding: var(--gap); }
Exercise 2. Switch to dark colours automatically when the user’s system is in dark mode.
Show answer
:root { --bg: #fff; --text: #222; }
@media (prefers-color-scheme: dark) {
:root { --bg: #121212; --text: #e0e0e0; }
}
body { background: var(--bg); color: var(--text); }
Frequently Asked Questions
Are CSS variables supported in all browsers?
Yes. Every modern browser has supported custom properties since about 2017. Only Internet Explorer, which is retired, lacks them.
Can I use calc() with variables?
Yes, and it is common: padding: calc(var(--space) * 2); doubles the spacing value.
Summary
- Declare with
--name: value, usually on:root; read withvar(--name, fallback). - Variables inherit and can be overridden per component.
- Changing a few variables creates themes such as dark mode.
- Unlike preprocessor variables, they can change at run time.
