TetSharing
CSS tutorial · 12 chapters
  1. Get started
  2. Selectors
  3. Cascade and specificity
  4. Colors
  5. Units and sizes
  6. Box model
  7. Text and fonts
  8. Flexbox
  9. Grid layout
  10. Media queries
  11. Custom properties
  12. Transitions

Final CSS quiz
Cheat sheet: css selectors

Chapter 11 of 12 · By Ethan Cole · Updated November 21, 2026

CSS Variables: Custom Properties and var() Explained

Your brand color appears in buttons, links, borders and headings. Without variables you repeat #0e7490 twenty times, and a redesign becomes a search-and-replace job. Custom properties, usually called CSS variables, let you name a value once and reuse it.

Defining and using

A custom property is any property whose name starts with two hyphens. You read it with var():

:root {
  --brand: #0e7490;
  --space: 1rem;
}
.button {
  background: var(--brand);
  padding: calc(var(--space) / 2) var(--space);
}

:root selects the <html> element, so properties defined there are available everywhere. Names are case-sensitive: --Brand and --brand are two different variables.

They cascade and inherit

Unlike variables in preprocessors such as Sass, custom properties are real CSS properties. They follow the cascade and are inherited by descendants. That means you can override a variable for one part of the page, and everything inside that part picks up the new value:

.card.danger { --brand: #be123c; }

Every var(--brand) inside a .danger card now resolves to red, with no other rule changed. This is how themes and component variants are built. The example defines a brand color on :root, overrides it in one card and prints the color each button ended up with.

HTML · editable

Expected page text

Save
Delete
Save button: rgb(14, 116, 144)
Delete button: rgb(190, 18, 60)
--space on :root: 12px

Fallback values

var(--accent, teal) uses teal if --accent is not defined. The fallback can itself be another var(). Fallbacks are handy in reusable components that may land on pages that never set the variable.

Changing variables from JavaScript

Because they are live properties, a script can update one and the whole page responds: document.documentElement.style.setProperty('--brand', '#7c3aed'). Dark mode switches often work this way, toggling a class that redefines a handful of color variables.

Common mistakes

  • Forgetting the double hyphen: -brand or brand is not a custom property.
  • Expecting var() to work in selectors or media query conditions; it only works in property values.
  • Mixing up case: var(--Brand) does not read --brand.

Summary

  • Define with --name: value;, read with var(--name).
  • They inherit, so redefining one on a container restyles everything inside.
  • var(--name, fallback) covers undefined variables.

Quiz: Custom properties

1. Which line defines a custom property?

Custom property names start with two hyphens.

2. A .card sets --brand: red and :root sets --brand: blue. What does var(--brand) give for a button inside the card?

The nearest ancestor that defines the property wins, because custom properties inherit.

3. What does var(--gap, 8px) do when --gap is not defined?

The second argument is the fallback value.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.