Sub topic 8
Design tokens with CSS custom properties
This chapter assumes you know CSS selectors, the cascade, inheritance and specificity. It also assumes you have met BEM naming from the previous chapter and can read a short @layer declaration. No build tools are needed.
What a design token is
A design token is a named design decision: a color, a spacing step, a font size, a radius. Components refer to the name instead of repeating the raw value. When the decision changes, you edit one declaration.
Without tokens, a brand color appears as #0b5fff in dozens of files. Some copies drift to #0b5ffe or #0a5fff. Nobody can tell which copies were intentional. A token gives each decision one source of truth and a name that says what the value is for.
In plain CSS, tokens are custom properties. These are names that start with --, declared like any other property and read with var().
:root {
--color-text: #1a1a1a;
--color-accent: #0b5fff;
--space-2: 0.5rem;
--space-4: 1rem;
}
.button {
color: var(--color-text);
padding: var(--space-2) var(--space-4);
border: 2px solid var(--color-accent);
}:root matches the <html> element. Declaring tokens there makes them available to every element, because custom properties inherit.
How custom properties work
Custom properties differ from Sass or Less variables in three ways that matter for architecture.
- They live in the browser. Sass variables disappear at compile time. Custom properties exist at runtime, so you can change them with a media query, a class, a data attribute or JavaScript.
- They follow the cascade. A declaration on
.cardoverrides one on:rootfor that element and its descendants. The usual specificity and source-order rules apply to the declaration itself. - They inherit. An element gets the value from its nearest ancestor that sets it. This lets you re-theme a subtree by setting tokens on its root element.
The browser substitutes var() when it computes each element's values. The following diagram shows that flow.
Tiers of tokens
A flat list of tokens stops working once you have more than a few dozen. Most teams use three tiers.
| Tier | Example | Describes | Used by |
|---|---|---|---|
| Primitive | `--blue-600` | A raw value in a palette or scale | Only other tokens |
| Semantic | `--color-accent` | The role a value plays | Components and pages |
| Component | `--button-bg` | A knob on one component | That component |
Primitives name what a value is. Semantic tokens name what it is for. Components read semantic tokens and never touch primitives.
:root {
/* primitives */
--blue-600: #0b5fff;
--red-600: #d11f3a;
--gray-900: #1a1a1a;
/* semantic */
--color-accent: var(--blue-600);
--color-danger: var(--red-600);
--color-text: var(--gray-900);
}
.button {
--button-bg: var(--color-accent);
background: var(--button-bg);
color: white;
}
.button--danger {
--button-bg: var(--color-danger);
}The BEM modifier .button--danger changes one component token. The base rule does not need to know which variants exist. Both selectors have the same specificity, so source order decides, as it should.
Theming by overriding semantic tokens
Because tokens are runtime values, a theme is a set of overrides for the semantic tier.
:root {
--color-bg: #ffffff;
--color-text: #1a1a1a;
}
@media (prefers-color-scheme: dark) {
:root {
--color-bg: #121212;
--color-text: #f2f2f2;
}
}
[data-theme="dark"] {
--color-bg: #121212;
--color-text: #f2f2f2;
}Components such as .card and .button need no changes. They already use var(--color-bg).
The second block repeats the first. Plain CSS has no way to share one block between a media query and an attribute selector. If the duplication bothers you, generate one of the two blocks in your build step. Accept it otherwise.
You can also scope a theme to a region of the page:
.promo {
--color-bg: #0b1f4d;
--color-text: #ffffff;
background: var(--color-bg);
color: var(--color-text);
}Every descendant of .promo that reads these tokens now uses the promo colors.
Common errors
Overriding a primitive and expecting semantic tokens to follow
This is the most frequent surprise. var() is resolved where the custom property is declared, then the resolved value is inherited.
:root {
--blue-600: #0b5fff;
--color-accent: var(--blue-600);
}
.promo {
--blue-600: #7a3cff; /* does NOT change --color-accent inside .promo */
}--color-accent was computed on :root as #0b5fff. Descendants inherit that finished value. To change it inside .promo, redeclare the semantic token there:
.promo {
--color-accent: var(--blue-600);
--blue-600: #7a3cff;
}Redeclaring --color-accent on .promo makes it resolve against .promo's own --blue-600. In practice, override semantic tokens and leave primitives alone.
Misreading the fallback
var(--space-3, 1rem) uses 1rem only when --space-3 is not defined. If you write --space-3: banana, the fallback is not used. The property that consumes it becomes invalid at computed-value time. It is then treated as unset, which means the inherited value for inherited properties and the initial value for the rest. The symptom is a rule that silently does nothing.
A typo in a token name behaves like an undefined token. Without a fallback, the property is dropped. Use browser dev tools to look for a var() shown with a warning icon. Stylelint can also catch these (see the linting chapter).
Using `var()` where it is not allowed
You can use var() only as a property value or inside another custom property. You cannot use it in selectors, property names, or media query conditions. This does not work:
@media (min-width: var(--bp-md)) { /* invalid */
.nav { display: flex; }
}Write breakpoints as literal values, or generate them at build time. Container queries and calc() can read tokens, but media query conditions cannot.
Unit mistakes
A token stores a token stream, not a typed value. --gap: 8 is valid as a custom property, but padding: var(--gap) is invalid because 8 has no unit. Either store the unit (--gap: 8px) or multiply in calc(): calc(var(--gap) * 1px). Prefer storing full values.
Naming tokens after their value
--color-blue stops making sense the day the brand turns green. Name semantic tokens after their role (--color-accent, --color-surface) and keep value names such as --blue-600 in the primitive tier, where they are honest.
Too many tokens
A token that is used once is a constant with a longer name. Add a token when a value is shared across components, expected to change together, or part of a theme. A one-off margin-top: 3px fix does not need one.
Typed tokens with `@property`
By default every custom property is an untyped string. @property registers a type, an inheritance behavior and an initial value.
@property --space-4 {
syntax: "<length>";
inherits: true;
initial-value: 1rem;
}With a registered type, the browser rejects invalid values at declaration time and can animate the property. Without it, a transition on a color token will jump instead of fade. Registration is optional, so reserve it for tokens that need validation or animation.
Where tokens fit in the architecture
Put primitive and semantic tokens in the earliest, lowest-specificity part of your stylesheet. With cascade layers, that is the base layer:
@layer reset, base, components, utilities;
@layer base {
:root {
--color-text: #1a1a1a;
--space-4: 1rem;
}
}Keep component tokens such as --button-bg inside the component's own rule. Tokens declared outside layers beat tokens declared inside any layer, so avoid mixing the two for the same property. A stray unlayered :root block can quietly win over your layered theme.
Practical rules
- Declare primitives once, in one file, and never use them in components.
- Give every semantic token a role-based name with a consistent prefix (
--color-,--space-,--font-size-). - Theme by overriding semantic tokens on
:root,[data-theme]or a region class. - Add a fallback to
var()only where a missing token is a real possibility, such as a component used outside its normal context. - Keep breakpoints as literals or generate them in the build.
Exercise
A page defines --blue-600: #0b5fff; and --color-accent: var(--blue-600); on :root. A .sidebar rule sets --blue-600: #7a3cff;, but the button inside .sidebar that uses background: var(--color-accent) stays #0b5fff. Why does this happen, and what is the smallest change that makes the button purple?
Show answer
--color-accent was resolved on :root, where --blue-600 is #0b5fff. Descendants inherit that resolved value, not the var() reference. Redeclare --color-accent: var(--blue-600); on .sidebar, next to the new --blue-600 value, so it resolves against the sidebar's own primitive. Alternatively, set --color-accent: #7a3cff; directly on .sidebar.
Exercise
You write padding: var(--gap); with --gap: 8; and the padding has no effect. Name the mechanism and give two fixes.
Show answer
The declaration is invalid at computed-value time because 8 is a number without a unit, so padding becomes unset (its initial value, 0). Fix it by storing the unit (--gap: 8px;) or by writing padding: calc(var(--gap) * 1px);.