← Back to HTML & CSS, page 6

html-css-0117 · Hard senior

How do you structure design tokens with CSS custom properties so that a theme change is one edit?

Use primitive tokens for raw values, semantic tokens for roles such as action and surface, and optional component tokens for local hooks. Components read semantic tokens, so a theme redefines only the semantic layer, in one place.

Full answer

Design tokens are named values for colour, spacing, type and so on. In CSS they are custom properties. A good setup has three layers, and each layer refers to the layer before it.

  1. Primitive tokens are the raw palette: --blue-600: #0a58ca, --gray-100: #f2f2f2, --space-2: 8px. Names describe the value, not its use. Components never use them directly.
  2. Semantic tokens say what a value is for: --color-action: var(--blue-600), --color-surface: var(--gray-100). They are the layer that changes with a theme.
  3. Component tokens are optional local hooks: --button-bg: var(--color-action). They give one component its own adjustable value.

To make a dark theme or a second brand, redefine only the semantic tokens inside a selector such as [data-theme="dark"]. All components follow, because they read semantic tokens. Nothing in component CSS changes.

Habits that help:

  • Name by role, such as --color-text-muted, not by look, such as --light-grey.
  • Keep a short scale for spacing and type, and use only the scale.
  • Put tokens on :root and the theme override on an attribute, so a theme can also apply to a part of a page.
  • Check contrast for each pair in each theme.
  • Generate tokens from one source file if several platforms use them.

The card is light and the button is blue. After setting data-theme="dark" on the page, the card is dark, the button is a lighter blue, and no component rule changed.

A palette of primitives is not a theme. The theme lives in the semantic layer.

html
<style>
  :root { --blue-600: #0a58ca; --blue-300: #7db0ff; --gray-100: #f2f2f2; --gray-900: #111; --space-2: 8px; }
  :root { --color-action: var(--blue-600); --color-surface: var(--gray-100); --color-text: var(--gray-900); }
  [data-theme="dark"] { --color-action: var(--blue-300); --color-surface: var(--gray-900); --color-text: var(--gray-100); }
  .card { background: var(--color-surface); color: var(--color-text); padding: var(--space-2); }
  .card button { --button-bg: var(--color-action); background: var(--button-bg); color: var(--color-surface); border: 0; padding: var(--space-2); }
</style>
<div class="card" id="c"><button id="b" type="button">Enrol</button></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Primitive tokens hold raw values.
  • Semantic tokens name roles and change per theme.
  • Component tokens are local hooks.
  • Components should not read primitives.
  • Check contrast in every theme.

Common mistakes

  • Using primitive colours directly in components, so a theme needs many edits.
  • Naming tokens by appearance, such as --dark-blue, which stops making sense in a new theme.
  • Creating a new token for every one-off value.

Follow-ups

  • Where do you put the dark theme override?
  • Why name tokens by role instead of by look?

Tips

  • Describe the three layers and what changes in a theme.
  • Say that components read only semantic tokens.