CSS var() fallback not applied when a custom property holds an invalid value type
0 reputation · 25 May 2025, 14:54 UTC
0 reputation · 25 May 2025, 14:54 UTC
I want a design-token setup where var(--token, fallback) reliably protects components, so that if a token is ever assigned a value of the wrong type (for example a length where a color is expected), the component still renders with its declared fallback color instead of losing the declaration entirely.
My understanding is that the fallback in var() only applies when the custom property is unset or explicitly set to the guaranteed-invalid value. If --token is defined but holds a value invalid for the target property, the declaration becomes invalid at computed-value time and resolves to the inherited or initial value — silently, with no console error. That makes the fallback useless for the exact failure mode I care about.
I am aware @property allows registering a custom property with a syntax, inherits, and initial-value, which could enforce typing and revert invalid assignments to the initial value. However, I am unsure how this interacts with unregistered tokens, older browsers that ignore @property, and tokens composed from multiple var() references.
@property the intended way to get predictable invalid-value handling, or is there a lighter-weight pattern?initial-value in all cases, or only for direct assignments?@property support — is graceful degradation to untyped behavior acceptable?30275 reputation · 25 May 2025, 15:53 UTC
The fallback in var(--token, fallback) is only used when the custom property is unset (or explicitly set to the guaranteed‑invalid keywords unset, initial or inherit). If the property is defined but holds a value that is syntactically valid yet semantically wrong for the target property (e.g., --token: 16px used where a color is expected), the var() function substitutes that value, the declaration becomes invalid at computed‑value time, and the property resolves to its inherited or initial value – the fallback is never consulted.
var() fallback mechanism is defined in the CSS Cascading and Inheritance Level 4 spec and triggers only for the "guaranteed‑invalid" state of a custom property.@property and given a syntax, inherits, and initial-value, the browser treats assignments that do not match the syntax as invalid and replaces them with the declared initial-value during value computation.@property ignore the rule entirely and treat all custom properties as <any-value>, preserving the existing untyped behavior.@property, specifying the appropriate syntax type (e.g., <color>, <length>) and an initial-value that matches your desired fallback.var(--token, fallback). The fallback now serves only as a safety net for the case where the token is truly unset; type mismatches are handled by the registration.@property (IE11, Safari <14), accept that tokens will remain untyped and the var() fallback will not protect against type errors. In that environment you can either:Do you need to support browsers that lack @property (IE11, Safari <14)? Knowing this determines whether you can rely solely on registration or must add a degradation strategy.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.