Sub topic 1
Declaring custom properties and reading them with var()
Prerequisites
This chapter assumes you know basic CSS syntax (selectors, properties, values), have a working idea of the cascade and specificity, and can read simple HTML. You do not need JavaScript here. Later chapters cover scripting.
What a custom property is
A custom property is a property whose name you choose. The name must start with two dashes, such as --brand-color. The browser stores the value you give it and applies it to nothing by itself. It only has an effect when another declaration reads it with the var() function.
Custom properties are often called CSS variables. They differ from variables in a preprocessor such as Sass, because the browser resolves them at runtime. They take part in the cascade and inheritance like any other property, so their values can change per element, per media query, or per state.
Custom property names are case-sensitive. --Brand and --brand are two different properties.
Declaring and reading
You declare a custom property like any other property, inside a rule. The most common place is :root, which matches the html element. Every element in the page inherits from it, so the value is available everywhere.
You read the value with var(--name).
:root {
--brand-color: #0b5fff;
--space: 8px;
}
.button {
background: var(--brand-color);
padding: calc(var(--space) * 2);
}Here .button gets a blue background and 16px of padding. If you change --space to 12px in one place, every rule that reads it updates.
Two points are worth noting:
var()can be used as a value, or as part of a value, for almost any normal property. It can sit inside functions such ascalc().var()cannot be used as a property name or as a selector.var(--side): 10pxis invalid.
What a value can contain
A custom property accepts almost any sequence of tokens. It does not have to be a color or a length. It can hold a partial value, a list, or a whole shorthand.
:root {
--shadow: 0 2px 4px rgb(0 0 0 / 0.25);
--font-stack: "Inter", system-ui, sans-serif;
}
.panel {
box-shadow: var(--shadow);
font-family: var(--font-stack);
}The browser does not check the value when you declare it. It only checks it when var() substitutes it into a real property. We return to that in a later chapter.
Fallback values
var() takes an optional second argument. The browser uses it when the custom property is not defined.
.card {
color: var(--card-color, #222);
border-color: var(--card-border, var(--brand-color, gray));
}If --card-color is missing, the text is #222. The second line shows nesting: the browser tries --card-border, then --brand-color, then gray. Everything after the first comma is the fallback, so a fallback may itself contain commas, as in a font list.
The fallback applies only when the property is undefined. If it is defined with a value that turns out to be unusable, the fallback is not used. The later chapter on invalid values explains that case.
The substitution flow
The diagram shows how the browser handles a var() reference.
"Defined on this element" includes values inherited from ancestors, because custom properties inherit by default.
A first working example
Put the pieces together in a small page:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Custom properties</title>
<style>
:root {
--brand-color: #0b5fff;
--space: 8px;
}
.button {
background: var(--brand-color);
color: white;
border: 0;
padding: calc(var(--space) * 2);
}
.note {
color: var(--note-color, #444);
}
</style>
</head>
<body>
<button class="button">Save</button>
<p class="note">No --note-color is defined, so the fallback applies.</p>
</body>
</html>Open the page and inspect the button in your browser's developer tools. The Computed pane shows background-color resolved to the real color. The Styles pane shows --brand-color on :root.
Common mistakes
- Writing
$brand-coloror@brand-color. Only names that start with--work. - Using a custom property without
var().background: --brand-color;is invalid. - Putting a unit-less number where a unit is needed.
padding: var(--space)with--space: 8fails, because8is not a length. Write8px, or usecalc(var(--space) * 1px). - Mixing up case in the name.
Exercise
Exercise
Given :root { --gap: 4px; }, write a declaration that sets margin to twice the gap, and explain what happens for .box { color: var(--box-color, navy); } when --box-color is not defined anywhere.
Show answer
margin: calc(var(--gap) * 2); gives 8px. For .box, the property is undefined, so the browser uses the fallback and the text color is navy.
Exercise
Why is padding: var(--space); invalid when --space: 8; is declared?
Show answer
The browser substitutes the token 8 into padding, and a bare number is not a valid length for padding. The declaration is invalid at computed-value time. Declaring --space: 8px fixes it.