Sub topic 11
Custom properties: inheritance you control
This chapter assumes you know how selectors and declarations work, how specificity and source order decide conflicts, and which properties inherit by default. You should also be comfortable inspecting an element in browser developer tools.
What a custom property is
A custom property is a property whose name you choose. The name starts with two hyphens, such as --gap. You read its value with the var() function.
:root { --gap: 1rem; }
.stack > * + * {
margin-top: var(--gap);
}Custom properties take part in the cascade like any other property. Specificity, origin, !important, layers and source order all decide which --gap declaration wins on an element. The difference is that the browser attaches no meaning to the value. It only stores the value until a var() reference uses it.
Inheritance is on by default
Ordinary properties such as margin and border do not inherit unless the specification says so. Custom properties always inherit. A child element gets its parent's computed value of --gap unless a rule sets a different one on the child.
That gives you a way to change a value for a whole subtree. Declare the property once on an ancestor, and every descendant that reads it sees the new value.
:root { --gap: 1rem; }
.compact { --gap: 0.25rem; }
.stack > * + * { margin-top: var(--gap); }Every .stack inside a .compact element uses 0.25rem. Every other .stack uses 1rem from the root. The .stack rule is identical in both places. Only the inherited value differs.
Fallback values
var() accepts a second argument that is used when the property is not defined.
.card {
padding: var(--card-padding, 1rem);
}If no ancestor defines --card-padding, the padding is 1rem. The fallback applies only when the property is missing or invalid at computed-value time. It does not apply when the property holds a value that is valid for the custom property but wrong for padding.
Where the value is substituted
The browser substitutes var() at computed-value time. It does not substitute when it parses the stylesheet. Two consequences follow.
- A custom property is resolved on each element, using that element's inherited and cascaded values. The same
var(--gap)can produce different results on different elements. - If the substituted result is not valid for the property that uses it, the declaration becomes invalid at computed-value time. The property then behaves as if it were unset. An inherited property takes the parent's value. A non-inherited property takes its initial value. The browser does not fall back to an earlier declaration in the cascade.
:root { --size: bold; }
p {
font-size: 1rem;
font-size: var(--size);
}Here font-size: var(--size) is accepted when the stylesheet is parsed, because the browser cannot yet check it. At computed-value time bold is not a valid length, so the declaration is invalid. Since font-size inherits, the paragraph takes its parent's font size. The earlier font-size: 1rem is not used, because the later declaration still won the cascade.
Common errors
- Expecting a fallback line to rescue a bad
var(). As the example above shows, the cascade has already chosen thevar()declaration. - Setting the property on the wrong element. If you declare
--gapon.stackitself and read it on.stack's parent, the parent sees nothing. Inheritance only goes downward. - Case mistakes. Custom property names are case-sensitive.
--Gapand--gapare different properties. - Using a name that is never defined. Without a fallback, the declaration becomes invalid at computed-value time. Add a fallback or define the property on
:root.
Debugging
In the Styles panel, var(--gap) usually shows the resolved value on hover, and the declaration is struck through or flagged if it is invalid at computed-value time. In the Computed panel you can read the final value of the property that uses it. Select a custom property in the Computed list, where your browser supports it, to see which element supplied it.
Exercise
Given :root { --gap: 1rem; } .compact { --gap: 0.25rem; } and .stack > * + * { margin-top: var(--gap); }, what margin does a .stack child get when the .stack is inside a .compact element, and why?
Show answer
The margin is 0.25rem. --gap is an inherited property, so elements inside .compact inherit its value of 0.25rem instead of the 1rem from :root, and var(--gap) is resolved per element.
Exercise
A rule p { font-size: 1rem; font-size: var(--size); } runs with --size: bold. What font size does the paragraph get, and why is it not 1rem?
Show answer
It takes the parent's font size, because font-size inherits. The var() declaration wins the cascade, but bold is invalid for font-size at computed-value time, so the property behaves as unset. The earlier 1rem declaration is not used.