Design Tokens with CSS Custom Properties
How to build a design token system with CSS custom properties: color scales, semantic tokens, spacing, typography, dark themes, and responsive scaling.
Overview
CSS custom properties (variables) let you define design tokens: named values for colors, spacing, typography, and every other reusable design decision. Unlike Sass or LESS variables, custom properties are live. They can change at runtime, respond to media queries, and cascade through the DOM like any other inherited value.
A preprocessor variable is frozen at build time; a custom property can flip when a data-theme attribute changes, when a media query matches, or when JavaScript writes a new value. Theming, responsive scaling, and component-level overrides come free with it — no JavaScript theming engine, no second stylesheet, no rebuild.
What follows is a complete token system in plain CSS: raw scales, semantic tokens, a dark theme, fluid typography, and component-scoped overrides you can paste straight into a project.
When to Use
- Building a design system with consistent spacing, colors, and typography
- Supporting two or more themes (light/dark, brand variants) on the same stylesheet
- Responsive scaling: adjusting spacing and font sizes at breakpoints without touching components
- Shipping a component library that must adopt the host application’s tokens
- Runtime theme switching without a page reload
When NOT to Use
- Legacy browser support: custom properties don’t work in IE11, so public sites targeting it need fallbacks
- Values that never change and never need theming — a hardcoded
z-indexis fine - Complex color math at runtime: reach for
color-mix()or compute colors in a build step instead
Solution
Basic token definitions
Start with raw scales (the palette), then semantic tokens (the meaning):
:root {
/* Color scale */
--color-primary-50: #eff6ff;
--color-primary-100: #dbeafe;
--color-primary-500: #3b82f6;
--color-primary-600: #2563eb;
--color-primary-700: #1d4ed8;
--color-primary-900: #1e3a8a;
/* Neutral scale */
--color-white: #ffffff;
--color-gray-100: #f3f4f6;
--color-gray-200: #e5e7eb;
--color-gray-500: #6b7280;
--color-gray-900: #111827;
--color-black: #000000;
/* Semantic tokens */
--color-bg: var(--color-white);
--color-text: var(--color-gray-900);
--color-text-muted: var(--color-gray-500);
--color-border: var(--color-gray-200);
--color-accent: var(--color-primary-500);
/* Spacing scale */
--space-1: 0.25rem;
--space-2: 0.5rem;
--space-3: 0.75rem;
--space-4: 1rem;
--space-6: 1.5rem;
--space-8: 2rem;
--space-12: 3rem;
--space-16: 4rem;
/* Typography */
--font-sans: "Inter", system-ui, sans-serif;
--font-mono: "JetBrains Mono", monospace;
--text-xs: 0.75rem;
--text-sm: 0.875rem;
--text-base: 1rem;
--text-lg: 1.125rem;
--text-xl: 1.25rem;
--text-2xl: 1.5rem;
--line-height-tight: 1.25;
--line-height-normal: 1.5;
--line-height-relaxed: 1.75;
/* Radius */
--radius-sm: 0.25rem;
--radius-md: 0.5rem;
--radius-lg: 0.75rem;
--radius-full: 9999px;
/* Shadows */
--shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
--shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
--shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.15);
}
Using tokens in components
Components consume semantic tokens, never raw palette values:
.card {
background: var(--color-bg);
color: var(--color-text);
border: 1px solid var(--color-border);
border-radius: var(--radius-md);
padding: var(--space-6);
box-shadow: var(--shadow-sm);
font-family: var(--font-sans);
font-size: var(--text-base);
line-height: var(--line-height-normal);
}
.card__title {
font-size: var(--text-xl);
font-weight: 600;
margin-bottom: var(--space-2);
}
.card__body {
color: var(--color-text-muted);
font-size: var(--text-sm);
}
Dark theme with semantic tokens
Because components only read semantic tokens, a dark theme is a re-mapping, not a rewrite:
:root {
--color-bg: var(--color-white);
--color-text: var(--color-gray-900);
--color-text-muted: var(--color-gray-500);
--color-border: var(--color-gray-200);
--color-surface: var(--color-white);
--color-surface-hover: var(--color-gray-100);
}
[data-theme="dark"] {
--color-bg: #0f172a;
--color-text: #e2e8f0;
--color-text-muted: #94a3b8;
--color-border: #1e293b;
--color-surface: #1e293b;
--color-surface-hover: #334155;
}
Toggle the theme by setting data-theme on the root element:
document.documentElement.setAttribute("data-theme", "dark");
For the full OS-level version of this pattern (including prefers-color-scheme and FOUC handling), see Dark Mode with prefers-color-scheme and CSS Variables.
Responsive spacing with media queries
Token values can change inside media queries, so the responsive logic lives in one place instead of every component:
:root {
--space-page: var(--space-4);
--text-heading: var(--text-xl);
}
@media (min-width: 768px) {
:root {
--space-page: var(--space-8);
--text-heading: var(--text-2xl);
}
}
@media (min-width: 1280px) {
:root {
--space-page: var(--space-12);
--text-heading: var(--text-2xl);
}
}
.page {
padding: var(--space-page);
}
.page__title {
font-size: var(--text-heading);
}
The same pattern works inside @container queries when the scaling should respond to the container instead of the viewport — see CSS Container Queries for Responsive Components.
Fluid typography with clamp
For smoother scaling, clamp() removes the breakpoints entirely:
:root {
--text-fluid-sm: clamp(0.875rem, 0.8rem + 0.25vw, 1rem);
--text-fluid-base: clamp(1rem, 0.9rem + 0.5vw, 1.125rem);
--text-fluid-lg: clamp(1.25rem, 1rem + 1vw, 1.5rem);
--text-fluid-xl: clamp(1.5rem, 1.2rem + 1.5vw, 2rem);
--text-fluid-2xl: clamp(2rem, 1.5rem + 2.5vw, 3rem);
}
h1 { font-size: var(--text-fluid-2xl); }
h2 { font-size: var(--text-fluid-xl); }
h3 { font-size: var(--text-fluid-lg); }
p { font-size: var(--text-fluid-base); }
Component-scoped tokens
Declare private tokens inside a component so variants override values, not declarations:
.button {
--button-bg: var(--color-primary-500);
--button-text: var(--color-white);
--button-padding-x: var(--space-4);
--button-padding-y: var(--space-2);
--button-radius: var(--radius-md);
background: var(--button-bg);
color: var(--button-text);
padding: var(--button-padding-y) var(--button-padding-x);
border-radius: var(--button-radius);
border: none;
cursor: pointer;
}
.button--secondary {
--button-bg: var(--color-gray-100);
--button-text: var(--color-gray-900);
}
.button--large {
--button-padding-x: var(--space-6);
--button-padding-y: var(--space-3);
}
.button--secondary changes two custom properties and nothing else — the background and color declarations are reused, so variants can’t drift out of sync.
Z-index scale
:root {
--z-base: 0;
--z-dropdown: 100;
--z-sticky: 200;
--z-overlay: 300;
--z-modal: 400;
--z-toast: 500;
}
.dropdown { z-index: var(--z-dropdown); }
.modal { z-index: var(--z-modal); }
.toast { z-index: var(--z-toast); }
Transition tokens
:root {
--transition-fast: 150ms ease-out;
--transition-base: 250ms ease-out;
--transition-slow: 400ms ease-out;
}
.button {
transition: background var(--transition-fast), transform var(--transition-fast);
}
.button:hover {
background: var(--color-primary-600);
transform: translateY(-1px);
}
Using tokens with Tailwind CSS v4
Tailwind v4’s @theme block defines tokens and generates utilities from them — but only when the names follow its namespaces:
@theme {
--color-brand-50: #eff6ff;
--color-brand-100: #dbeafe;
--color-brand-500: #3b82f6;
--color-brand-600: #2563eb;
--color-brand-900: #1e3a8a;
--spacing-1: 0.25rem;
--spacing-2: 0.5rem;
--spacing-4: 1rem;
--spacing-6: 1.5rem;
--radius-sm: 0.25rem;
--radius-md: 0.5rem;
--radius-lg: 0.75rem;
}
The prefix decides the utility family: --color-* produces bg-brand-500, --spacing-* produces p-6, --radius-* produces rounded-md. A custom name like --brand-blue generates nothing — it stays a plain custom property.
Explanation
Three layers make all of this work:
- Raw scale: the palette.
--color-primary-500: #3b82f6means nothing by itself. - Semantic tokens: intent.
--color-accent: var(--color-primary-500)says what the color is for. - Component tokens: local APIs like
--button-bg, scoped to the component that uses them.
Components read the lowest layer they need. Because var() resolves at computed time, a semantic token that points to a raw token picks up whichever value is in scope — so [data-theme="dark"] only has to redefine the semantic layer, and every component follows without a single selector change.
Two cascade details worth remembering:
- Custom properties inherit. A token set on
.cardflows into all of.card’s descendants, which is powerful for scoped themes and a common source of “why did my modal turn dark” bugs. var()accepts a fallback:color: var(--accent, #3b82f6)renders the hex value if--accentis undefined. Useful when a component might run outside your token file.
Decide where tokens live by scope, not habit: :root for global scales, attribute selectors like [data-theme] for themes, class rules like .button for component APIs.
One edge case that bites everyone once: an invalid at computed-value time token doesn’t fall back to a previous declaration. If --accent is set to an invalid value (or left empty), color: var(--accent) resolves to the property’s initial or inherited value — it doesn’t fall back to the color declared earlier in the same rule. The var() fallback only triggers when the property is unset, not when it’s invalid. Keep a --token: initial or a fallback value handy for components that might run without the theme file.
On naming, consistency beats cleverness. A --{category}-{name}-{scale} pattern (--color-primary-500, --space-4, --text-xl) reads predictably, sorts well in DevTools, and matches what Tailwind’s @theme namespaces expect if you migrate later. Prefix private tokens with the component name (--button-bg) so global and local APIs never collide.
For tokens that need type checking or smooth transitions, register them with @property. A registered property carries a syntax (<color>, <length>, <integer>), an initial-value, and an inherits flag: the browser can interpolate transition: background var(--transition-fast) when the token itself changes, and it rejects invalid values at parse time instead of letting them surface at computed-value time. @property is also the way to make a token non-inheriting, which matters inside Web Components, where a leaked --color-bg can surprise a shadow tree.
Variants
Reading tokens from JavaScript
function getToken(name) {
return getComputedStyle(document.documentElement)
.getPropertyValue(name)
.trim();
}
const primaryColor = getToken("--color-primary-500");
const spaceUnit = getToken("--space-4");
Theme switching with localStorage
const theme = localStorage.getItem("theme") || "light";
document.documentElement.setAttribute("data-theme", theme);
function toggleTheme() {
const current = document.documentElement.getAttribute("data-theme");
const next = current === "dark" ? "light" : "dark";
document.documentElement.setAttribute("data-theme", next);
localStorage.setItem("theme", next);
}
Brand-specific tokens
Multi-brand apps can scope palettes the same way themes do:
[data-brand="acme"] {
--color-primary-500: #e11d48;
--color-primary-600: #be123c;
}
[data-brand="globex"] {
--color-primary-500: #059669;
--color-primary-600: #047857;
}
Best Practices
- Use semantic token names (
--color-bg,--color-text) instead of raw color names (--blue-500) — semantic tokens are what make theming a re-mapping instead of a rewrite - Keep raw scales and semantic tokens separate: raw scales are the palette, semantic tokens are the meaning
- Build the spacing scale on a base unit (0.25rem) and multiply, so values stay consistent
- Use
clamp()for fluid typography that adapts smoothly across screen sizes - Group tokens by category (colors, spacing, typography, shadows) with comments
- Scope private tokens to components (
--button-bg) so variants override values, not declarations - Test both themes while building: retrofitting dark mode onto a light-only UI is where token systems get expensive
Common Mistakes
- Using raw color values in components:
color: #3b82f6instead ofcolor: var(--color-accent)makes theming impossible. - Hardcoding spacing:
padding: 14pxinstead ofpadding: var(--space-3)produces inconsistent spacing across the app. - Skipping the semantic layer: mapping components straight to raw scale tokens means a theme change touches every component.
- Overriding tokens at component level without scoping: setting
--color-bginside a.cardrule leaks into every descendant. - Not testing the dark theme: token combinations that pass contrast in light mode often fail in dark mode. Check the pairs against the WCAG accessibility criteria before shipping a theme.
See Also
- MDN — Using CSS custom properties
- MDN — var() function
- W3C Design Tokens Community Group
- Companion resources — a layered
tokens.cssplus a demo page with runtime theme toggle
Frequently Asked Questions
What's the difference between raw tokens and semantic tokens?
Raw tokens are the palette (--color-blue-500: #3b82f6). Semantic tokens assign meaning (--color-accent: var(--color-blue-500)). Components consume semantic tokens, so changing the accent means updating one token, not every component.
Can I use custom properties with preprocessors?
Yes. Sass/LESS variables resolve at compile time; custom properties resolve at runtime. Teams often keep Sass for static values and use custom properties for anything themeable.
How do I support IE11?
You can't — custom properties don't exist in IE11. Guard them with @supports and declare a fallback first:
.card { background: #ffffff; }
@supports (--css: variables) {
.card { background: var(--color-bg); }
}
How do custom properties cascade?
Like regular inherited properties. A token on :root applies everywhere; a token on .dark-theme overrides it for descendants of .dark-theme. Nearest declaration wins.
Should I use custom properties or Tailwind's @theme?
@theme in Tailwind v4 is custom properties under the hood. If you're on Tailwind, use @theme so your tokens generate utilities. Writing plain CSS? Use custom properties directly.
Do design tokens have a standard format?
A W3C community group is working on a Design Tokens format for sharing tokens between design tools and code. CSS custom properties are the most common way to consume those tokens on the web.
Related Resources
CSS Container Queries for Responsive Components
How to use CSS container queries so components adapt to the space they actually get inside a sidebar, grid cell, or card — not just the viewport width.
RecipeDark Mode with prefers-color-scheme and CSS Variables
How to implement dark mode using CSS prefers-color-scheme media query, CSS custom properties, and manual toggle with localStorage persistence.
RecipeExhaustive Type Checking with Discriminated Unions
How to use TypeScript discriminated unions for exhaustive type checking, ensuring all cases are handled at compile time with never type assertions.
GuideWCAG 2.2 Accessibility: A Developer Guide
A practical guide to WCAG 2.2 compliance: the four POUR principles, ARIA, keyboard navigation, accessible forms and modals, tested with real code examples.
GuideComplete Guide to CSS Grid and Flexbox
Master CSS layout: Grid vs Flexbox, template areas, subgrid, container queries, logical properties and responsive patterns without media queries.