Sub topic 4
How var() substitution works and what happens with invalid values
This chapter assumes you can declare a custom property, read it with var(), and write a fallback. It also assumes you know how the cascade picks a winning declaration. Here you will learn when var() is replaced, and what the browser does when the result is not a valid value.
Substitution happens at computed-value time
A custom property holds a sequence of tokens. The browser does not check that sequence against any property's grammar when it parses --gap: 12px or --gap: banana. It stores the tokens as written.
The check happens later. For each element, the browser follows these steps:
- It runs the cascade and picks the winning declaration for every property, including custom properties.
- It resolves
var()references in custom properties. A custom property can usevar()to read another. - It replaces each
var()in an ordinary property with the tokens of the custom property. - It parses the resulting text as a value for that ordinary property.
The browser calls step 4 "computed-value time". Only at this point does it learn whether the value is valid.
Invalid at computed-value time
Suppose a custom property holds a value that makes no sense for the property that uses it.
:root {
--size: banana;
}
p {
font-size: 20px;
font-size: var(--size);
}A normal invalid declaration is dropped when the stylesheet is parsed. The browser then falls back to the earlier font-size: 20px. A declaration with var() is different. The browser cannot reject it at parse time, because it does not yet know what --size holds. So it accepts the declaration, and it wins the cascade over the earlier one. Then substitution produces font-size: banana, which is invalid.
The spec calls this state invalid at computed-value time. The declaration is not discarded in favor of an earlier rule. Instead the property becomes unset:
- For an inherited property such as
font-sizeorcolor,unsetmeans the value of the parent. - For a non-inherited property such as
backgroundorborder,unsetmeans the initial value.
In the example, the paragraph does not get 20px. It gets the parent's font size.
Fallbacks and what counts as "missing"
The fallback in var(--x, fallback) is used only when --x is not defined, or when it holds the guaranteed-invalid value. It is not used when --x is defined but wrong for the property.
:root {
--size: banana;
}
p {
font-size: var(--size, 20px);
}Here --size exists, so the fallback is ignored. The result is still invalid at computed-value time, and the paragraph inherits its font size.
The guaranteed-invalid value is what an undefined custom property holds. You can also create it on purpose with the keyword initial:
.card {
--accent: initial;
border-color: var(--accent, gray);
}Setting --accent: initial resets it to the guaranteed-invalid value. The fallback gray now applies, even if an ancestor had defined --accent. This is a handy way to say "ignore anything inherited here".
References that cannot be resolved
Two other situations also produce invalid values.
- Cycles. If
--a: var(--b)and--b: var(--a), both properties become guaranteed-invalid. Any fallback on the reading side then applies. - Missing property with no fallback.
margin: var(--missing)makes the declaration invalid at computed-value time, somarginbecomesunset. Sincemarginis not inherited, it gets its initial value,0.
Debugging an invalid value
When a style seems to vanish, work through these checks:
- In the browser dev tools, find the declaration that uses
var(). Most tools mark it with a warning such as "invalid at computed-value time". - Look at the computed value of the custom property on that element. A typo in the name gives an undefined property.
- Check that the value suits the property. A bare number such as
12fails forpadding, while12pxworks. - Check the scope. The custom property may be defined on a sibling or child instead of an ancestor.
Exercise
Given the CSS below, what font size does the paragraph get when its parent has font-size: 18px, and why?
:root { --size: 2 }
p { font-size: 14px; font-size: var(--size, 16px); }
Show answer
It gets 18px, the parent's size. --size is defined, so the fallback 16px is not used. The tokens 2 are not a valid font-size, so the declaration is invalid at computed-value time. The var() declaration still wins over 14px in the cascade, and since font-size is inherited, unset means the parent's value.
Exercise
How can you make var(--accent, gray) use gray inside one component, even though an ancestor sets --accent: red?
Show answer
Set --accent: initial on the component. That resets the property to the guaranteed-invalid value, so the fallback gray applies.