Sub topic 2
Providing fallback values
This chapter assumes you can write basic CSS, understand selectors and the cascade, and have declared a custom property and read it with var(). Here you will learn how the second argument of var() works, how to nest fallbacks, and which mistakes cause surprises.
What a fallback is
The var() function takes a custom property name and an optional second argument. That second argument is the fallback. The browser uses it when the custom property is not defined on the element, which means no value is available through the cascade or inheritance.
.card {
color: var(--card-color, #222);
border-color: var(--card-border, var(--brand-color, gray));
}If --card-color is not defined for a .card element, its text color is #222. If --card-color is defined, its value wins and the fallback is ignored.
A fallback does not change the cascade. The browser first resolves which value the custom property has on the element. Only when there is none does it substitute the fallback.
Nesting fallbacks
A fallback can itself contain var(). The browser tries each level from left to right.
In the second declaration above, the browser checks --card-border first. If it is missing, it checks --brand-color. If that is also missing, it uses gray.
Keep chains short. Two levels are common. Beyond that, the chain is hard to read and a better fix is usually to define the property once at a higher level.
Commas inside a fallback
Only the first comma separates the name from the fallback. Everything after it is the fallback, including more commas. This makes lists work as fallback values.
body {
font-family: var(--font-body, "Helvetica Neue", Arial, sans-serif);
}Here the fallback is the whole font list. You do not need to wrap it in extra syntax.
An empty fallback
A fallback may be empty. var(--x,) is valid, and it substitutes nothing when --x is undefined. This is rarely needed, but it differs from var(--x). With no comma, an undefined property makes the declaration invalid at computed-value time, which a later chapter covers.
Fallbacks are not for invalid values
A fallback applies only when the property is undefined, or set to the keyword initial. It does not apply when the property is defined with a value that turns out to be wrong for the place you use it.
:root {
--space: blue;
}
.box {
padding: var(--space, 8px);
}--space is defined, so the browser substitutes blue. The result, padding: blue, is not valid. The browser does not go back to 8px. Instead the declaration becomes invalid at computed-value time, and padding falls back to its inherited or initial value, not to your fallback.
Fallbacks in components
Fallbacks are most useful when a component accepts optional customization. The component reads a hook property, and supplies a default if the page has not set it.
.btn {
background: var(--btn-bg, var(--brand-color, #0b5fff));
padding: var(--btn-padding, 0.5rem 1rem);
color: white;
}A page that sets nothing gets a working button. A page can override --btn-bg on a single element or an ancestor without editing the component's rules.
A fallback can also be stored as a custom property of its own. Compare these two approaches:
| Approach | Where the default lives | Trade-off |
|---|---|---|
| Inline fallback: `var(--btn-bg, #0b5fff)` | In every rule that reads the property | Works with no setup, but the default is repeated |
| Defined default: `.btn { --btn-bg: #0b5fff; }` | In one declaration on the component | Easier to change, but it overrides inherited values from ancestors |
The second row has a catch. If .btn defines --btn-bg itself, a value set on a parent element is no longer inherited, because the element's own declaration wins. A fallback inside var() leaves inheritance intact, so it suits properties that callers are expected to set from outside.
Common mistakes
- Putting a space before the name, or omitting the leading
--. The name must start with two dashes and match the declared name exactly. Custom property names are case-sensitive, so--Brandand--brandare different. - Expecting the fallback to fix a bad value, as shown above.
- Writing long nested chains that hide where a value actually comes from. Use browser DevTools, which show the computed value and its source, when debugging.
- Using a fallback to hide a typo in the property name. The page looks fine, but the custom property is never read.
Check your understanding
Exercise
Given :root { --gap: 2 } and .row { margin-left: var(--gap, 12px); }, what is the computed margin-left of .row, and why is the 12px fallback not used?
Show answer
The value is not 12px. --gap is defined as 2, so the browser substitutes 2 and the fallback is ignored. margin-left: 2 is invalid because a length needs a unit (only 0 may be unitless). The declaration becomes invalid at computed-value time, so margin-left takes its initial value, 0, not the fallback.
Exercise
Write a rule for .alert that sets background to --alert-bg if defined, else to --brand-color, else to #eee.
Show answer
.alert { background: var(--alert-bg, var(--brand-color, #eee)); }