Learn CSS Variables

A CSS variable is a value saved under a name, so you can use it in many places. Save the value once in :root (the selector for the whole page) with --name…

A shop's website uses its blue in thirty places: headings, buttons, links, lines. Then the shop's owner asks you to change it to green. Do you have to find and replace them one by one, and hope you don't miss any?

There is an easier way: a CSS variable (its official name is custom property). You save a value once under a name, then use that name in many places. When you change the saved value, every place that uses the name changes too. It works like a contact on your phone: the phone number is saved once, and you call by name. In CSS, the contact's name is the variable's name, and the number is the saved value.

Make a variable, then use it

This example saves two values in :root, then uses them in the heading and the button:

<h1>Title</h1>
<button class="button">Sign up</button>

<style>
:root {
  --primary: blue;
  --radius: 8px;
}

h1 {
  color: var(--primary);
}

.button {
  background: var(--primary);
  border-radius: var(--radius);
}
</style>

The result: a blue heading, and a blue button with 8px rounded corners. Now change the value of --primary to green in :root. The heading and the button both turn green right away, and you never touch the h1 and .button rules.

So there are two steps: save the value in :root with --name: value;, then use it with var(--name). If you type the value again somewhere else, like color: blue;, that place doesn't use the variable, so it won't change with it later.

A fallback value

var() can take a second value, after a comma:

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

That second value is a fallback value. If the variable --accent was never made, the browser uses gray. If --accent exists, the browser uses its value.

A variable for one part of the page

A variable can also be made in another selector, not only in :root:

.promo {
  --primary: orange;
}

Inside the .promo element, var(--primary) gives orange. Outside .promo, the value stays blue from :root. When the same variable name is made in two places, an element uses the value from the nearest element that wraps it.

⚠️ The variable isn't being read?

💡 JavaScript can change them. JavaScript can change a CSS variable's value while the page is open, for example with document.documentElement.style.setProperty('--primary', 'red'). That is an easy way to build a dark mode switch.

🔬 Try it yourself

Change a variable's value below and watch everything that uses it change too.

TL;DR: A CSS variable is a value saved under a name, so you can use it in many places. Save the value once in :root (the selector for the whole page) with --name: value; (the name starts with two hyphens), then use it in any rule with var(--name). When you change the value in :root, everything that uses it changes too. var(--name, fallback) uses the fallback value when the variable was never made. A variable made in another selector, like .promo, only applies inside that element.

⚡ Save it once

Write:

:root {
  --primary: blue;
}

Meaning: --primary: blue; makes a CSS variable: the value blue is saved under the name --primary, so you can use it in many places. A variable's name always starts with two hyphens --. The rest is written like a normal declaration: name, colon, value, semicolon. :root is <html>, which wraps the whole page, so a variable in :root can be used by every rule.

⚡ Use it anywhere

Write:

h1 {
  color: var(--primary);
}
Guess first: the value of --primary is changed to green. Does the h1 rule need to change too?

No. Everything that uses var(--primary) turns green right away.

Meaning: var(--primary) reads the value saved in the variable --primary. Write it where a value normally goes. color: --primary; without var() doesn't work.

⚡ A fallback value

Write:

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

Meaning: The second value inside var() is a fallback value. If the variable --accent was never made, the browser uses gray. If --accent exists, the browser uses its value.

What you will learn