Cheat sheet #10

CSS custom properties

A cheat sheet for web design students. Store a value once, give it a meaningful name, and then reuse it everywhere. Learn how custom properties keep a design consistent, make changes quick and easy, and help you follow the “don’t repeat yourself” principle.

The short version

Declare a value once, with a name that starts with two hyphens, then use it with var().

:root { --color-primary: #60ab1e; } a { color: var(--color-primary); }
On this cheat sheet

What are custom properties?

Your own named values

A custom property is a value that you name and store in your CSS so that you can reuse it in many places. Custom properties are often called CSS variables. You choose the name, which must begin with two hyphens, and CSS keeps the value for you.

They’re supported in all current browsers, so you can use them freely.

The “don’t repeat yourself” (DRY) principle

DRY is a rule of good programming practice. It says that every piece of information should be written in one place only. If you repeat it, you create more work for yourself, and more chances to make mistakes. A brand colour that’s used in 40 places is a good example.

Define once, reuse:root { --color-primary: #60ab1e; } a { color: var(--color-primary); } h1 { color: var(--color-primary); } .button { background: var(--color-primary); } blockquote { border-left: 4px solid var(--color-primary); } footer { border-top: 2px solid var(--color-primary); } /* To change the colour: 1 edit. */
Repeating yourselfa { color: #60ab1e; } h1 { color: #60ab1e; } .button { background: #60ab1e; } blockquote { border-left: 4px solid #60ab1e; } footer { border-top: 2px solid #60ab1e; } /* To change the colour: 5 edits. Miss one, or mistype it, and the site becomes inconsistent. */

Why they matter:

Try it. This mock-up is styled only with var(--brand) and var(--heading-font). If you change either value, everything that uses it updates at once, with no other edits needed:

LIVE DEMO

Hill Café

Freshly roasted coffee, baked in house. Read the menu

Book a table

The brand colour is used in 4 places. It is stored in 1.

This demo uses a little JavaScript to change the properties, to show that they are live values. In your own work, you’ll normally just edit the CSS.

Declaring a custom property

Name, colon, value, semicolon

Declare a custom property inside a rule, just like any other declaration. The only difference is that its name begins with --.

:root { --color-primary: #60ab1e; --font-body: Inter, sans-serif; --space-m: 1rem; } /* ^ name ^ value */

Choose names by purpose

Name a property for what it’s for, not for what it looks like today. That way, the name stays true when the value changes. Group related names with a common prefix, such as --color-, --font- or --space-.

Do--color-primary --color-text --font-heading --space-m --radius
Don’t--blue (what if it becomes green?) --x (means nothing) --Color_Primary (mixed case) --color primary (a space isn't allowed) -color-primary (one hyphen)

Using a custom property

The var() function

Use var() wherever you would normally write the value. Inside the brackets, give the name of the property, including the two hyphens.

a { color: var(--color-primary); font-family: var(--font-body); padding: var(--space-m); }
TechniqueSyntaxWhat it does
Basic usecolor: var(--color-primary);Uses the stored value
With a fallbackcolor: var(--color-accent, #e8a317);Uses the value after the comma if the property isn’t defined
In a calculationpadding: calc(var(--space-m) * 2);Does arithmetic with the stored value
One property built from another--color-link: var(--color-primary);A custom property can use another
Fallback chainvar(--button-bg, var(--color-primary))Tries one, then another
Docolor: var(--color-primary); color: var(--color-primary, #60ab1e);
Don’tcolor: --color-primary; color: var(color-primary); color: var(--Color-Primary);

If you use a name that isn’t defined, and there’s no fallback, the declaration has no effect and the property falls back to its default value. A misspelt name fails silently, so check your names carefully. Note that you can use var() in any value, but not in property names, selectors or media query conditions.

The :root pseudo-class and inheritance

Declare global values on :root

The :root pseudo-class (see cheat sheet 6) matches the root element of the document, which in HTML is always the <html> element. It’s at the top of the family tree from cheat sheet 3, so every element on the page is a descendant of it.

:root { --color-primary: #60ab1e; --font-body: Inter, sans-serif; }

Why :root works: custom properties are inherited. A child element receives its parent’s custom properties, in the same way that it receives its parent’s color or font-family. So, if you declare a custom property on the root, every element on the page can use it, and that makes it global.

Custom properties and CSS properties

What kind of values can be stored in a custom property?

Any kind of value can be stored, and a custom property can be used with any CSS property that accepts that kind of value.

Kind of valueDeclareUse
Colour (hex)--color-primary: #60ab1e;color: var(--color-primary);
background-color: var(--color-primary);
border-color: var(--color-primary);
Colour with alpha--color-overlay: #14213d99;background-color: var(--color-overlay);
Typeface--font-heading: Lora, serif;font-family: var(--font-heading);
Font size--font-size-large: 1.5rem;font-size: var(--font-size-large);
Line height (a plain number)--line-height: 1.6;line-height: var(--line-height);
Spacing--space-m: 1rem;padding: var(--space-m);
margin-bottom: var(--space-m);
gap: var(--space-m);
Border radius--radius: 0.5rem;border-radius: var(--radius);
Shadow--shadow: 0 2px 8px rgb(0 0 0 / 15%);box-shadow: var(--shadow);
Width--content-width: 65ch;max-width: var(--content-width);
Time--speed: 200ms;transition: color var(--speed);
A whole image--hero: url("../images/hero.jpg");background-image: var(--hero);

Use custom properties for values that are shared or repeated, and for real design decisions. A one-off value, such as a single unusual margin, is fine as an ordinary value.

A typical use case: a branding project

Colour and typography that must stay consistent

A brand will have a style guide, which is a fixed set of colours, typefaces and spacing that’s used in the same way across the whole site. Custom properties turn that guide into code. You define each design decision once, in a block of design tokens, and everything else uses only var(). This guide is rendered from the tokens below:

/* 1. The style guide, defined once */ :root { /* Colour */ --color-primary: #60ab1e; --color-primary-dark: #417415; --color-accent: #e8a317; --color-text: #1b3008; --color-surface: #ffffff; --color-background: #eef2f7; /* Typography */ --font-heading: Lora, Georgia, Charter, Cambria, serif; --font-body: Inter, Roboto, "Helvetica Neue", "Arial Nova", sans-serif; --font-size-base: 1rem; --font-size-large: 1.5rem; --line-height: 1.6; /* Spacing and shape */ --space-s: 0.5rem; --space-m: 1rem; --space-l: 2rem; --radius: 0.5rem; }
/* 2. The rest of the stylesheet uses only var() */ body { font-family: var(--font-body); font-size: var(--font-size-base); line-height: var(--line-height); color: var(--color-text); background-color: var(--color-background); } h1, h2, h3 { font-family: var(--font-heading); color: var(--color-primary); } main a { color: var(--color-primary); } main a:hover { color: var(--color-primary-dark); } article { background-color: var(--color-surface); padding: var(--space-m); border-radius: var(--radius); border-left: 4px solid var(--color-accent); } .button { background-color: var(--color-primary); color: var(--color-surface); padding: var(--space-s) var(--space-m); border-radius: var(--radius); }
What you gain
  • Consistency. Every heading, link and button uses the same colours and fonts, because there’s only one copy of each.
  • Rebranding in minutes. Change --color-primary and --font-heading, and the whole site follows.
  • A shared language. Designers and developers both say “primary colour”, rather than “that blue”.
Good practice
  • Keep all the tokens together at the top of the stylesheet so that they’re easy to find and read.
  • Build variants from other tokens, as in --color-link: var(--color-primary);
  • Check that every pairing of text and background has enough contrast (see cheat sheet 5).

Example use case: a client changes their brand colour from blue to green. With custom properties, you edit one line (--color-primary), and the links, headings, buttons and borders all update together, with no chance of a missed or mistyped hex code.

Local scope and the cascade

Custom properties can be declared on any element

The :root pseudo-class isn’t the only place you can declare a custom property. A custom property declared on any selector is available to that element and all its descendants. This is its local scope, and elsewhere on the page, it doesn’t exist.

.card { --card-padding: 1.5rem; /* only available inside .card */ padding: var(--card-padding); } .card h2 { margin-bottom: var(--card-padding); } /* works: h2 is a descendant of .card */ footer { padding: var(--card-padding); } /* doesn't work: footer is outside .card */

A local value overrides a global one with the same name

Each element uses the value from the nearest ancestor that sets it, starting with the element itself and working up the family tree. So, if you declare the same name again on a closer element, the local value wins, but only there. The global value is untouched everywhere else.

This follows the normal cascade rules from cheat sheet 6. When two rules apply to the same element, they’re settled by specificity, and then by order, so .button.danger beats .button. An inline style attribute beats both.

A simple use case: a dark container element

Suppose the footer needs to be dark. Rather than rewriting every colour rule for the footer, you can simply redeclare the colour properties on the footer. Every rule inside it uses var(), so the footer re-themes itself automatically.

/* Global defaults */ :root { --panel-bg: #edf4fb; --panel-text: #0d2438; --panel-link: #296eac; } /* One set of rules, written once */ .panel { background-color: var(--panel-bg); color: var(--panel-text); } .panel a { color: var(--panel-link); } /* Local override: same names, new values, only for .dark */ .panel.dark { --panel-bg: #14213d; --panel-text: #ffffff; --panel-link: #9bb8ff; }

Default panel

Uses the global values. A link

<div class="panel">

Dark panel

Same rules, local values. A link

<div class="panel dark">

Example use case: an inverted (dark) footer, a highlighted “warning” box, or a button variant such as .button.danger. In each case, you write the component’s rules once, and a variant only changes the custom properties it needs.

Component hooks with fallbacks

If you combine local scope with a fallback in var(), a component can have a sensible default that any variant can override:

.button { background-color: var(--button-bg, var(--color-primary)); } .button.danger { --button-bg: #a82a22; } /* only danger buttons change */
A gotcha:root { --color-primary: #60ab1e; --color-primary-dark: var(--color-primary); /* worked out here, on :root */ } footer { --color-primary: #9bb8ff; } /* Inside footer, --color-primary changes, but --color-primary-dark still holds the value it was given on :root. */

A custom property built from another is worked out where it’s declared. So, if you override the source property locally, you’ll need to redeclare the dependent property in the same place.

More ways to redefine once

Redeclaring custom properties

Because the same name can be redeclared, you can change a whole design from one place, for dark mode or for larger screens, for example:

:root { --color-background: #eef2f7; --color-text: #14213d; --space-l: 2rem; } @media (prefers-color-scheme: dark) { :root { --color-background: #0f1626; --color-text: #e8edf7; } } @media (min-width: 48rem) { :root { --space-l: 3rem; } }

Every rule that uses these properties will update itself. This cheat sheet uses the same technique to switch between its light and dark colours.

Common mistakes

MistakeFix
Writing color: --color-primary;Use var(): color: var(--color-primary);
One hyphen: -color-primaryTwo hyphens: --color-primary
A different case or spelling in var(), such as var(--Color-Primary)Copy the name exactly. Names are case-sensitive.
Using a property outside its scope, such as one declared on .card used in the footerDeclare it on :root, or on a common ancestor
Naming by looks: --blueName by purpose: --color-primary
Turning every value into a variableUse them for repeated or shared design decisions
Hex codes still scattered through the stylesheetReplace repeated values with var(), and keep the definitions in :root
Overriding a source property but not the ones built from itRedeclare the dependent properties locally too
Trying var() in a selector or media query conditionIt works only in values. Use a literal value there.

Check before you upload

Learn more

Tip: in your browser’s dev tools, select an element and look in the Styles panel. Custom properties appear in the list of inherited values, and if you hover over a var(), its value is shown.