CSS Variables
CSS custom properties — usually called CSS variables — let you store a value once and reuse it everywhere. Change the variable and every place that uses it updates. They're the foundation of themes, dark mode and design systems.
Declaring and using variables
A custom property name starts with two dashes. You read it with the var() function.
:root {
--brand: #6200ea;
--radius: 10px;
}
.button {
background: var(--brand);
border-radius: var(--radius);
}Declaring variables on :root (the <html> element) makes them available to the whole page, because custom properties are inherited.
<!DOCTYPE html>
<html>
<head>
<style>
:root {
--brand: #6200ea;
--accent: #ffab00;
--radius: 12px;
--space: 14px;
}
body { font-family: system-ui, sans-serif; padding: var(--space); }
.card { border: 3px solid var(--brand); border-radius: var(--radius); padding: var(--space); max-width: 280px; }
.card h3 { color: var(--brand); margin-top: 0; }
.btn { background: var(--brand); color: white; border: none; border-radius: var(--radius); padding: 10px 18px; font: inherit; }
.tag { background: var(--accent); padding: 2px 8px; border-radius: var(--radius); }
</style>
</head>
<body>
<div class="card">
<h3>Variables <span class="tag">new</span></h3>
<p>Change <code>--brand</code> in the :root rule and everything updates.</p>
<button class="btn">Button</button>
</div>
</body>
</html>Tip: Try editing
--brandto#00897band--radiusto0in the playground above.
Fallback values
var() accepts a second argument used when the variable is not defined: var(--gap, 8px). Fallbacks can even contain another var().
Scope and overriding
Variables follow the cascade. Redefine one on a more specific element and it changes only inside that element — perfect for variants.
<!DOCTYPE html>
<html>
<head>
<style>
:root { --color: #1e88e5; }
body { font-family: system-ui, sans-serif; display: flex; gap: 10px; flex-wrap: wrap; }
.alert { --color: #1e88e5; border-left: 6px solid var(--color); background: color-mix(in srgb, var(--color) 12%, white); color: var(--color); padding: 12px; width: 150px; border-radius: 6px; }
.alert.success { --color: #2e7d32; }
.alert.danger { --color: #c62828; }
.alert.warn { --color: #ef6c00; }
</style>
</head>
<body>
<div class="alert">Info alert</div>
<div class="alert success">Success alert</div>
<div class="alert danger">Danger alert</div>
<div class="alert warn">Warning alert</div>
</body>
</html>One component rule, four variants — each variant only changes --color.
Theming and dark mode
Switch an entire colour scheme by redefining variables under a class or a media query.
<!DOCTYPE html>
<html>
<head>
<style>
.theme { --bg: #ffffff; --text: #212121; --muted: #757575; --card: #f5f5f5; --primary: #3949ab; }
.theme.dark { --bg: #121212; --text: #eeeeee; --muted: #9e9e9e; --card: #1e1e1e; --primary: #9fa8da; }
body { margin: 0; font-family: system-ui, sans-serif; display: flex; }
.theme { flex: 1; background: var(--bg); color: var(--text); padding: 14px; min-height: 220px; }
.theme .card { background: var(--card); padding: 12px; border-radius: 10px; }
.theme small { color: var(--muted); }
.theme a { color: var(--primary); }
</style>
</head>
<body>
<div class="theme"><div class="card"><b>Light</b><br><small>Same HTML, same rules.</small><br><a href="#">A link</a></div></div>
<div class="theme dark"><div class="card"><b>Dark</b><br><small>Only variables differ.</small><br><a href="#">A link</a></div></div>
</body>
</html>In a real site you'd put the dark values inside @media (prefers-color-scheme: dark) { :root { … } }.
Variables with calc()
Variables hold any token sequence — numbers, lengths, colours, even partial values. Combine them with calc() to derive sizes from one base value.
<!DOCTYPE html>
<html>
<head>
<style>
:root { --unit: 8px; }
body { font-family: system-ui, sans-serif; }
.bar { height: 26px; margin: 6px 0; background: linear-gradient(90deg, #ff4081, #7c4dff); border-radius: 6px; color: white; padding-left: 8px; line-height: 26px; }
.s1 { width: calc(var(--unit) * 10); }
.s2 { width: calc(var(--unit) * 20); }
.s3 { width: calc(var(--unit) * 30); }
.s4 { width: calc(var(--unit) * 40); }
</style>
</head>
<body>
<div class="bar s1">×10</div>
<div class="bar s2">×20</div>
<div class="bar s3">×30</div>
<div class="bar s4">×40</div>
<p>Change <code>--unit</code> to 5px and all bars rescale.</p>
</body>
</html>Key facts
| Fact | Detail |
|---|---|
| Naming | Must start with --; case-sensitive (--Main ≠ --main) |
| Inheritance | Inherited by descendants |
| Fallback | var(--x, fallback) |
| Invalid value | If a variable produces an invalid value, the property falls back to its inherited/initial value |
| JavaScript | el.style.setProperty('--brand', 'red') updates it live |
| Limits | Can't be used in media query conditions or selectors |
Test yourself
How do you read the value of a custom property named --gap?
- $gap
- var(--gap)
- --gap()
- @gap
Why are global variables usually declared on :root?
- It's required syntax
- Custom properties inherit, so values on the root element reach every element
- It's faster
- Only :root can hold variables
What does var(--size, 16px) do if --size is not defined?
- Throws an error
- Uses 16px
- Uses 0
- Ignores the whole rule