Sub topic 1
Transitions or animations: choosing the right tool
CSS can change a property value over time in two ways: transitions and keyframe animations. They look similar on screen, but they start differently, and they are controlled differently. This chapter explains the working model behind each one, gives you a way to choose between them, and lists the mistakes that cause the most confusion.
Prerequisites
This chapter assumes you already know:
- Basic CSS selectors and how the cascade decides which declaration wins.
- Common properties such as
color,opacityandbackground-color. - Pseudo-classes such as
:hoverand:focus-visible. - Basic HTML, and how to add or remove a class with JavaScript using
classList.
You do not need to know @keyframes syntax or easing functions yet. Later chapters cover them in detail.
The shared idea
Without motion, a style change is instant. The browser computes a new value and paints it on the next frame. Motion in CSS means the browser instead computes many in-between values, one per frame, until it reaches the final value. This in-between calculation is called interpolation.
The two features differ in who decides the start and end values, and what causes the motion to begin.
| Question | Transition | Keyframe animation |
|---|---|---|
| What starts it? | A change in a computed value | The `animation-name` being applied to an element |
| Who defines the steps? | The browser, from old value to new value | You, with `@keyframes` |
| Number of steps | Two (start and end) | Two or more |
| Can it repeat? | No | Yes, with `animation-iteration-count` |
| Needs a trigger? | Yes, a state or style change | No, it can run on page load |
| Can it reverse on its own? | Yes, when the state ends | Only with `animation-direction` |
| Can it be paused? | Not directly | Yes, with `animation-play-state` |
How a transition works
A transition watches an element's computed values. When a transitioned property changes, the browser does not jump to the new value. It interpolates from the old one over the given duration.
.button {
background-color: #2563eb;
transform: translateY(0);
transition: background-color 200ms ease-out, transform 200ms ease-out;
}
.button:hover,
.button:focus-visible {
background-color: #1d4ed8;
transform: translateY(-2px);
}The transition declaration lives on the base rule, not on :hover. The browser uses the transition settings of the style it is moving toward. When the pointer enters, the hover style has no transition of its own, so it would normally snap. In practice the hover rule inherits the same transition value from .button through the cascade, so both directions are smooth. If you put transition only inside the :hover rule, the way in is smooth and the way out snaps back instantly. This is the most common transition mistake.
You never describe the in-between states of a transition. You describe the two end states, and the browser fills in the middle. That makes transitions the right fit for interface feedback, where the element is moving between two known conditions.
How a keyframe animation works
An animation does not wait for a property to change. You define named steps with @keyframes, then attach the name to an element. The animation starts as soon as the style applies.
@keyframes pulse {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.1);
}
}
.badge {
animation: pulse 1.5s ease-in-out infinite;
}Here the badge scales up and back down forever. No hover, class change or other trigger is needed. A transition cannot do this, because a transition has only a start and an end, and nothing here ever changes state.
Choosing between them
Ask these questions in order.
- Does the element move between two states that the user or your code controls, such as hover, focus, expanded or collapsed? Use a transition.
- Does the effect need more than two steps, or does it need to repeat? Use an animation.
- Must it run without any state change, such as on page load or while a request is pending? Use an animation.
- Should the element start in a different state than its natural one, then settle? Use an animation, or a transition with a starting style (covered in a later chapter).
When both would work
Some effects can be built either way. A card that fades in on page load is a typical case. As an animation it needs no trigger.
@keyframes fade-in {
from {
opacity: 0;
transform: translateY(8px);
}
to {
opacity: 1;
transform: none;
}
}
.card {
animation: fade-in 400ms ease-out both;
}The both fill mode applies the from values before the animation begins and keeps the to values afterward, so the card does not flash or snap. Fill modes are covered in a later chapter. To build the same fade as a transition, you would need a class that JavaScript adds after the first render, and that adds code and timing risk. When the only reason for a transition is to fake a trigger, choose the animation.
The reverse also holds. A menu item that darkens on hover could be a pair of animations, one for entering and one for leaving. But a transition gives you the reverse for free and handles interruption better, as the next section shows.
Behavior that surprises people
Interruption
If a user moves the pointer off a button halfway through a hover transition, the transition reverses from its current in-between value. The browser does not jump to the end first. This makes transitions feel natural under fast input. An animation that you restart or remove can jump, because removing animation-name drops the element straight back to its normal style.
Cascade priority
Animations and transitions take part in the cascade at a higher priority than normal author rules. While an animation runs, its values override a normal declaration for the same property. A running transition overrides even !important declarations. If a style seems to be ignored, check whether motion is controlling that property.
Properties that cannot interpolate
Interpolation needs two values the browser can calculate between, such as two colors or two lengths. A change from height: 0 to height: auto has no calculable midpoint by default, so it switches instantly. The same applies to display: none to display: block. Which properties interpolate, and how, is the subject of a later chapter.
Animations snap back
When an animation ends, the element returns to the values in its normal styles, unless a fill mode keeps the final keyframe. A common bug is an element that fades out and then reappears at full opacity. Adding forwards or both to the animation fixes it.
Overbroad transitions
The transition: all 300ms shorthand is tempting, but it transitions every property that changes, including ones you did not plan for, such as width set by a media query. Name the properties instead, as in the button example above.
Running code when motion ends
Both features fire events when they finish, which lets JavaScript react. A transition fires transitionend once for each property that finished, so check event.propertyName if the element transitions several properties.
const button = document.querySelector('.button');
button.addEventListener('transitionend', (event) => {
if (event.propertyName === 'transform') {
console.log('Transform transition finished');
}
});Animations have the matching animationend event, which reports event.animationName instead. Events are only reliable if the motion actually runs. If the property never changes, or the element is hidden with display: none, no event fires. Do not build logic that waits forever on one.
Motion and user preferences
Choosing the right tool also means making the motion optional. Some users get discomfort or nausea from large movement and set a system preference for reduced motion. You can honor it with a media query, which works for transitions and animations alike.
@media (prefers-reduced-motion: reduce) {
.badge {
animation: none;
}
.button {
transition: none;
}
}Infinite and large-scale animations are the biggest concern, so a looping effect like pulse should always have a reduced-motion rule.
Summary of the decision
- Use a transition for two-state feedback that reverses cleanly: hover, focus, toggles, expanding panels.
- Use a keyframe animation for multi-step, looping, or self-starting effects: spinners, attention cues, entrance sequences.
- Keep
transitionon the base rule, name the properties, and add a fill mode when an animation's end state must stay.
Exercise
A notification icon should shake three times when the page loads, with no user action. Should you use a transition or a keyframe animation, and why?
Show answer
Use a keyframe animation. The effect needs no trigger, it has multiple steps (left, right, back), and it repeats a set number of times. A transition needs a state change to start and has only a start and an end value, so it cannot do any of these.
Exercise
A link has a:hover { color: red; transition: color 300ms; } and no transition on the base a rule. What happens when the pointer enters and leaves the link?
Show answer
The color fades to red over 300ms when the pointer enters, because the hover style declares the transition. When the pointer leaves, the base style has no transition, so the color snaps back to its original value immediately. Moving the transition declaration to the base a rule fixes it.