CSS @property for Typed Custom Properties
Typed custom properties without JavaScript
Custom properties were strings. You couldn't animate them or get browser validation. @property gives you a type, so the browser can interpolate and validate.
The old hack vs the modern fix
View demo:root {
--hue: 0;
}
.wheel {
background: hsl(var(--hue), 80%, 50%);
transition: --hue .3s;
/* ignored, not interpolable */
}@property --hue {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
.wheel {
background: hsl(var(--hue), 80%, 50%);
transition: --hue .3s;
}Registered custom properties browser support
Live demo
Why the modern way wins
Animatable
Transition or animate custom properties. The browser interpolates by type.
Validated
Invalid values fall back to initial-value. No silent string surprises.
No JS
No script to parse or tween. Pure CSS for hue, length, or number transitions.
- Lines Saved
- JS → 0
- No script for tweening
- Old Approach
- Untyped string
- No animation, no validation
- Modern Approach
- @property
- syntax + initial-value
How it works
Custom properties were untyped. The browser stored them as strings, so you couldn't transition from 0 to 360 for a hue, and invalid values weren't caught. You needed JS to tween or validate.
@property --name { syntax: "<angle>"; inherits: false; initial-value: 0deg; } registers the variable with a type. The browser can interpolate it in transitions and animations and will reject invalid values. Syntax can be <length>, <color>, <number>, and more.