Sub topic 10
Animating values with registered properties
This chapter assumes you can declare custom properties and read them with var(). It also assumes you know how transition and @keyframes work, and that you have seen @property registration. It covers why registration makes animation possible and how to use it.
Why plain custom properties don't animate
An unregistered custom property has no type. The browser stores its value as a token stream, which is raw text with no meaning attached. When the value changes, the browser can't work out the values between the old and new ones. It swaps the value at the first moment of the transition.
.box {
--offset: 0px;
transform: translateX(var(--offset));
transition: --offset 0.4s ease; /* has no effect: --offset is untyped */
}
.box:hover {
--offset: 100px;
}The box jumps to its new position on hover. The transition is valid CSS, but it has nothing to interpolate.
Registering a type
@property gives the browser a type for the property, which lets it interpolate. You supply three descriptors:
syntaxis the type the value must match, such as'<angle>'.inheritsistrueorfalse.initial-valueis the value used when none is set.
@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 browser interpolates --angle from 0deg to 180deg. Each frame, var(--angle) is substituted with the current value, and rotate() receives a real angle.
Choosing a syntax
The syntax value decides how the property interpolates, and which values are valid for it.
| Syntax | Example value | Interpolates as |
|---|---|---|
| `'<length>'` | `12px` | A length |
| `'<percentage>'` | `40%` | A percentage |
| `'<angle>'` | `90deg` | An angle |
| `'<color>'` | `#0b5fff` | A color, channel by channel |
| `'<number>'` | `0.5` | A number |
| `'*'` | any tokens | Not at all |
The universal syntax '*' is the default for unregistered properties. A property registered with '*' is no more animatable than an unregistered one.
Animating a gradient
Gradients are the common practical use. Browsers don't interpolate between two background-image gradients, but they can animate a registered number or percentage inside the gradient.
@property --fill {
syntax: '<percentage>';
inherits: false;
initial-value: 0%;
}
.bar {
height: 12px;
background: linear-gradient(90deg, teal var(--fill), #ddd var(--fill));
transition: --fill 0.6s ease;
}
.bar:hover {
--fill: 100%;
}The two colour stops move together, so the bar appears to fill from left to right.
The same technique works in @keyframes:
@property --hue {
syntax: '<number>';
inherits: false;
initial-value: 0;
}
@keyframes cycle {
to {
--hue: 360;
}
}
.swatch {
background: hsl(var(--hue) 80% 50%);
animation: cycle 6s linear infinite;
}What to remember when registering
- Registration happens once, globally. If the same name is registered twice, the later declaration is ignored.
- The initial value must be computationally independent. Use
0degor10px. Don't use1em, which depends on context, orvar(--x). - An invalid value falls back. If
--angle: redis set on a property registered as<angle>, the declaration is invalid at computed-value time and the property uses its initial value or inherited value. - Set
inheritsdeliberately. Withfalse, children don't see the value, which avoids recalculating styles down the whole subtree on each animation frame. - Relative units resolve early. A registered
<length>given2emis converted to pixels on the element where it is set. Children then inherit the resolved length, not2em.
Exercise
Exercise
A button uses background: radial-gradient(circle, white var(--r), blue var(--r)); and transition: --r 0.3s;. On hover, --r changes from 0% to 100%, but the gradient jumps instead of growing. What is missing, and what does it need to contain?
Show answer
The property is not registered, so the browser treats it as untyped and can't interpolate it. Add an @property --r rule with syntax: '<percentage>', inherits: false and initial-value: 0%. The transition then interpolates --r on every frame.
Exercise
You register --size with syntax: '*' and add transition: --size 1s. Does it animate?
Show answer
No. The universal syntax '*' gives the browser no type, so it can't interpolate between values. Use a specific syntax such as '<length>'.