Design Tokens and Theming With Custom Properties
Building a token system in CSS: primitive, semantic and component tokens, numeric scales, spacing and type scales, theming by swapping semantic tokens, component APIs with var() fallbacks, the derived-token trap and typed tokens with @property.
kvi5hnuCode Sprout team member8 min readOn this page
A design token is a named design decision: a color, a spacing step, a font, a radius, a shadow, a duration. Instead of writing #1d4ed8 in forty places, a stylesheet writes var(--color-accent) and defines the value once. Custom properties (article 27) are how tokens live in CSS, and with a little structure they turn into a theming system: dark mode, brands, density and high contrast become a matter of swapping a few values.
Three tiers

Large systems separate tokens by how much meaning they carry:
- Primitive tokens (also called base, raw or reference tokens) are the palette: every available value, named by what it is.
--blue-600,--gray-50,--space-3,--radius-md. They say nothing about use. - Semantic tokens (alias or system tokens) name a purpose:
--color-accent,--color-surface,--color-text-muted,--space-inset. Each points to a primitive. Themes are defined at this tier. - Component tokens are a component's own settings:
--button-bg,--card-padding. They point to semantic tokens and let one component be adjusted without touching others.
@layer theme {
:root {
/* primitives */
--blue-300: oklch(72% 0.13 255);
--blue-600: oklch(50% 0.16 255);
--gray-0: oklch(100% 0 0);
--gray-900: oklch(22% 0.01 80);
--space-3: 0.75rem;
/* semantic */
--color-accent: var(--blue-600);
--color-surface: var(--gray-0);
--color-text: var(--gray-900);
--space-inset: var(--space-3);
}
}
@layer components {
.button {
--button-bg: var(--color-accent); /* component token */
background: var(--button-bg);
}
}
Components use semantic or component tokens, never primitives. Then a theme only has to redefine the semantic tier, and every component follows.
Smaller sites can skip a tier: primitives plus semantic tokens are often enough.
Naming
- Numeric scales for primitives leave room to grow:
--gray-50through--gray-950, where the number roughly follows darkness. When a new shade is needed between 200 and 300,--gray-250fits without renaming anything. A palette inoklch()makes such steps easy to create (article 32). - Purpose, not appearance, for semantic tokens:
--color-danger, not--color-red. A dark theme or a rebrand changes the value, and the name stays true. - A consistent pattern helps people guess names: category, then role, then variant, then state, like
--color-text-mutedor--color-accent-hover. - Prefix tokens in shared libraries, as in
--acme-color-accent, to avoid collisions with the host page.
Scales for space and type
Spacing and font sizes work best as small, fixed scales, so the whole interface uses a few related values instead of arbitrary ones:
:root {
--space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem;
--space-4: 1rem; --space-5: 1.5rem; --space-6: 2rem; --space-7: 3rem;
/* fluid type: grows with the viewport between two limits (article 77) */
--text-sm: clamp(0.83rem, 0.8rem + 0.15vw, 0.9rem);
--text-md: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
--text-lg: clamp(1.25rem, 1.1rem + 0.6vw, 1.6rem);
--text-xl: clamp(1.6rem, 1.3rem + 1.4vw, 2.4rem);
}
Use rem so tokens respect the user's font size setting (article 24). Durations, easings, shadows, radii and z-index layers are tokens too: --duration-fast: 150ms, --z-dialog: 100.
Theming
A theme is a set of semantic token values applied to a subtree. The card component contains no theme logic at all; each theme only changes semantic tokens on an ancestor:
[data-theme="dark"] {
color-scheme: dark;
--color-surface: var(--gray-900);
--color-text: var(--gray-50);
--color-accent: var(--blue-300); /* lighter, to keep contrast on dark */
}
[data-theme="forest"] { --color-accent: var(--green-600); --radius: 2px; }
[data-density="compact"] { --space-inset: var(--space-2); --text-md: 0.8rem; }
- Themes can be combined: brand and density are independent attributes, so
data-theme="forest" data-density="compact"works. - Themes can be nested: a dark promo band inside a light page just sets
data-theme="dark"on the band, and custom properties inherit into it. - For light and dark only,
light-dark()(article 34) keeps both values in one declaration:--color-surface: light-dark(var(--gray-0), var(--gray-900));, driven bycolor-scheme. - Respect user preferences: default to
prefers-color-scheme, add a@media (prefers-contrast: more)block that strengthens borders and text tokens, and remember that in forced colors mode (article 35) the browser replaces your colors with system colors anyway.

These are settings readers change in their operating system, not on the page, so the playground below can't show them. Each column is the same HTML and component CSS; only a media query on :root redefines semantic tokens: eight for dark mode, and four (muted text, border color, border width and accent) for more contrast. Notice that the description text, which passes contrast checks at normal settings, turns fully black when the user asks for more contrast.
Component tokens as an API
A component can read an optional custom property with a fallback, so users of the component can adjust it without writing selectors that reach into it:
.button {
background: var(--button-bg, var(--color-accent));
padding: var(--button-padding, 0.45em 0.9em);
border-radius: var(--button-radius, var(--radius));
}
<button class="button" style="--button-bg: var(--color-danger)">Delete</button>
The fallback is used only while --button-bg is undefined, so the component works out of the box. This is also how web components expose styling hooks across shadow boundaries, since inherited custom properties pass into shadow trees (article 14).
The derived-token trap
A token computed from another token is resolved on the element where it's declared, and descendants inherit the finished value:

On the left, --accent-soft was computed once on :root, from blue. Changing --accent on .promo doesn't recompute it, so the promo keeps a blue tint. There are three fixes:
- Redefine the derived token wherever the source changes, in the theme rule:
[data-theme] { --accent-soft: color-mix(…); }. - Declare derived tokens on every element,
* { --accent-soft: … }, as on the right. It works, but sets a property on every element. - Derive at the point of use, in the component:
background: color-mix(in oklab, var(--accent) 15%, white);.
Article 33 shows the same effect with relative colors.
Typed tokens with @property
Registering a token (article 28) gives it a type, an initial value and control over inheritance:
@property --color-accent {
syntax: "<color>";
inherits: true;
initial-value: #1d4ed8;
}
- An invalid value, like
--color-accent: 20px, falls back to the initial value instead of breaking everyvar()that uses it. - Typed tokens can be transitioned, so a theme switch can animate colors:
transition: --color-accent 300ms. inherits: falsesuits component tokens that shouldn't leak into nested components.
Registered properties are computed to their type, so they also make the derived-token trap easier to see in DevTools.
Tokens outside CSS
Design tools and multiple platforms (web, iOS, Android) often share one source of tokens. The Design Tokens Format Module, a JSON format from a W3C community group, reached its first stable version, 2025.10, in October 2025. It's a community report, not a W3C standard. A token there looks like this:
{
"color": {
"accent": { "$type": "color", "$value": "{blue.600}" }
}
}
Build tools such as Style Dictionary convert such files into custom properties, Sass variables or native platform code. The browser never reads the JSON. What reaches CSS is still custom properties, organized as described above.
@property --color-accent { syntax: "<color>"; inherits: true; initial-value: #1d4ed8; }
@property --color-surface { syntax: "<color>"; inherits: true; initial-value: white; }
:root {
/* primitives */
--blue-300: oklch(72% 0.13 255); --blue-600: oklch(50% 0.16 255);
--green-50: oklch(96% 0.03 150); --green-600: oklch(48% 0.11 150);
--red-600: oklch(52% 0.18 27); --red-300: oklch(72% 0.13 27);
--gray-0: oklch(100% 0 0); --gray-50: oklch(97% 0.004 80); --gray-200: oklch(88% 0.006 80);
--gray-600: oklch(48% 0.01 80); --gray-900: oklch(22% 0.01 80); --gray-950: oklch(16% 0.01 80);
--space-2: 0.5rem; --space-3: 0.75rem; --space-4: 1rem;
}
.app {
/* semantic tokens, light */
color-scheme: light;
--color-bg: var(--gray-50); --color-surface: var(--gray-0); --color-text: var(--gray-900);
--color-text-muted: var(--gray-600); --color-border: var(--gray-200);
--color-accent: var(--blue-600); --color-on-accent: var(--gray-0); --color-danger: var(--red-600);
--space-inset: var(--space-4); --radius: 10px; --text-md: 0.95rem;
transition: --color-accent 300ms, --color-surface 300ms;
}
.app[data-theme="dark"] {
color-scheme: dark;
--color-bg: var(--gray-950); --color-surface: var(--gray-900); --color-text: var(--gray-50);
--color-text-muted: oklch(75% 0.01 80); --color-border: oklch(35% 0.01 80);
--color-accent: var(--blue-300); --color-on-accent: var(--gray-950); --color-danger: var(--red-300);
}
.app[data-theme="forest"] { --color-accent: var(--green-600); --color-bg: var(--green-50); --radius: 2px; }
.app[data-density="compact"] { --space-inset: var(--space-2); --text-md: 0.8rem; --radius: 6px; }
@media (prefers-reduced-motion: reduce) { .app { transition: none; } }
/* components: semantic and component tokens only */
.app { background: var(--color-bg); padding: var(--space-3); border-radius: 8px; }
.card {
background: var(--color-surface); color: var(--color-text); font-size: var(--text-md);
border: 1px solid var(--color-border); border-radius: var(--radius);
padding: var(--space-inset); display: grid; gap: calc(var(--space-inset) * 0.6);
}
.card h3 { margin: 0; font-size: 1.15em; }
.card p { margin: 0; color: var(--color-text-muted); }
.row { display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: center; }
.card input { accent-color: var(--color-accent); }
.button {
background: var(--button-bg, var(--color-accent)); color: var(--button-fg, var(--color-on-accent));
border: 0; border-radius: calc(var(--radius) * 0.7); padding: 0.4em 0.8em; font: 600 1em system-ui, sans-serif;
}
.button--ghost { --button-bg: transparent; --button-fg: var(--color-accent); box-shadow: inset 0 0 0 1.5px currentColor; }
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.switch { display: flex; flex-wrap: wrap; gap: 0.2rem 0.7rem; border: 0; padding: 0; margin: 0 0 0.6rem; }
.switch legend { font-weight: 600; padding: 0; margin-block-end: 0.2rem; }
The script only sets attributes. Every visual change comes from semantic tokens, and because --color-accent and --color-surface are registered as colors, switching themes animates them. The Cancel button sets its own --button-bg component token without any extra selector.
What's current
- Custom properties,
@property,light-dark()andcolor-mix()work in all browsers, which covers everything a token system needs in CSS. - The Design Tokens Format Module 2025.10 is the first stable shared JSON format; tool support is growing.
Common questions
How many tokens should a system have? As few as cover real needs. A handful of semantic colors, a spacing scale of 6–8 steps and a type scale of 5–7 steps go a long way.
Should components use primitives directly?
No. If a card uses --gray-0, the dark theme can't change it without a card-specific rule.
Why didn't my derived token change in the dark theme?
It was computed on :root from the light value. Redefine it in the theme rule, or derive it where it's used.
Data attribute or class for themes?
Either works. Data attributes read well for exclusive values, like data-theme="dark", and keep themes apart from component classes.
Summary
- Tokens name design decisions; custom properties carry them in CSS.
- Primitives hold values, semantic tokens name purposes, component tokens configure one component.
- Themes redefine semantic tokens on a subtree; they combine and nest naturally.
var(--component-token, var(--semantic))gives components an override API with defaults.- Derived tokens resolve where they're declared; redefine or derive them at the point of use.
@propertyadds types, safe initial values and animatable theme switches.
Practice questions
Which tier does each token belong to?
--orange-500: oklch(70% 0.17 55);
--color-warning: var(--orange-500);
--alert-border: var(--color-warning);
Answer
Primitive, semantic and component, in that order.
Write a dark theme that only changes semantic tokens, for a page whose components use --color-surface, --color-text and --color-accent.
Answer
[data-theme="dark"] {
color-scheme: dark;
--color-surface: var(--gray-900);
--color-text: var(--gray-50);
--color-accent: var(--blue-300);
}
No component rule changes.
This hover color stays blue inside .promo. Why, and how do you fix it?
:root { --accent: blue; --accent-hover: color-mix(in oklab, var(--accent), black 20%); }
.promo { --accent: orange; }
.promo .button:hover { background: var(--accent-hover); }
Answer
--accent-hover was computed on :root from blue, and .promo inherits the finished value. Redefine it in .promo, or compute it where it's used:
.button:hover { background: color-mix(in oklab, var(--accent), black 20%); }
Let users of a .badge component change its color without writing selectors, with the accent color as the default.
Answer
.badge { background: var(--badge-bg, var(--color-accent)); }
Then <span class="badge" style="--badge-bg: var(--color-success)"> overrides it.
Previous: Organizing a Stylesheet With Layers: Reset, Theme, Layout, Components and Utilities
Next: Pattern Libraries, a CSS-First Workflow and Refactoring Safely

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.