← Back to CSS
15

CSS Variables: Tokens, Themes and Runtime Updates

Use CSS custom properties as a living design system with reusable tokens, cleaner theming and runtime styling without duplicated rules.

📘 Theory

Declaration, inheritance and scope

The basics matter because custom properties are part of the cascade, not a separate system.

A custom property is declared with a name like `--space-md` and consumed with `var(--space-md)`. Because it participates in the cascade, it can inherit and be redefined by context.

Not everything belongs in `:root`. Put global tokens there, but keep component-only variables close to the component when that improves clarity.

  • Global: `:root { --space-md: 1rem; }`
  • Component: `.card { --card-padding: 1rem; }`
  • Context: `[data-density="compact"] { --space-md: .75rem; }`

Base tokens and semantic tokens

This is the pattern that scales better in real products.

1

Base tokens represent raw values such as palette entries, spacing or radii. Semantic tokens represent UI intent: surface, text, accent, border, danger and so on.

2

Components should consume semantic tokens whenever possible. That way a rebrand or theme change happens at the token layer, not one button at a time.

Fallbacks and runtime updates

Custom properties become even more useful when you need resilience and live UI changes.

1

Fallbacks like `var(--color-title, #111827)` protect the component if a token is missing during a migration or partial refactor.

2

You can also update variables from JavaScript in real time. That is powerful for theme pickers, accessibility settings or product customization panels.

🧭 Key visuals

CSS variable scope

It clarifies how global tokens and local overrides can coexist cleanly.

Relationship between global CSS variables in root and local component variables.

Token flow with CSS variables

It helps reinforce the idea of centralizing decisions and propagating brand changes consistently.

Flow from root-level token definition to component consumption with var().

🧪 Learn by doing

Example Demo: design tokens applied to a panel See how a component changes when the values come from reusable tokens instead of hardcoded styles.
Example Interactive demo: CSS variables and HSL at runtime Adjust hue, saturation and lightness to see how one root-level token can propagate through the entire interface.

🏁 Challenges

Challenge Challenge: dashboard theme tokens Define theme tokens in `:root` and override them for a dark mode state.

🧰 Resources

Test

Check your knowledge with a test about CSS.

Test for CSS
CodePen: CSS variables in action
Open in CodePen

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com