- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Variables
CSS Foundations
CSS Variables
Define reusable values that participate in the cascade and can change across components, themes, and media conditions. The examples show global tokens, component-level overrides, fallbacks, inheritance, and typed properties registered with @property.
CSS variables store a value once and reuse it everywhere: write --brand: #0f766e, then use var(--brand) wherever you need that color. Change one line and the whole site updates. Unlike Sass variables, they live in the browser and can change while the page runs.
The official name is custom properties. Because they are real CSS properties, they follow the cascade and inheritance rules: a value set on a parent is available to all its children, and can be changed for one section only. That is what makes them perfect for CSS Theming such as dark mode.
Declare and use
Example
css
:root {
--color-accent: #0f766e;
--space-card: 1.25rem;
}
.card {
padding: var(--space-card);
border-top: 0.25rem solid var(--card-accent, var(--color-accent));
}A variable name starts with two hyphens, and var() reads it. Global values usually go on :root, the top of the page. The second value inside var(--card-accent, var(--color-accent)) is a fallback: if --card-accent is not set, the card uses the site accent color.
Names are case-sensitive: --Brand and --brand are different variables.
Variables as a component's settings
Example
css
.button {
--button-bg: #334155;
--button-fg: white;
background: var(--button-bg);
color: var(--button-fg);
}
.button--danger { --button-bg: #b91c1c; }The button reads its colors from its own variables. The danger version changes just one variable instead of repeating the whole rule. This is how most design systems use them.
A typo does not fall back
If a variable holds something invalid for where it is used, for example --space: red inside padding, the fallback is not used. The property quietly becomes its default value instead. So if a style "just disappears", check the variable's value in DevTools.
Giving a variable a type
Example
css
@property --progress {
syntax: "<number>";
inherits: false;
initial-value: 0;
}Normally the browser treats a variable as plain text and cannot animate it. @property tells it the variable is a number, length or color, so it can be transitioned smoothly, which enables effects like animated gradients.
Custom Property Syntax
| Syntax | Purpose | Example |
|---|---|---|
| --name | Declares a custom property. | --space: 1rem |
| var() | Reads a custom property. | gap: var(--space) |
| var() fallback | Supplies a value when the property is invalid or missing. | color: var(--ink, black) |
| @property | Registers type, inheritance, and an initial value. | @property --angle { ... } |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Variables example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
:root {
--color-accent: #0f766e;
--space-card: 1.25rem;
}
.card {
padding: var(--space-card);
border-top: 0.25rem solid var(--card-accent, var(--color-accent));
}
.button {
--button-bg: #334155;
--button-fg: white;
background: var(--button-bg);
color: var(--button-fg);
}
.button--danger { --button-bg: #b91c1c; }
@property --progress {
syntax: "<number>";
inherits: false;
initial-value: 0;
}
</style>
</head>
<body>
<main><article class="card" style="--card-accent:#7c3aed"><h1>Local design tokens</h1><button class="button">Default</button> <button class="button button--danger">Danger</button></article></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| Custom properties | 49 | 15 | 31 | 9.1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
CSS Variables exercises
Define and use a variable
On :root define --brand with the value #008080. Make the title use it as its color.
The HTML your code works on
<div class="theme">
<h2 class="title">Hello</h2>
<p class="text">Body text</p>
<button class="btn">Go</button>
</div>Result
