Implementing Design Tokens for Consistent UI Theming

By : manish

Language: css

Date Published: 4 days, 3 hours ago

Design tokens are the single source of truth for visual decisions—colors, spacing, typography, shadows, and more. By storing these values in a centralized place (commonly CSS custom properties or a JSON file), you ensure consistency across components, pages, and even platforms (web, mobile, desktop). Changes to a token propagate automatically, reducing the risk of visual drift and making theme switches (like light/dark or brand-specific palettes) straightforward.

In a frontend codebase, start by defining tokens in the :root or a dedicated .css file using --token-name. Reference them throughout your stylesheets with var(--token-name). For dynamic theming, toggle a class on <html> or <body> to swap token values, or load a different token file via JavaScript. Pair this approach with a style‑dictionary or token‑transform tool if you need to export tokens to multiple formats (e.g., for iOS/Android). This method keeps CSS clean, improves maintainability, and scales well as your design system grows.


 

Style : Dark Mode : Line Number: Download Font Size:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* tokens.css – define all design tokens here */
:root {
  /* Color palette */
  --color-primary: #2563eb;
  --color-primary-dark: #1d4ed8;
  --color-background: #ffffff;
  --color-background-dark: #111827;
  --color-text: #111827;
  --color-text-dark: #f9fafb;

  /* Typography */
  --font-sans: "Inter", system-ui, sans-serif;
  --font-size-base: 1rem;          /* 16px */
  --font-size-lg: 1.125rem;        /* 18px */
  --font-size-xl: 1.25rem;         /* 20px */
  --line-height-base: 1.5;
  --line-height-tight: 1.25;

  /* Spacing (8px grid) */
  --space-0: 0;
  --space-1: 0.25rem;   /* 4px */
  --space-2: 0.5rem;    /* 8px */
  --space-3: 0.75rem;   /* 12px */
  --space-4: 1rem;      /* 16px */
  --space-6: 1.5rem;    /* 24px */
  --space-8: 2rem;      /* 32px */

  /* Border radius */
  --radius-sm: 0.25rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;

  /* Shadow */
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.1);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1),
               0 2px 4px -2px rgba(0,0,0,0.1);
}

/* Optional dark theme overrides */
.dark {
  --color-background: #111827;
  --color-background-dark: #0f172a;
  --color-text: #f9fafb;
  --color-text-dark: #ffffff;
}

/* Example usage in components */
.button {
  background: var(--color-primary);
  color: var(--color-text-dark);
  border: none;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
  font-size: var(--font-size-base);
  line-height: var(--line-height-base);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}
.button:hover {
  background: var(--color-primary-dark);
}

/* HTML example */
/*
<button class="button">Click me</button>
*/
design-tokens theming css-variables frontend scalability
Login to see the comments