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);
}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
Properties File ParserParse Java .properties files with comments, continuations, separators, and escapes, then emit clean JSON.Aspect Ratio CalculatorSimplify a width-to-height ratio, show its decimal value, and derive a matching height for a new width.Border Radius PreviewPreview four-corner border-radius values on a live box and copy the generated shorthand border-radius CSS declaration.Box Shadow GeneratorBuild CSS box-shadow values with offset, blur, spread, color, opacity, and inset controls, with a live preview of the result.
Report a problem with this exampleOpens a pre-filled issue on GitHub.