Sub topic 10
Animating values with registered properties
This chapter assumes you know how to declare custom properties and read them with var(). It also assumes you know how @property registers a typed property (syntax, inherits, initial-value) and have a basic grasp of CSS transitions and keyframe animations. The goal is to show why registration makes animation possible, how to use it in practice, and where it fails.
Why plain custom properties do not animate
An unregistered custom property has no type. The browser treats its value as a string of tokens. It can substitute that string into other properties, but it cannot compute a value halfway between 0deg and 180deg, because it does not know that either one is an angle.
When you transition an unregistered custom property, the browser switches from the old value to the new one at the start of the transition (or at the 50% mark of a keyframe animation). This is called a discrete change. No intermediate values are produced.
.box {
--offset: 0px;
transform: translateX(var(--offset));
transition: --offset 0.4s ease;
}
.box:hover {
--offset: 100px;
}In this example the transition declaration has no visible effect. The box jumps to its new position on hover and jumps back on exit.
Registering the property fixes this. Once the browser knows --offset is a <length>, it can interpolate between two lengths like any built-in property.
The animation recipe
Animating a custom property takes three steps.
- Register the property with a
syntaxthat describes an animatable type. - Use the property inside a normal declaration through
var(). - Change the value, then transition it or animate it with
@keyframes.
Here is the example from the plan:
@property --angle {
syntax: '<angle>';
inherits: false;
initial-value: 0deg;
}
.spinner {
transform: rotate(var(--angle));
transition: --angle 0.4s ease;
}
.spinner:hover {
--angle: 180deg;
}On hover, the computed value of --angle moves from 0deg to 180deg over 0.4 seconds. On each frame the browser substitutes the current value into rotate(), so the element turns smoothly.
Note that you transition the custom property itself (--angle), not transform. If you transitioned transform, the browser would interpolate the transform and the custom property would not need to be registered. The point of the technique is that the custom property is the thing that changes over time.
Which syntax values interpolate
The registered syntax decides how the browser interpolates. Not every type produces a smooth result.
| Syntax | Example value | Interpolation |
|---|---|---|
| `<length>` | `10px` to `50px` | Linear, as for `width` |
| `<percentage>` | `0%` to `100%` | Linear |
| `<length-percentage>` | `10px` to `50%` | Linear, mixing units through `calc()` |
| `<number>` | `0` to `1` | Linear |
| `<integer>` | `0` to `10` | Interpolated as a number, then rounded |
| `<angle>` | `0deg` to `180deg` | Linear |
| `<color>` | `red` to `blue` | Per channel, in the interpolation color space |
| `<time>`, `<resolution>` | `1s` to `3s` | Linear |
| `<transform-function>` | `rotate(0)` to `rotate(90deg)` | Per function, when the lists match |
| `*` (universal) | anything | Discrete only |
| `<custom-ident>`, `<url>`, `<string>` | `foo` to `bar` | Discrete only |
Two details matter in practice.
<integer>values pass through fractional numbers during the transition and are rounded when read. A counter that goes from0to100will step through whole numbers.- A syntax like
<length> | autoaccepts both types, but interpolation only works when both the start and end values are the same type. Going from10pxtoautois discrete.
Animating with keyframes
Transitions are fine for state changes such as hover. For continuous motion, use @keyframes.
@property --turn {
syntax: '<angle>';
inherits: false;
initial-value: 0deg;
}
.loader {
width: 80px;
height: 80px;
border-radius: 50%;
background: conic-gradient(from var(--turn), #0b5fff, #ffffff, #0b5fff);
animation: rotate-gradient 2s linear infinite;
}
@keyframes rotate-gradient {
to {
--turn: 360deg;
}
}This case shows the real value of the technique. You cannot interpolate the stops of a conic-gradient() directly, and background as a whole does not animate between two gradients. By pulling one piece of the gradient into a typed property, you animate just that piece.
The same pattern works for a gradient position, a color stop, or a percentage fill.
@property --fill {
syntax: '<percentage>';
inherits: false;
initial-value: 0%;
}
.meter {
height: 12px;
background: linear-gradient(
to right,
#0b5fff var(--fill),
#dddddd var(--fill)
);
transition: --fill 0.6s ease-out;
}
.meter:hover {
--fill: 80%;
}Both colors stop at the same position, so the boundary between them moves as --fill changes.
Animating a number for display
A registered <integer> can drive a counter. CSS counters can display it through counter-reset, which accepts a var() value.
@property --count {
syntax: '<integer>';
inherits: false;
initial-value: 0;
}
.stat {
counter-reset: n var(--count);
animation: count-up 1.5s ease-out forwards;
}
.stat::after {
content: counter(n);
}
@keyframes count-up {
from {
--count: 0;
}
to {
--count: 250;
}
}The text appears through the content property, which is generated content. Screen readers may or may not announce it, and users cannot select it. For information that matters, keep the final number in real HTML text and use this effect only as decoration.
The role of inheritance
The inherits descriptor affects both performance and behavior when you animate.
When inherits: true, every descendant of the animated element sees the new value on every frame. The browser must recompute styles for the whole subtree each time. When inherits: false, only the element itself is affected.
For an animated property, prefer inherits: false unless children need the value. The spinner and meter examples above need it only on the element that uses the value.
The reverse is useful when you want a parent animation to drive children. Set inherits: true, animate the value on the parent, and let children read it in their own declarations.
@property --progress {
syntax: '<number>';
inherits: true;
initial-value: 0;
}
.panel {
animation: fill 2s linear forwards;
}
.panel .bar {
width: calc(var(--progress) * 100%);
}
.panel .label {
opacity: var(--progress);
}
@keyframes fill {
to {
--progress: 1;
}
}One animation on .panel now drives two children with different formulas.
What the browser does each frame
Understanding the pipeline helps you predict cost and behavior.
Custom property animation runs on the main thread. Browsers can run animations of transform and opacity on the compositor thread, which keeps them smooth when JavaScript is busy. An animated custom property cannot take that path, even when the property it feeds is transform. Each frame goes through style recalculation, and then through layout or paint depending on what var() feeds.
This has a practical consequence:
- If
--offsetfeedstransformoropacity, you pay for style recalculation and paint on each frame, but not for layout. - If it feeds
width,height, ormargin, every frame also triggers layout.
For simple motion, animating transform directly is cheaper. Use a registered property when you need to animate something the browser cannot otherwise interpolate, or when several properties must share one animated value.
Common errors
The animation does not run. Check that the property is registered. An @property rule with an invalid syntax or a missing initial-value is dropped, and the property falls back to being unregistered. A syntax other than * requires an initial-value.
The value jumps at the start. The initial-value must be computationally independent. For a <length>, 10px works, but 2em does not, because em depends on context. Use absolute units in initial-value.
The start or end value is the wrong type. If you set --angle: 180 with no unit, the declaration is invalid for an <angle> syntax. The property computes to its initial value, so nothing animates (or the value snaps back).
Two values of different units look discrete. With <length>, going from 10px to 2rem works. With <length> | <percentage> as the syntax, going from 10px to 50% does not interpolate smoothly, because the types differ. Use <length-percentage> to allow mixing.
Registration appears after use. @property is global and order independent for the cascade. Still, a duplicate registration of the same name causes the last valid rule to win. Keep one definition per property in a shared stylesheet.
Registering from JavaScript
You can also register a property from script with CSS.registerProperty(). It accepts the same fields as the @property rule.
if (window.CSS && CSS.registerProperty) {
try {
CSS.registerProperty({
name: '--angle',
syntax: '<angle>',
inherits: false,
initialValue: '0deg'
});
} catch (error) {
console.warn('Property already registered:', error.message);
}
}Registering the same name twice throws an InvalidModificationError, so the try block matters if the code can run more than once. Use the CSS rule when you can. It keeps the definition next to the styles that depend on it.
Respecting reduced motion
Animation that runs without user action can cause problems for people with vestibular disorders. Wrap continuous or large animations in a media query that checks for the user's preference.
@media (prefers-reduced-motion: reduce) {
.loader {
animation: none;
}
.spinner {
transition: none;
}
}With this in place, the user sees the final state without the motion.
Decision criteria
| Situation | Choice |
|---|---|
| Moving, rotating, scaling, or fading one element | Animate `transform` or `opacity` directly |
| Animating a piece of a gradient, such as an angle, a stop, or a position | Registered custom property |
| One value drives several properties with different formulas | Registered custom property with `inherits: true` |
| Value is a keyword or string, such as `auto` or a font name | Cannot interpolate. Use another technique |
| Animation must stay smooth under heavy script load | Prefer compositor-friendly properties |
Exercise
A .dial element uses transform: rotate(var(--turn)), and --turn is declared in :root as --turn: 0deg, with no @property rule. A hover rule sets --turn: 90deg and the element has transition: --turn 0.3s. The dial snaps to 90 degrees instead of turning. What is the cause, and what is the fix?
Show answer
Without registration, --turn is an untyped token stream, so the browser cannot interpolate it and changes it discretely. Add @property --turn { syntax: '<angle>'; inherits: false; initial-value: 0deg; }. The existing transition then interpolates the angle on every frame. The :root declaration can stay, but with inherits: false it applies only to the root element. The dial would then see the initial value 0deg. Either set --turn on .dial itself, or register with inherits: true.
Exercise
You want a progress bar's fill to animate from 0% to 75% by changing a custom property used in linear-gradient(). Write the @property rule and the property that makes the bar fill when the page loads.
Show answer
Register the property: @property --fill { syntax: '<percentage>'; inherits: false; initial-value: 0%; }. Then use it in the gradient stops, linear-gradient(to right, #0b5fff var(--fill), #ddd var(--fill)), and add animation: load 1s ease-out forwards; with @keyframes load { to { --fill: 75%; } }. The forwards fill mode keeps the final value after the animation ends.