Sub topic 6
Primitive, semantic and component tokens
This chapter assumes you can declare custom properties and read them with var(). It also assumes you know that custom properties inherit down the DOM tree and that var() accepts a fallback. Here you will split tokens into three tiers, decide which tier a value belongs in, and see how the tiers resolve in the browser.
Why one flat list of tokens stops working
A small project can get by with a flat list: --blue, --gray, --padding. Problems start when the design grows.
--blueis used for links, buttons, focus rings, and an info banner. Changing the brand color means finding out which uses were "brand" and which were "informational".- A dark theme has to replace values, but
--gray-900as text on a light page is a surface color on a dark one. The name describes the paint, not the job. - A button needs slightly less padding in one place. Editing the shared
--paddingchanges every other component that reads it.
Tiers solve this by giving each token one job. A primitive token says what a value is. A semantic token says what a value is for. A component token says which knob one component exposes.
Primitive tokens: the raw palette
A primitive token holds a raw value and carries no meaning about where it is used. Primitives are your palette: every color, size, and font you are allowed to use.
Name primitives by what they are, not by their use. --color-blue-500 is a good primitive name. --color-button-bg is not, because it names a use.
Primitives usually come in scales: a numbered ramp of a hue (--color-blue-100 to --color-blue-900), a spacing ladder, a type size ladder. Components should rarely read primitives directly. If a rule says color: var(--color-blue-500), you have lost the ability to change "what accent means" in one place.
Semantic tokens: roles
A semantic token gives a role to a primitive. The role is what the interface needs: text, surface, accent, border, danger. The value is a reference to a primitive.
:root {
/* Primitive tokens: raw values */
--color-blue-500: #2563eb;
--color-gray-50: #f9fafb;
--color-gray-900: #111827;
/* Semantic tokens: meaning, built from primitives */
--color-text: var(--color-gray-900);
--color-surface: var(--color-gray-50);
--color-accent: var(--color-blue-500);
}
.button {
background: var(--color-accent);
color: white;
}
.card {
background: var(--color-surface);
color: var(--color-text);
}The rules at the bottom never mention a primitive. To rebrand, you point --color-accent at a different primitive and no component rule changes. Themes work the same way: a dark theme redefines the semantic tokens and leaves the primitives alone. The next chapter builds on this.
Semantic tokens are also where design intent lives. --color-danger and --color-accent might both point to the same hex value today. Keeping two names lets them diverge later without a search-and-replace.
Component tokens: the local knobs
A component token is declared on the component's own selector and exposes the values that component lets its variants or its users change. Its default usually comes from a semantic or shared-scale token.
:root {
--space-2: 0.5rem;
--space-3: 0.75rem;
--space-4: 1rem;
}
.card {
/* Component token with a default taken from the shared scale */
--card-padding: var(--space-4);
padding: var(--card-padding);
}
.card--compact {
--card-padding: var(--space-2);
}
.badge {
/* Fallback used only when --badge-padding is not defined */
padding: var(--badge-padding, var(--space-3));
}Two patterns appear here, and they differ in who owns the default.
- Declared default (
.card). The component declares--card-paddingitself and reads it. A variant like.card--compactoverrides the one value. Thepaddingrule is written once. - Fallback default (
.badge). The component never declares--badge-padding. It reads it with a fallback, so a parent or a variant can set it from outside, and without that the badge uses--space-3.
Use the declared default when the component owns the knob. Use the fallback form when the knob is an optional hook for the surrounding page.
The tradeoff is that a declared default on .card also wins over a value set on an ancestor, because a declaration on the element itself beats inheritance. If a page section sets --card-padding: 2rem on a wrapper, .card ignores it. With the fallback pattern, the ancestor value flows in.
How the tiers resolve
References point one way only: component tokens read semantic tokens, and semantic tokens read primitives. Properties such as padding and background read the component or semantic layer.
The dotted line shows that a rule can skip the component tier. That is fine for simple components that expose no knobs.
A naming pattern that keeps the tiers readable
The name should tell you the tier without opening the file.
| Tier | Pattern | Examples | Describes |
|---|---|---|---|
| Primitive | `--category-hue-step` | `--color-blue-500`, `--space-4` | What the value is |
| Semantic | `--category-role` | `--color-text`, `--color-accent` | What the value is for |
| Component | `--component-property` | `--card-padding`, `--button-bg` | Which knob a component exposes |
A primitive name ends in a step or number. A semantic name ends in a role. A component name starts with the component. Spacing is the common exception: --space-4 works as both a primitive and a shared-scale token, and many teams accept that rather than inventing an extra layer for it.
Where resolution happens: the scope gotcha
This is the edge case that surprises people most. A custom property that contains var() is resolved on the element where it is declared. Its resolved value is then what children inherit. Children do not re-run the var() with their own local values.
:root {
--color-blue-500: #2563eb;
--color-red-500: #dc2626;
--color-accent: var(--color-blue-500);
}
.alert {
/* Does not change --color-accent for descendants */
--color-blue-500: #dc2626;
}
.alert a {
color: var(--color-accent); /* still #2563eb */
}--color-accent was resolved on :root to #2563eb. Descendants of .alert inherit that resolved value. Overriding a primitive inside .alert only matters to rules that read the primitive directly. The fix is to override at the semantic tier, in the scope:
.alert {
--color-accent: var(--color-red-500);
}Now --color-accent is declared on .alert and resolves there, so every descendant sees red. This is why themes and scoped variations belong at the semantic tier and not at the primitive tier.
Component tokens and variants
Component tokens are the cleanest way to write variants. Instead of repeating full property declarations in each variant, a variant changes only the tokens.
.button {
--button-bg: var(--color-accent);
--button-fg: white;
--button-padding: var(--space-3) var(--space-4);
background: var(--button-bg);
color: var(--button-fg);
padding: var(--button-padding);
}
.button--danger {
--button-bg: var(--color-danger);
}
.button--small {
--button-padding: var(--space-2) var(--space-3);
}The .button rule defines structure once. Each variant is one or two lines. Because both variants only set tokens, they combine without a conflict: class="button button--danger button--small" changes the background and the padding independently.
This example uses --color-danger and --space-*, which you would define in :root like the earlier ones.
When to skip a tier
Three tiers is a default, not a rule.
- Small sites. If you have one theme and a dozen colors, primitives plus semantic tokens are enough. Add component tokens only when a component gains variants.
- Single-use values. A value used in exactly one rule, such as a one-off
max-width, does not need a token. Tokens are for decisions the interface shares. - Component tokens everywhere. Giving every property of every component a token creates many names with no consumers. Expose a knob when a variant or a page needs it.
Common mistakes
- Components reading primitives.
background: var(--color-blue-500)bypasses the role layer. A theme change then needs edits in the components. - Semantic names that describe appearance.
--color-light-grayas a semantic token breaks as soon as dark mode makes it dark. Use--color-surface. - Circular references.
--a: var(--b); --b: var(--a);makes both properties invalid at computed-value time. The declarations are treated as unset, which usually looks like a missing style. - Component tokens declared too low. If
.carddeclares--card-paddingon itself, a wrapper cannot override it by inheritance. Pick the declared or fallback pattern deliberately. - Skipping the scope's cascade. Setting a semantic token on a wrapper only affects descendants of that wrapper. Elements outside it, such as a portal or modal appended to
body, keep the root values.
Exercise
A page defines --color-accent: var(--color-blue-500) on :root. A .promo section sets --color-blue-500: #16a34a; and contains a link styled with color: var(--color-accent). What color is the link, and what is the smallest change that makes it green?
Show answer
The link stays blue (#2563eb). --color-accent was resolved on :root, and .promo descendants inherit that resolved value. The --color-blue-500 override only affects rules that read the primitive directly. The smallest fix is to declare --color-accent inside .promo, for example .promo { --color-accent: #16a34a; } or point it at a green primitive with var(). Then it resolves on .promo and the link inherits green.
Exercise
You have .badge { padding: var(--badge-padding, var(--space-3)); } and a page that wraps badges in .sidebar { --badge-padding: 0.25rem; }. Does the badge padding change? Would it change if .badge declared --badge-padding: var(--space-3); on itself instead?
Show answer
Yes, the first version changes. .badge never declares --badge-padding, so the value inherited from .sidebar is used and the fallback is ignored. In the second version it would not change, because a declaration on the .badge element itself beats the inherited value from .sidebar.
Exercise
Sort these names into primitive, semantic, and component tokens: --color-gray-900, --color-surface, --card-padding, --space-4, --button-bg, --color-danger.
Show answer
Primitive: --color-gray-900, --space-4. Semantic: --color-surface, --color-danger. Component: --card-padding, --button-bg.