Sub topic 12
Common pitfalls and how to debug them
This chapter assumes you can declare tokens, consume them with var(), and layer primitive, semantic and component tokens. It also assumes you have used the theming scopes from earlier chapters. Here you will learn the failures that show up once a token system grows, and a repeatable way to find their cause.
Why token bugs look strange
Most CSS errors are caught when the browser parses the stylesheet. An invalid declaration is dropped, and the previous valid declaration for that property still applies. Custom properties break this rule. The browser accepts almost any value for a custom property when it parses it. It checks whether the value makes sense only when var() is substituted into a real property.
This delay explains most of the confusing behavior in this chapter.
Invalid at computed-value time
Suppose a token holds a value that the consuming property cannot use:
:root {
--card-width: large;
}
.card {
width: 300px;
width: var(--card-width);
}The width: var(--card-width) declaration parses fine, because the browser cannot know yet whether large is valid. At computed-value time the substitution produces width: large, which is invalid. The declaration is then treated as invalid at computed-value time. The earlier width: 300px is not restored. Instead the property behaves as if it were set to unset:
- For an inherited property such as
color, the element takes the parent's value. - For a non-inherited property such as
width, the element takes the initial value, which isauto.
So the card ignores your 300px fallback and the result looks arbitrary.
DevTools shows this clearly. In the Styles pane, Chrome and Edge draw a warning icon next to the declaration. Hovering over it says the declaration is invalid at computed-value time. Firefox shows a similar note. The Computed pane shows the unset result.
Fallbacks that do not trigger
The second argument of var() is used only when the token is not defined. It is not used when the token is defined with a value that the property rejects.
.badge {
--badge-padding: 12; /* missing unit */
padding: var(--badge-padding, 0.75rem);
}Here --badge-padding exists, so the fallback is ignored. The substitution gives padding: 12, which is invalid, and the padding becomes 0.
Two related details:
- An empty value such as
--badge-padding: ;is a defined, valid empty value, so the fallback is also ignored. This leaves the property with nothing in it. - The fallback can itself contain
var(), as invar(--a, var(--b, 1rem)). A long chain is hard to read, so keep it to one level and put defaults in the tier that owns them.
Names are case sensitive
Standard properties ignore letter case, but custom property names do not. --Color-Accent and --color-accent are different tokens. A misspelled name is not an error. It is simply undefined, and var() then uses the fallback or the unset behavior. If a token seems to have no effect, check the spelling first. A naming convention with only lowercase letters and hyphens removes this whole class of bug.
Resolution happens where the token is declared
This is the most common theming bug. A custom property that contains var() is resolved on the element that declares it, and the resolved result is what children inherit. Consider this setup:
:root {
--color-gray-900: #111827;
--color-text: var(--color-gray-900);
}
.promo {
--color-gray-900: #fef3c7;
}
.promo p {
color: var(--color-text);
}You might expect the paragraph to use the new gray. It does not. --color-text was resolved on :root, using the root's --color-gray-900. The .promo element inherits the finished value #111827. Redefining the primitive in a descendant scope never reaches the semantic token.
The fix is to override the token that the components read:
.promo {
--color-text: #fef3c7;
}The same problem affects scales built with calc(). If --space-4 is defined on :root as calc(var(--space-base) * 4), then setting --space-base on .compact does not change --space-4 inside it. To make a scale respond to a local base, redeclare the derived tokens in that scope too. Alternatively, have the components compute the value from the base at the point of use.
Cycles
A token that refers to itself, directly or through other tokens, becomes invalid:
:root {
--a: var(--b);
--b: var(--a);
}Every token in the cycle becomes guaranteed-invalid, which behaves as undefined. A common accidental version is writing --space: var(--space) while trying to extend a token inherited from the parent. This is not possible, because the declaration refers to its own new value. Give the new token a different name, such as --space-local.
Units and numbers
A token holds a sequence of CSS tokens, not a typed value. Two consequences cause frequent trouble:
- Text is pasted as written.
--gap: 8followed byvar(--gap)pxproduces8 pxwith a space, which is invalid. Store8px, or usecalc(var(--gap) * 1px)when the token must stay a bare number. - Multiplying two lengths in
calc()is invalid.calc(var(--space-base) * var(--space-base))fails, butcalc(var(--space-base) * 4)works, because one side is a number.
Bare numbers are useful for things like --accent-hue, as shown in the @property chapter. Document in the token name or comments which tokens are unitless.
Places where var() does not work
var() can appear only in property values. You cannot use it in these places:
- Selectors.
- Property names.
- Media query conditions, so
@media (min-width: var(--bp-md))does not work. - Container query conditions.
Token systems that need shared breakpoints usually keep the numbers in a build step or write the values out. The last chapter covers generating CSS from source data.
Registered properties and inheritance
Registering a token with @property changes how failures behave:
- A value that does not match the declared
syntaxis rejected when the stylesheet is parsed. The declaration is dropped, and the previous valid declaration or the initial value applies. This makes errors visible earlier. - Setting
inherits: falsestops the token from flowing to descendants. If you register a token like this and declare it on:root, children read the initial value instead. Registered tokens that act as theme values should normally useinherits: true. initial-valuemust be computationally independent. It cannot depend onvar()or on relative units such asem.
Reading values from JavaScript
getComputedStyle(el).getPropertyValue('--token') returns the token's substituted text, not a fully evaluated value. For an unregistered --space-4 defined as calc(var(--space-base) * 4), the result is the text calc(0.25rem * 4), not 1rem. The returned string can also start with a space, so call trim(). An empty string means the token is undefined on that element.
If you need a resolved value, apply the token to a real property on a probe element and read that property, for example getComputedStyle(probe).paddingLeft.
A debugging workflow
When a token seems wrong, work from the consuming property back to the declaration.
Practical steps for each stage:
- Select the element in DevTools. Look in the Styles pane for the invalid warning on the consuming declaration.
- In the Computed pane, search for the token name, such as
--color-text. Chrome lists custom properties there. The value shown is what this element sees. - In the Styles pane, scroll through the "Inherited from" sections to see which ancestor supplied the token.
- Edit the token live in DevTools to confirm the fix before changing the source.
For a wider view, this script lists every token declared in readable stylesheets with its value on a chosen element:
function listTokens(element = document.documentElement) {
const names = new Set();
for (const sheet of document.styleSheets) {
let rules;
try {
rules = sheet.cssRules;
} catch {
continue; // cross-origin stylesheet, cannot be read
}
for (const rule of rules) {
if (!rule.style) continue; // skips @media, @property and similar
for (const prop of rule.style) {
if (prop.startsWith('--')) names.add(prop);
}
}
}
const computed = getComputedStyle(element);
return [...names].sort().map((name) => ({
name,
value: computed.getPropertyValue(name).trim() || '(undefined here)',
}));
}
console.table(listTokens(document.body));Run it with different elements to see where a token disappears or changes.
Performance notes
Changing a custom property on :root can force the browser to recalculate styles for the whole document, because any descendant might depend on it. This is fine for a theme switch. It is costly when done on every animation frame or scroll event. For frequent updates, set the token on the smallest element that needs it, and prefer animating transform or opacity where you can.
Quick reference
| Symptom | Likely cause | Check |
|---|---|---|
| Property falls back to `auto`, `0` or the parent's value | Invalid at computed-value time | Warning icon in the Styles pane |
| Fallback in `var()` is ignored | Token is defined but has a bad value, or an empty one | Computed pane value of the token |
| Token has no effect at all | Misspelled name, different case | Compare names character by character |
| Theme override changes nothing | Derived token was resolved on `:root` | Override the semantic token instead |
| Scale ignores a local base | `calc()` resolved at the declaring element | Redeclare the scale in the scope |
| Children read the initial value | Registered with `inherits: false` | `@property` rule |
| JavaScript returns `calc(...)` text | Unregistered tokens are not evaluated | Read a real property on a probe element |
Exercise
A stylesheet defines --color-text: var(--color-gray-900); on :root. A .dark-panel rule sets --color-gray-900: #f9fafb;, but text inside the panel stays dark. Why does this happen, and what is the smallest change that fixes it?
Show answer
--color-text is resolved on :root using the root's --color-gray-900, and descendants inherit the finished value #111827. Overriding the primitive inside .dark-panel never reaches it. The fix is to set --color-text: #f9fafb; (or var(--color-gray-50)) inside .dark-panel, so the token the components read is redefined in that scope.
Exercise
Given --gap: 8; and .row { gap: var(--gap, 1rem); }, what gap does .row get, and why is the fallback not used? How would you fix it while keeping --gap a bare number?
Show answer
The gap is 0. The fallback applies only when the token is undefined, and --gap is defined. The substituted declaration gap: 8 is invalid because it has no unit, so it is invalid at computed-value time and the property is treated as unset, which gives the initial value. Keeping the bare number, write gap: calc(var(--gap) * 1px);. You can also store the token as 8px.