Skip to the content

Lesson 25 of 296

CSS variables

What you will learn

  • What CSS variables are and how to write them
  • Fallback values
  • Where variables can be used (scope)
  • Building a design system with variables
  • Dark mode with variables
  • Changing variables with JavaScript
On this page

What are CSS variables?

A CSS variable (officially a custom property) stores a value so you can reuse it everywhere. Change it in one place and every use updates.

:root {
  --brand: #1f5fbf;
  --radius: 8px;
  --space: 1rem;
}

.button {
  background: var(--brand);
  border-radius: var(--radius);
  padding: var(--space);
}
  • A variable name starts with two hyphens: --brand.
  • You read it with var(): var(--brand).
  • :root means the <html> element. Variables set there are available everywhere.

Fallback values

color: var(--text-color, #222);

If --text-color is not set, the browser uses #222.

Scope and inheritance

Variables follow the same inheritance rules as other properties. A variable set on an element applies to that element and all its children, and can be changed for one part of the page.

:root { --accent: blue; }
.danger-zone { --accent: red; }

.button { background: var(--accent); }

Buttons are blue everywhere, but red inside .danger-zone. You did not need to write a new button style.

A simple design system

Big projects keep all design choices in variables, often called design tokens:

:root {
  --color-text: #1b2430;
  --color-muted: #5b6675;
  --color-bg: #ffffff;
  --color-primary: #1f5fbf;

  --font-body: "Inter", system-ui, sans-serif;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 1rem;
  --space-4: 2rem;

  --radius-sm: 4px;
  --radius-md: 8px;

  --shadow-card: 0 2px 8px rgb(0 0 0 / 0.08);
}

Using only these values keeps the whole site consistent. A designer can change the brand colour once and the whole site follows.

Dark mode with variables

:root {
  --color-bg: #ffffff;
  --color-text: #1b2430;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #12161c;
    --color-text: #e8ecf1;
  }
}

body {
  background: var(--color-bg);
  color: var(--color-text);
}

The components never change. Only the variables do.

To let users switch manually, use a class or attribute instead:

[data-theme="dark"] {
  --color-bg: #12161c;
  --color-text: #e8ecf1;
}

JavaScript then sets data-theme="dark" on the <html> element.

Changing variables with JavaScript

document.documentElement.style.setProperty("--brand", "#e63946");

Every element using var(--brand) updates straight away.

CSS variables vs Sass variables

Sass is a tool that adds extra features to CSS and turns them into normal CSS before the site goes live. Sass has its own variables, written like $brand.

CSS variablesSass variables
When the value is decidedWhen the page runs, in the browserWhen the CSS is built, before the page runs
Can change on the page (themes, JavaScript)YesNo
Follow inheritance and scopeYesNo
Need a build toolNoYes

This difference is a common interview question.

Common mistakes

  • Forgetting the two hyphens or the var().
  • Spelling a variable name wrong. There is no error; the property is simply ignored or uses the fallback.
  • Using variables in media query conditions: @media (min-width: var(--md)) does not work.
  • Creating hundreds of variables with no system, which is as messy as having none.

How to say it in an interview

"CSS custom properties store reusable values, declared with two hyphens and read with var, with an optional fallback. Unlike Sass variables, they exist at runtime, follow the cascade and inheritance, and can be changed by JavaScript or scoped to part of the page. I use them for design tokens and theming, for example swapping a set of colour variables for dark mode."

Key words

WordMeaning
Custom propertyThe official name of a CSS variable
var()The function that reads a variable
:rootThe top element of the page, <html>
Design tokenA named design value such as a colour or spacing
ThemingChanging the whole look by changing variables
SassA tool that adds features to CSS before it runs

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.