Sub topic 11
Limitations and common pitfalls
This chapter assumes you can declare custom properties, read them with var(), and explain how they inherit. It covers the places where custom properties behave in surprising ways, and how to avoid each one.
How var() resolution can fail
The browser substitutes var() references at computed-value time, after the cascade has picked a winner. Several pitfalls follow from that order of events.
Invalid at computed-value time
The browser can't tell whether var(--size) is valid for width until substitution. At parse time it accepts the declaration. If the substituted result is wrong, the property is not skipped, and the browser does not fall back to an earlier declaration. It becomes unset. That means inherit for inherited properties and initial for the rest.
:root {
--size: red;
}
.box {
width: 200px;
width: var(--size); /* becomes unset, so width is auto, not 200px */
}The usual symptom is a property that silently reverts to its default. Check the computed value in DevTools. Chrome and Firefox mark such declarations as invalid at computed-value time.
Units and numbers
A custom property holds a sequence of tokens, not a typed value. You can't glue a unit onto a substituted number.
:root {
--space: 8;
}
.a { margin: var(--space)px; } /* invalid */
.b { margin: calc(var(--space) * 1px); } /* 8px */Store the unit inside the property when you can (--space: 8px). Store a bare number only when you need to multiply it by different units.
Where var() is not allowed
You can use var() only in property values. It does not work in:
- property names, such as
var(--prop): red - selectors
- media query and container query conditions
- the name of an at-rule
:root { --bp: 600px; }
/* Does not work: the condition is never substituted */
@media (min-width: var(--bp)) {
.nav { display: flex; }
}Write the breakpoint as a literal, or generate the CSS with a build tool. A custom property can't make a media query responsive, but a media query can change a custom property. That is how the dark theme example works.
The same limit applies to strings. var() does not substitute inside quoted text or inside an unquoted url(). Put the whole value in the property instead:
:root {
--hero: url("hero.jpg");
}
.banner {
background-image: var(--hero);
}Inheritance and late changes
A custom property that contains var() is resolved on the element where it is declared. Its children inherit the result, not the formula.
:root {
--gap: 4px;
--double: calc(var(--gap) * 2); /* resolved here: 8px */
}
.tight {
--gap: 1px; /* --double is still 8px inside .tight */
}To make the derived value follow the change, move the calc() into the rule that consumes it: padding: calc(var(--gap) * 2). You can also redeclare --double inside .tight.
Fallbacks and empty values
A fallback applies only when the property is missing or holds the guaranteed-invalid value. An empty declaration such as --x: ; is a valid empty value, so the fallback is skipped. To reset a property so the fallback applies again, write --x: initial;.
.card {
--accent: initial;
border-color: var(--accent, gray); /* uses gray */
}Cycles
If two properties refer to each other, both become guaranteed-invalid. A property that refers to itself does the same.
:root {
--a: var(--b);
--b: var(--a);
}Reading values from JavaScript
getPropertyValue returns the raw token text, including leading whitespace, and returns an empty string for an undefined property. Always call trim(). Don't expect computed results either. An unregistered --w: calc(1px + 2px) comes back as the text calc(1px + 2px).
const raw = getComputedStyle(document.documentElement)
.getPropertyValue('--space');
console.log(JSON.stringify(raw)); // might be " 8px"
console.log(raw.trim() || 'not set');Other things to watch
- Names are case-sensitive:
--Brandand--brandare different properties. - Changing a property on
:rootcan force style recalculation for the whole page. For values that change often, such as pointer-driven effects, set the property on the smallest element that needs it. - Unregistered properties don't animate smoothly. The value flips halfway through a transition. Register the property with
@propertyto fix that. - Custom properties do not work in legacy browsers such as Internet Explorer 11. If you must support them, provide a plain declaration as a baseline, and use
@supports (--a: 0)to detect support.
Exercise
A stylesheet sets --pad: 12; on :root. The rule .box { padding: var(--pad)px; } has no effect. Explain why, and give two ways to fix it.
Show answer
var(--pad)px substitutes to the tokens 12 px, which is not a valid length, so the declaration is invalid at computed-value time and padding becomes unset. Fix it by writing padding: calc(var(--pad) * 1px);, or by declaring --pad: 12px; and using padding: var(--pad);.
Exercise
:root defines --gap: 4px and --double: calc(var(--gap) * 2). Inside .tight, --gap is set to 1px. What is --double inside .tight, and why?
Show answer
It is 8px. --double was resolved on :root, and .tight inherits the computed result. To get 2px, compute the calc() in the consuming rule or redeclare --double inside .tight.