Skip to content
Web Design & Development

CSS Custom Properties: The Decisions That Matter

Variables defined in CSS, such as --brand-color, that can be reused across a stylesheet and changed at runtime, often used to implement design tokens and themes.

MediaScaleUp Editorial Team 2 min read 8 views
CSS Custom Properties: The Decisions That Matter

The key figures

Syntax
declared with two hyphens and read with var()
Inheritance
custom properties inherit like other CSS properties
Fallbacks
var() accepts a fallback value
@property
registers a custom property with a type, initial value and inheritance

Why this is worth getting right

They let a site change colors, spacing and themes in one place, and unlike preprocessor variables they can be updated live by CSS or JavaScript.

Do this, not that

Do

  • Define design tokens as custom properties at the root
  • Override them in scopes for themes and components
  • Provide fallbacks for critical values
  • Name properties by purpose rather than appearance
  • Document the available properties in the design system

Don’t

  • Hundreds of one-off properties with unclear names
  • Using custom properties for values that never change or repeat
  • Relying on JavaScript to set values CSS could handle
  • Circular references between properties

When to bring in help

Our advice Bring in help when implementing theming or dark mode, or when a stylesheet has grown into repeated hard-coded values.

Where this comes from

The figures and practices above come from the sources listed.

Working on something like this?

We take on Web Design & Development work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.

Where to go next

Spotted something wrong? Report an error on this page. We correct on the page and say what changed.

All services

The work behind this article, and what it costs.

MediaScaleUp Editorial Team

Articles here are researched, written and checked by the MediaScaleUp studio team before they are published, and dated when they are reviewed.

Keep reading

More in Web Design & Development