CSS transition vs animation: states, keyframes, interruptions and when to use each
CSS transition vs animation explained: two states vs keyframe sequences, how each behaves when interrupted, events, @starting-style and a clear decision table.
CSS has two built-in ways to move things: transition and animation. They share a lot. Both take a duration, a delay and an easing function, and both animate the same properties. That makes it easy to reach for the wrong one, and the difference only shows when a user hovers off halfway, clicks twice, or the page first loads.
This post explains what each one is for, shows how differently they behave when interrupted, covers the events and newer features like @starting-style, and ends with a decision table and the mistakes that come up most often. If you are choosing between CSS and a JavaScript library instead, read GSAP vs CSS animation.
Transitions: from one state to another
A transition says: when this property changes, do not jump to the new value, move to it over time. It needs a trigger, which is any change of the computed value. That change can come from a pseudo-class like :hover, :focus-visible or :checked, a class added by JavaScript, a changed custom property, or an inline style.
.button {
background: #27272a;
transform: translateY(0);
transition: background-color 200ms ease, transform 200ms cubic-bezier(.2, .8, .2, 1);
}
.button:hover,
.button:focus-visible {
background: #3f3f46;
transform: translateY(-2px);
}
A transition only ever knows two values: where the property is now and where it should be. You do not describe the path in between, only the duration and the easing curve. When the hover ends, the property changes back and the same transition runs in the other direction.
Animations: a keyframe sequence
An animation runs a named @keyframes sequence. It does not need a change of value. It starts as soon as the animation property applies to the element, which can be on page load.
.dot {
animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
40% { transform: scale(1.35); opacity: .6; }
100% { transform: scale(1); opacity: 1; }
}
That gives animations things transitions do not have:
- Several steps. Any number of keyframes, each with its own easing through
animation-timing-functioninside the keyframe. - Autoplay. No trigger needed. Spinners, loaders, attention pulses and entrance effects run on their own.
- Looping.
animation-iteration-countcan be a number orinfinite, andanimation-direction: alternateplays every other loop backwards. - Fill mode.
animation-fill-mode: forwardskeeps the last keyframe after the animation ends,backwardsapplies the first keyframe during the delay, andbothdoes both. - Pausing.
animation-play-state: pausedfreezes it in place.
The real difference: interruption
The difference that matters most in UI is what happens when the state changes again before the motion finishes. Try it below. Press the button, then press it again while the dots are still moving.
The orange dot uses a transition. When you toggle back mid-way, it turns around from wherever it is and glides home. The browser starts a new transition from the current value to the new target. The CSS Transitions spec also shortens the reverse duration in proportion to how far the first transition got, so a quick hover-off does not take the full duration to undo.
The blue dot uses two keyframe animations, one for each direction. Toggling mid-way swaps one animation for the other, and the new animation starts from its own first keyframe. So the dot jumps to the far end and then travels back. An animation does not know where the element currently is. It only knows its keyframes.
This is why hover, focus, open and close, and any toggle a user can flip quickly should use transitions. They are reversible by design.
Restarting and replaying
The flip side is that transitions cannot be replayed. If the value does not change, nothing runs. Animations can be restarted, with a few rules:
- Changing
animation-name, or removing the animation and applying it again, restarts it from the first keyframe. - Removing a class and adding it back in the same JavaScript task does nothing, because the browser never sees the in-between style. Wait a frame, or use the Web Animations API with
element.getAnimations()and callplay()after settingcurrentTimeback to 0. - Changing the duration or easing of a running animation applies to it immediately, without a restart.
Events
Both fire DOM events you can listen to, which is how you chain JavaScript work after the motion.
| Transitions | Animations |
|---|---|
transitionrun: created, before any delay | animationstart: after the delay |
transitionstart: after the delay | animationiteration: at the end of each loop except the last |
transitionend: finished, once per property | animationend: finished |
transitioncancel: interrupted or removed | animationcancel: removed before finishing |
Two details catch people out. transitionend fires once for every property that transitioned, so a transition on opacity and transform fires twice; check event.propertyName. And neither end event fires if the motion is cancelled, for example when the element gets display: none, so do not rely on them alone to clean up state. Events also bubble, so a listener on a parent hears its children's transitions too.
Entry and exit: @starting-style and allow-discrete
Transitions used to have two gaps. They could not run when an element first appears, and they could not animate to or from display: none. Two newer features close both.
@starting-style defines the values an element starts from on its first style update, so a transition has something to move from. transition-behavior: allow-discrete lets discrete properties such as display take part in a transition: when hiding, display flips to none at the end, and when showing, it flips at the start.
.panel {
opacity: 1;
transform: translateY(0);
transition:
opacity 250ms ease,
transform 250ms ease,
display 250ms allow-discrete;
}
.panel.is-hidden {
display: none;
opacity: 0;
transform: translateY(8px);
}
/* where it starts when it first appears or stops being display: none */
@starting-style {
.panel {
opacity: 0;
transform: translateY(8px);
}
}
Adding and removing is-hidden now fades the panel in and out, it is removed from layout when hidden, and it animates in on first render too. For popovers and dialogs in the top layer, add overlay 250ms allow-discrete to the list as well. These features are supported in current versions of the major browsers, but support for overlay is narrower, so check it for your audience. In older browsers the panel simply appears and disappears without motion, which is a safe fallback. For whole-page or shared-element changes, see the View Transitions API guide.
Which one to use
| You need | Use |
|---|---|
| Hover, focus, press, checked or open/close states | Transition |
| Motion that users can reverse mid-way | Transition |
An element fading in when it is added, or hidden with display: none | Transition with @starting-style and allow-discrete |
| A loop: spinner, pulse, shimmer, orbit | Animation |
| More than two steps, or different easing per step | Animation |
| Motion that starts on its own on page load | Animation, or a transition with @starting-style if it is a simple fade-in |
| Replaying the same effect on demand | Animation, or the Web Animations API |
| Motion tied to scroll position | Animation with animation-timeline |
A good rule: if the motion describes a change of state, use a transition. If it describes an event or an ongoing effect, use an animation. Many components use both, for example a button with a hover transition and a loading spinner animation inside it.
Common mistakes
- Using a keyframe animation for hover. It plays on hover-in, then snaps back on hover-out because the animation is removed. Use a transition, or see how to capture hover animations to study how polished sites do it.
transition: all. It animates properties you did not intend, including ones that change for layout reasons, and makes theme switches crawl. List the properties.- Putting the transition only on the hover state. Then the motion runs on hover-in but the hover-out is instant. Put
transitionon the base rule. - Transitioning from
auto.height: autodoes not transition by default. Usegrid-template-rows: 0frto1fr, amax-height, orinterpolate-sizewhere it is supported. - Forgetting fill mode. Without
forwardsorboth, an entrance animation with a delay shows the final state first, then jumps to the first keyframe, or snaps back at the end. - Animating layout properties.
width,topandmargintrigger layout every frame. Prefertransformandopacityin both transitions and animations. - Ignoring reduced motion. Both need a
prefers-reduced-motionrule. Our prefers-reduced-motion guide covers what to keep and what to remove.
Reading the timing from a live site
When you want to match a transition or animation you have seen elsewhere, guessing the duration and curve rarely gets it right. Chrome DevTools shows both in the Animations panel. The Capture Motion Chrome extension records the element while it plays, including hover states, and gives you the keyframes, durations, delays and easing of every animated property. You can export them as CSS, React or GSAP code, a self-contained HTML and CSS file, or JSON, then decide for yourself whether the result should be a transition or an animation in your codebase.
Questions
What is the main difference between a CSS transition and a CSS animation?
A transition animates a property between two values when that value changes, for example on hover or when a class is added. An animation runs a @keyframes sequence that can have many steps, start on its own, loop and keep its end state with animation-fill-mode.
Why does my CSS animation jump when I move the mouse away?
When the selector that applies the animation stops matching, the animation is removed and the element snaps back to its base styles. For hover effects that should reverse smoothly, use a transition instead.
Can a CSS transition run when the page loads?
Not by itself, because there is no change of value to transition from. The @starting-style rule gives an element a starting value for its first style update, so it can transition in when it is first rendered or goes from display: none to visible.
How can I see the exact timing of a transition or animation on another site?
Open the Animations panel in Chrome DevTools, or use the Capture Motion Chrome extension, which records the element while it plays and reads the keyframes, duration, delay and easing of every animated property, whether it is a transition or a keyframe animation.
