Sub topic 12
Common pitfalls and how to debug them
This chapter assumes you can declare custom properties and read them with var(). It also assumes you know how they cascade and inherit, and you have seen fallback values and @property. Here you will learn the mistakes that come up most often with tokens, and how to find them in browser DevTools.
Invalid at computed-value time
A custom property accepts almost any value when it is declared. The browser checks the value only when var() is substituted into a real property. If the result is not valid for that property, the declaration becomes invalid at computed-value time.
The browser does not fall back to the previous rule in the cascade. It treats the property as unset. For a non-inherited property such as background, that means the initial value (transparent).
:root {
--color-accent: 16px; /* wrong type, but accepted here */
}
.button {
background: #eee; /* ignored: overridden below */
background: var(--color-accent); /* invalid at computed-value time */
}The button ends up with a transparent background. DevTools usually shows the declaration with a warning icon, and the Computed pane shows the initial value.
Fallbacks do not catch wrong values
The fallback in var(--x, fallback) is used only when --x is not defined (or is the guaranteed-invalid value). It is not used when --x exists but holds a bad value for the property.
.badge {
/* --badge-padding is defined as "red", so the fallback is NOT used */
padding: var(--badge-padding, 1rem);
}If you need a bad value to be rejected up front, register the property with @property. A registered property that receives a value not matching its syntax is invalid when declared, and it falls back to its initial-value.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Value is ignored and the property resets | Wrong type for the target property | Check the token's value against the property that uses it |
| Token is blank or has no effect | Typo in the name | Names are case-sensitive: `--Color-Text` and `--color-text` are different |
| Override has no effect on a component | Token is set on `:root` but the component redefines it locally | Override at the same level or closer to the element |
| Theme works in one area only | Override applied to a scope that does not contain the element | Move the override to a common ancestor |
| `calc()` returns nothing | Missing units, such as `calc(var(--n) * 4)` where `--n` has a unit but the other side is wrong | Multiply a length by a unitless number |
| Token cannot be animated | Unregistered property, so the browser sees only text | Register with `@property` |
Circular references
If two tokens refer to each other, both become invalid.
:root {
--a: var(--b);
--b: var(--a); /* cycle: both are guaranteed-invalid */
}Any property that uses --a or --b behaves as if no value was set, and a fallback will apply. A self-reference such as --size: calc(var(--size) * 2) is also a cycle.
Inheritance surprises
Custom properties inherit by default, and a token is resolved where it is declared. A var() inside a custom property is substituted on that element, and the child inherits the result, not the formula.
:root {
--space-base: 4px;
--space-2: calc(var(--space-base) * 2); /* resolves to 8px here */
}
.compact {
--space-base: 2px; /* --space-2 stays 8px inside .compact */
}To make .compact rescale, redeclare --space-2 in that scope as well, or declare the derived tokens on a selector that matches every element you want to rescale.
Debugging workflow
- Inspect the element and open the Styles pane. Look for struck-through or warning-marked declarations.
- Open the Computed pane and check the final value of the affected property.
- Click through a
var()reference. Chrome and Firefox DevTools show the resolved value when you hover over it. - Check which element defines the token. Tokens appear under the rule that declares them, and inherited ones are grouped under "Inherited from".
- Read the value from the console to confirm what the browser resolved.
const el = document.querySelector('.button');
const value = getComputedStyle(el).getPropertyValue('--color-accent');
console.log(JSON.stringify(value)); // empty string means undefined or invalidAn empty string means the token is not defined on that element, or it is a cycle.
Exercise
A token --card-padding: 2em solid is defined on :root, and .card { padding: var(--card-padding, 1rem); } renders with no padding. Why is the 1rem fallback not used, and how would you catch this earlier?
Show answer
The fallback applies only when the token is undefined. Here the token is defined, so its value is substituted. 2em solid is not valid for padding, so the declaration is invalid at computed-value time and padding resets to its initial value of 0. To catch the error earlier, register --card-padding with @property and syntax: "<length>", so the bad value is rejected when declared and the initial value is used.