Sub topic 11
When to use @keyframes instead
This chapter assumes you know how transition and @starting-style work from the earlier chapters. You should also be comfortable with animation and @keyframes basics.
Two tools, two models
A transition moves between two states. It needs a before-change style and an after-change style. @starting-style supplies the before-change style when none exists, for example on first render.
A keyframe animation does not compare states. It runs a timeline you define, whenever the animation property applies. That makes it a better fit when the effect is not a simple change from A to B.
Choose by the shape of the effect
| Need | Better tool |
|---|---|
| One start value and one end value | Transition with `@starting-style` |
| Reversible effect that can be interrupted midway | Transition |
| Three or more steps (overshoot, bounce, shake) | `@keyframes` |
| Repeating or looping motion | `@keyframes` |
| Effect that runs on load with no state change | `@keyframes` |
| Different timing per step | `@keyframes` |
A multi-step entry with @keyframes
A "pop in" that overshoots its final size needs three stops. A transition cannot express that, because it only knows two values.
.toast {
animation: toast-in 0.5s ease-out;
}
@keyframes toast-in {
0% {
opacity: 0;
transform: translateY(1rem) scale(0.95);
}
60% {
opacity: 1;
transform: translateY(-0.25rem) scale(1.02);
}
100% {
opacity: 1;
transform: none;
}
}Animations start when the element first renders, and also when it changes from display: none to a displayed value. So this needs no @starting-style rule. The first style update starts the animation directly.
Where keyframes are weaker
Keyframe animations are not reversible by default. If you want a matching exit, you write a second @keyframes rule and switch the animation value. If the user triggers the exit while the entry is still running, the new animation starts from its own 0% keyframe. The element can visibly jump. A transition instead reverses from its current computed value.
Exits also need care. Setting display: none removes the element at once, and its animation stops with it. You have two options:
- Run the exit animation first, then set
display: nonein ananimationendhandler. - Include
displayin the keyframes. Browsers that support discrete animation ofdisplaywill keep the element rendered until the animation ends.
.toast.is-leaving {
animation: toast-out 0.3s ease-in forwards;
}
@keyframes toast-out {
to {
opacity: 0;
transform: translateY(1rem);
display: none;
}
}The forwards fill mode keeps the final keyframe applied after the animation ends.
Reduced motion applies here too
Keyframe animations need the same reduced-motion handling as transitions. Remove or shorten them rather than only shortening the duration. Looping animations in particular should stop.
@media (prefers-reduced-motion: reduce) {
.toast {
animation-duration: 0.01ms;
animation-iteration-count: 1;
}
}Common mistakes
- Using
@starting-stylefor a bounce. It only supplies a start value, so there is no way to add a middle step. - Forgetting
forwardson an exit animation, so the element snaps back to full opacity before it is hidden. - Expecting a keyframe exit to be interruptible. It restarts instead of reversing.
Exercise
A notification should slide in, overshoot slightly, then settle. Should you use a transition with @starting-style or @keyframes, and why?
Show answer
Use @keyframes. The effect has three stops (start, overshoot, rest), and a transition with @starting-style only moves between two values. The animation also starts on first render, so no @starting-style rule is needed.
Exercise
Why can an exit written with @keyframes jump if the user dismisses a panel while its entry animation is still running?
Show answer
Changing the animation property starts the new animation from its own 0% keyframe, not from the element's current values. A transition instead starts from the current computed value, so it reverses smoothly.