The two-layer (or three-layer) model
A CSS color system is usually organized in two or three layers:
- Palette tokens — the raw colors. Every color in your system gets a token here.
- Semantic tokens — named by function, pointing to palette tokens. This is what components use.
- Component tokens (optional) — specific to a single component. Used sparingly, for cases where a component needs its own variants.
Most projects do well with just two layers. The third layer is worth adding only when a component has complex needs that don't map cleanly to semantic tokens.
Layer 1: Palette tokens
Palette tokens are named by their color family and step:
:root {
/* Brand palette */
--color-primary-50: #F0F7F5;
--color-primary-100: #DCE9E3;
--color-primary-200: #B4D2C5;
--color-primary-300: #8BAF9E;
--color-primary-400: #4E9B8A;
--color-primary-500: #267D70;
--color-primary-600: #1E6359;
--color-primary-700: #174C45;
--color-primary-800: #103630;
--color-primary-900: #08221E;
/* Neutrals */
--color-neutral-50: #F9FAFB;
--color-neutral-100: #F1F3F2;
--color-neutral-200: #E5E7EB;
--color-neutral-300: #D1D5DB;
--color-neutral-400: #9CA3AF;
--color-neutral-500: #6B7280;
--color-neutral-600: #4B5563;
--color-neutral-700: #374151;
--color-neutral-800: #1F2937;
--color-neutral-900: #111827;
/* Semantic states */
--color-success-500: #16A34A;
--color-warning-500: #D97706;
--color-error-500: #DC2626;
--color-info-500: #2563EB;
}
Naming rules:
- Family name + step (50–900).
- Never name by usage (e.g. "primary-button-color").
- Never name by hex (e.g. "teal-500" — but if you must, be consistent).
- Every color gets a token. No raw hex values anywhere else in the codebase.
Layer 2: Semantic tokens
Semantic tokens are what components actually use. They're named by their role, not their color:
:root {
/* Surfaces */
--surface-page: var(--color-neutral-50);
--surface-card: #FFFFFF;
--surface-elevated: var(--color-neutral-100);
--surface-hover: var(--color-neutral-100);
--surface-active: var(--color-neutral-200);
/* Text */
--text-primary: var(--color-neutral-900);
--text-secondary: var(--color-neutral-600);
--text-tertiary: var(--color-neutral-500);
--text-inverse: #FFFFFF;
--text-on-primary: #FFFFFF;
/* Borders */
--border-default: var(--color-neutral-300);
--border-strong: var(--color-neutral-400);
--border-focus: var(--color-primary-500);
/* Interactive */
--interactive-primary: var(--color-primary-500);
--interactive-primary-hover: var(--color-primary-600);
--interactive-primary-active: var(--color-primary-700);
--interactive-primary-disabled: var(--color-primary-200);
--interactive-secondary: var(--color-neutral-200);
/* Status */
--status-success: var(--color-success-500);
--status-warning: var(--color-warning-500);
--status-error: var(--color-error-500);
--status-info: var(--color-info-500);
}
Components only use these tokens:
.button-primary {
background: var(--interactive-primary);
color: var(--text-on-primary);
border: 1px solid var(--interactive-primary);
}
.button-primary:hover {
background: var(--interactive-primary-hover);
border-color: var(--interactive-primary-hover);
}
.input-field {
background: var(--surface-card);
border: 1px solid var(--border-default);
color: var(--text-primary);
}
.input-field:focus {
border-color: var(--border-focus);
box-shadow: 0 0 0 3px rgba(38, 125, 112, 0.15);
}
No component ever references var(--color-primary-500) directly. That would break the abstraction.
Theme switching with data attributes
Dark mode is implemented by overriding semantic tokens under a different attribute selector:
[data-theme="dark"] {
--surface-page: #0D0D0D;
--surface-card: #1A1A1A;
--surface-elevated: #262626;
--surface-hover: #333333;
--surface-active: #404040;
--text-primary: #F5F5F5;
--text-secondary: #B8B8B8;
--text-tertiary: #8A8A8A;
--border-default: #404040;
--border-strong: #606060;
--interactive-primary: var(--color-primary-300);
--interactive-primary-hover: var(--color-primary-200);
--interactive-primary-active: var(--color-primary-100);
}
Every component automatically adapts because it uses semantic tokens — no component code changes for dark mode.
System preference detection
Most dark modes should default to the OS preference:
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--surface-page: #0D0D0D;
/* ... dark token overrides ... */
}
}
And if a user explicitly picks light mode, the [data-theme="light"] attribute should override the media query. The :not([data-theme="light"]) selector handles this — the media query only applies when the user hasn't forced light mode.
Color function variants
Modern CSS supports several color functions. Understanding the trade-offs:
| Format | Best for | Avoid when |
|---|---|---|
| HEX | Static colors, tokens | You need alpha or dynamic manipulation |
rgb() | Alpha via rgba() | You need hue-relative adjustments |
hsl() | Dynamic lightness/saturation | You need exact perceptual matching |
oklch() | Perceptual, gamut-aware | You need IE/older browser support |
color-mix() | Computed variants | You need static output for older browsers |
Using color-mix() for computed variants
A very useful modern pattern — compute hover states and tints from a single source color:
:root {
--interactive-primary: var(--color-primary-500);
--interactive-primary-hover: color-mix(in srgb, var(--interactive-primary) 85%, black);
--interactive-primary-tint: color-mix(in srgb, var(--interactive-primary) 15%, white);
}
This means changing the primary color updates every derived color automatically. color-mix() is supported in all modern browsers (Chrome 111+, Firefox 113+, Safari 16.2+).
Component tokens (when to add layer 3)
Add component-level tokens when:
- A component has 5+ variants, each needing its own color.
- The component is being extracted into a library and needs to be customizable.
- The component has unique color relationships that don't fit the semantic model (e.g. a multi-series chart component).
Example:
.chart-series {
--chart-1: #003F5C;
--chart-2: #2F4B7C;
--chart-3: #665191;
--chart-4: #A05195;
--chart-5: #D45087;
--chart-6: #F95D6A;
--chart-7: #FF7C43;
--chart-8: #FFA600;
}
Chart colors are their own mini-system because they have different requirements (accessibility across CVD, distinguishable at small sizes) — they don't map to the general semantic tokens.
Documentation matters
A token system without documentation is a token system that will drift. Document:
- Every token and what it's for.
- Every token's contrast ratio.
- Which tokens can be layered (e.g. "text-secondary is only valid on surface-page and surface-card, not on surface-hover").
- Which tokens exist only for a specific component.
Testing the system
- Grep the codebase for raw hex values — there shouldn't be any outside the palette layer.
- Grep for palette tokens used directly in components — that's a layering violation.
- Test both themes with a contrast checker for every text/background pair.
- Test theme switching at runtime — no flash of unstyled content, no layout shift.