Design Tokens and Theming

Encode colours, spacing, typography and radii as named tokens so design and code share one source of truth and themes are easy.

Intermediate · about 3 hours · difficulty 2/5

Learn first: Custom Properties and Modern CSS Features, Tailwind CSS

A design system is the shared language of a product: tokens, components, patterns and guidelines. Its foundation is design tokens, named values for every visual decision, used by designers in Figma and by developers in code.

Token layers

  • Primitive (base) tokens: the raw palette and scales, such as blue-600, space-4, radius-md, font-size-lg.
  • Semantic tokens: meaning, not appearance, such as color-bg-surface, color-text-muted, color-border-danger. Components use these.
  • Component tokens (optional): button-primary-bg for fine control.

Themes (light, dark, high contrast, brand variants) simply remap semantic tokens to different primitives. Components never change.

In code

  • Implement tokens as CSS custom properties on :root and theme selectors ([data-theme="dark"]), consumed by plain CSS, CSS Modules or a Tailwind theme.
  • Respect prefers-color-scheme by default, let users override, persist the choice and set it before first paint to avoid a flash of the wrong theme.
  • Tools like Style Dictionary or Figma variables exports can generate CSS, TypeScript and mobile formats from a single JSON source.
  • Check every theme for contrast and visible focus rings.

Beyond tokens

Consistent spacing rhythm, a type scale, elevation (shadows) and motion durations are tokens too. Document when to use each, not just what exists.

Pitfalls

Using primitive tokens directly in components (dark mode then needs edits everywhere), inventing one-off values, and themes that were never tested for contrast.

Good enough to move on

You can define primitive and semantic tokens, build light and dark themes with no flash on load, and explain why components should reference semantic tokens.

Key concepts

  • What a design system includes
  • Primitive vs semantic tokens
  • Tokens as CSS custom properties
  • Light, dark and high-contrast themes
  • Avoiding theme flash on load
  • Style Dictionary and Figma variables
  • Type scale and spacing rhythm

Mini project

Create a token set (primitives plus semantic tokens) for a fictional brand, implement light, dark and high-contrast themes with a persisted toggle and no flash on load, and verify contrast for each.

Check yourself

Why should a Button use color-bg-accent rather than blue-600 directly?

Free resources