Sub topic 11
When to use @keyframes instead
This chapter assumes you know how CSS transitions work (transition-property, duration, timing function), how display: none removes an element from rendering, and how @starting-style and transition-behavior: allow-discrete were used in earlier chapters. Here you compare transitions with @keyframes and decide which one fits a given effect.
The two models
A transition animates between two states. The browser compares the before-change style with the after-change style and interpolates the difference. You declare the endpoints (with @starting-style for entry), and the browser handles the path.
A keyframe animation does not depend on a change of state. You name an animation, describe its steps with @keyframes, and attach it with animation. It starts when the element gets the animation name applied, including on first render. That is why @keyframes has always worked for entry effects without any starting-style.
Here is the same fade-and-slide entry written both ways.
/* Transition version */
.card {
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
@starting-style {
opacity: 0;
transform: translateY(1rem);
}
}/* Keyframes version */
@keyframes card-in {
from {
opacity: 0;
transform: translateY(1rem);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.card {
animation: card-in 0.4s ease;
}Both produce the same result on first render. The differences show up when something else changes the element mid-effect, when the element leaves, or when the effect needs more than two states.
How each model handles interruption
Suppose a user toggles a panel open and closed quickly.
With a transition, the browser starts the new transition from the element's current computed value. If the panel is halfway faded in and the user closes it, it reverses from where it is. The browser also shortens the reversed duration so the motion feels proportional. You get smooth reversal for free.
With a keyframe animation, the animation is tied to a rule. If you remove the class that applies card-in and add a class that applies card-out, the new animation starts from its own from keyframe. The element jumps to the first frame of the new animation, which causes a visible snap unless you author the exit keyframes carefully, for example by omitting the from block so the browser uses the current computed value as the start.
@keyframes panel-out {
/* No "from": the browser uses the element's current value */
to {
opacity: 0;
transform: translateY(1rem);
}
}Omitting from helps, but it only works well when the animated properties are not already controlled by another running animation.
Comparing the options
| Question | Transition with `@starting-style` | `@keyframes` |
|---|---|---|
| Needs a state change to run | Yes | No, runs when applied |
| Runs on first render | Only with `@starting-style` | Yes |
| Number of steps | Two (start and end) | Any number |
| Reverses smoothly when interrupted | Yes | Not automatically |
| Can loop | No | Yes (`animation-iteration-count`) |
| Can animate `display` and `overlay` for exit | Yes, with `allow-discrete` | Yes, but `display` is discrete inside keyframes (see below) |
| Where the end state lives | In the normal rule | In the keyframes, plus `animation-fill-mode` |
| Browser support | Newer (Baseline 2024 for `@starting-style`) | Universal |
Choose keyframes when
The effect has more than two stages
A transition has only a start and an end. If the element should overshoot, pause, and settle, you need intermediate stops.
@keyframes pop-in {
0% {
opacity: 0;
transform: scale(0.8);
}
60% {
opacity: 1;
transform: scale(1.05);
}
100% {
opacity: 1;
transform: scale(1);
}
}
.toast {
animation: pop-in 0.35s ease-out;
}You can approximate overshoot in a transition with a cubic-bezier() or linear() timing function, and that is often enough. Use keyframes when different properties need different timing at different moments.
The effect repeats or runs without a trigger
Loading spinners, pulsing badges and attention shakes loop or run on their own. A transition needs a change in style to start, so it cannot loop. Use animation with animation-iteration-count: infinite.
@keyframes pulse {
50% {
transform: scale(1.1);
}
}
.badge {
animation: pulse 1.2s ease-in-out infinite;
}The browser does not support `@starting-style`
Browsers that do not understand @starting-style ignore the block. The element then appears instantly, which is an acceptable fallback for many interfaces. If the entry effect is important and you must support those browsers, use @keyframes for entry. You can also combine both, as shown below.
You need to animate an element that appears without a style change you control
Some elements are rendered by third-party code, and you can only add a class or a stylesheet. A keyframe animation attached by a selector runs when the element matches, so it does not depend on your timing relative to the first style update.
Choose transitions when
The effect is a simple A to B change that must reverse
Showing and hiding a panel, a dialog or a popover is exactly this case. The browser handles interruption, and the end states are written in the same rules you already maintain for the open and closed states. Entry and exit stay symmetrical because both read from the same properties.
You are animating top-layer elements
Popovers and modal dialogs enter the top layer when they open and leave it when they close. Transitions with overlay and display set to allow-discrete keep the element in the top layer until the exit finishes. Keyframes can do the same, but the pattern is less direct, because animation on the closing state needs the element to still be rendered when the animation starts.
You want the final state to live in the normal rule
With a transition, the settled values are your regular declarations. With a keyframe entry, the final values are in the last keyframe, and if the normal rule disagrees, the element snaps when the animation ends unless you use animation-fill-mode: forwards. Fill modes carry their own cost: a forwards fill keeps overriding the cascade for that element, which can surprise you later when another rule tries to change the property.
Keyframes for exit
Keyframes can animate display too. In @keyframes, display is a discrete property, so it flips at the 50% mark of the keyframe interval by default, unless you set it explicitly at a particular stop. Modern browsers support animating display and content-visibility inside keyframes, which makes exit animations possible with animation alone.
@keyframes fade-out {
from {
opacity: 1;
}
to {
opacity: 0;
display: none;
}
}
.panel.is-hidden {
animation: fade-out 0.3s forwards;
}Here the forwards fill mode keeps display: none after the animation ends. Be aware of two points. First, when the class is removed, the panel reappears with no entry effect unless the entry is handled separately. Second, because the animation runs from a class toggle, an interrupted exit starts again from the from keyframe, so you get the snap described earlier.
This approach is workable, but the transition version in the previous chapters is shorter, reverses cleanly and needs no fill mode.
Combining both
Nothing prevents you from mixing the two. A common pairing is a transition for the open and close state and a keyframe animation for a decorative effect that occurs only once.
@keyframes shake {
25% { transform: translateX(-4px); }
75% { transform: translateX(4px); }
}
.field.has-error {
animation: shake 0.3s;
}
.field {
opacity: 1;
transition: opacity 0.3s ease;
@starting-style {
opacity: 0;
}
}Be careful when both target the same property. A running keyframe animation overrides the transitioning value for that property, because animations sit above normal declarations in the cascade. Keep the two on different properties, or use separate wrapper elements.
A fallback pattern for entry
If you want the keyframes entry for older browsers and want the transition behavior in new ones, use @supports with the @starting-style at-rule selector test.
.card {
opacity: 1;
animation: card-in 0.4s ease;
}
@supports at-rule(@starting-style) {
.card {
animation: none;
transition: opacity 0.4s ease;
@starting-style {
opacity: 0;
}
}
}Support for at-rule() in @supports is itself recent, so older browsers that lack it will simply keep the keyframes version. That is the outcome you want here. If you prefer not to depend on at-rule(), pick one technique and accept the fallback behavior of an instant appearance.
Decision flow
The diagram summarizes the choice for a new effect.
Reduced motion applies to both
Whichever you choose, respect the user's motion preference. For keyframes, shorten the animation or remove it.
@media (prefers-reduced-motion: reduce) {
.card,
.badge,
.toast {
animation-duration: 0.01ms;
animation-iteration-count: 1;
}
}Setting animation-iteration-count: 1 matters for looping effects such as the pulse, because shortening the duration of an infinite animation would make it flicker rapidly. For purely decorative loops, animation: none is often better.
Common mistakes
- Using
@keyframesfor a show/hide toggle and then wondering why a quick second click snaps the element to the start. Use a transition, or omitfrom. - Forgetting
animation-fill-mode: forwardson an exit, so the element returns to full opacity after the animation ends. - Setting a transition and an animation on the same property and finding the transition has no effect. The animation wins.
- Expecting a keyframe entry to run again when you toggle a class that does not change
animation-name. The animation only restarts when the name changes or the element is re-rendered.
Exercise
A notification badge should pulse continuously while there are unread messages, and a settings panel should fade in and out when toggled, reversing smoothly if the user toggles it twice quickly. Which technique fits each, and why?
Show answer
Use @keyframes with animation-iteration-count: infinite for the badge, because a transition needs a style change to start and cannot loop. Use a transition with @starting-style and transition-behavior: allow-discrete for the panel, because the browser starts a reversed transition from the current computed value, so quick toggles do not snap.
Exercise
You wrote an exit animation with @keyframes fade-out and animation: fade-out 0.3s on .panel.is-hidden. After the animation finishes, the panel is fully visible again. What is missing, and how do you fix it?
Show answer
The animation has no fill mode, so when it ends the element returns to its normal styles. Add animation-fill-mode: forwards (or forwards in the shorthand), and set display: none in the final keyframe so the panel stays hidden and is removed from rendering.