Sub topic 4
Fallback values with var()
This chapter assumes you can declare a custom property and read it with var(). It also assumes you know how custom properties inherit down the tree, both covered in earlier chapters. Here you will learn what the second argument of var() does, when it applies, and when it does not.
The syntax
var() takes a property name and an optional fallback, separated by the first comma.
.badge {
padding: var(--badge-padding, 0.75rem);
}If --badge-padding has a value on the element (set directly or inherited), that value is used. If it has no value, the browser uses 0.75rem.
Only the first comma separates the name from the fallback. Everything after it is the fallback, including more commas.
body {
font-family: var(--font-body, system-ui, sans-serif);
}The fallback here is the whole list system-ui, sans-serif.
When the fallback is used
The fallback applies only when the custom property is not defined for that element. Two cases count as "not defined":
- No ancestor or the element itself ever declared the property.
- The property was set to the keyword
initial, which resets it to its guaranteed-invalid starting state.
An empty-looking declaration or a wrong-looking value does not trigger the fallback. A defined value always wins, even if it is useless for the property you put it in.
:root {
--color-text: 16px; /* defined, but not a color */
}
.note {
color: var(--color-text, black);
}The fallback black is not used here, because --color-text exists. The browser substitutes 16px into color, finds the result invalid, and treats the declaration as invalid at computed-value time. Since color is an inherited property, .note takes its parent's color. Custom properties accept almost any value when declared, so the error only shows up at the point of use. The pitfalls chapter returns to this.
Nesting fallbacks
A fallback can itself be a var(). This builds a chain that is checked from left to right.
.button {
background: var(--button-bg, var(--color-accent, #2563eb));
}The browser tries --button-bg, then --color-accent, then the literal color. Keep chains short. Two levels is usually enough, and a third level suggests the token structure needs work.
The component hook pattern
Fallbacks let a component expose an optional setting without declaring it anywhere. The component reads a token with a default, and a consumer can override it from outside.
.badge {
padding: var(--badge-padding, var(--space-3));
}
.sidebar {
--badge-padding: 0.25rem;
}A .badge in a .sidebar inherits the override. Any other .badge uses --space-3. The trade-off is that the default lives inside one rule, so it is invisible in your token list. Use this pattern for optional hooks. For tokens every component needs, declare a value on :root, as the tiers chapter shows.
Do not use a fallback to hide a missing token during development. A typo such as --color-acent with a fallback renders fine and never reports an error, so the wrong value can ship unnoticed.
Resolution at a glance
Exercise
Given :root { --gap: red; } and .row { margin-left: var(--gap, 1rem); }, what margin does .row get, and why isn't 1rem used?
Show answer
The margin-left declaration is invalid at computed-value time, so margin-left resets to its initial value of 0, because it is not an inherited property. The fallback 1rem is skipped because --gap is defined (as red). A fallback only covers a missing property, not a value that doesn't fit where it is used.