CSS Custom Properties Design Tokens

Define design tokens using CSS custom properties with fallback values and runtime theme switching capability.

CssConfig Management

Code

:root {
  --color-primary: #4f46e5;
  --color-bg: #ffffff;
  --spacing-unit: 8px;
}

[data-theme="dark"] {
  --color-primary: #818cf8;
  --color-bg: #1a1a2e;
}

.card {
  background: var(--color-bg);
  color: var(--color-primary);
  padding: calc(var(--spacing-unit) * 3);
}

CSS Custom Properties Design Tokens

Define design tokens using CSS custom properties with fallback values and runtime theme switching capability.

Code

:root {
  --color-primary: #4f46e5;
  --color-bg: #ffffff;
  --spacing-unit: 8px;
}

[data-theme="dark"] {
  --color-primary: #818cf8;
  --color-bg: #1a1a2e;
}

.card {
  background: var(--color-bg);
  color: var(--color-primary);
  padding: calc(var(--spacing-unit) * 3);
}

Explanation

Custom properties store reusable values that descendants read with var(), enabling runtime theme switching by overriding a token on a root selector. Expected behavior: var() falls back to the supplied default when the token is undefined. Constraint: custom property values are not validated at parse time, so a typo surfaces only when the property is finally used.

Interactive Tools for This Topic