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.
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:
Consistency. The same colour, font or spacing is guaranteed to be the same everywhere, because it’s one value, not many copies.
Efficiency. You can change a design decision in one line, and the whole site updates.
Readable code. A value such as var(--color-primary) tells you what the value is for, whereas #60ab1e doesn’t.
Flexibility. Values can be changed for a single section, for dark mode or at different screen sizes, and JavaScript can change them live.
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 */
The name starts with two hyphens, as in --color-primary. One hyphen isn’t enough.
Names are case-sensitive, so --Brand and --brand are two different properties.
After the --, you can use letters, numbers, hyphens and underscores. However, it’s best to follow the naming rules from cheat sheet 1, and use lowercase, with hyphens.
The value can be almost anything you could write in CSS, such as a colour, a length, a font list, a number, a shadow, or even a whole url(). CSS stores it exactly as written.
As usual, each declaration ends with a semicolon.
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-.
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.
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.
html ← :root declares --color-primary
├── body inherits it
│ ├── header
│ │ └── h1 inherits it: color: var(--color-primary)
│ └── main
│ └── p
│ └── a inherits it: color: var(--color-primary)
└── (and every other element)
Both :root and html select the same element, but :root has a higher specificity (a pseudo-class counts like a class), and it’s the convention for global custom properties.
Put all your site-wide values together in a single :root block at the top of the stylesheet, so that you have a single source of truth.
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.
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:
/* 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.
Looking up --color-text for an a inside footer:
a (no) → footer (yes: #ffffff) stop: the local value wins
Looking up --color-text for an a inside main:
a (no) → p (no) → main (no) → body (no) → html (yes: #14213d) the global value
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;
}
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:
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:
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.