Sub topic 1
Transitions or Animations: Choosing the Right Tool
Prerequisites
This chapter assumes you know basic CSS selectors and the cascade. You should also know common properties such as color, opacity and background. You should be comfortable with pseudo-classes such as :hover and :focus-visible, and with toggling a class in JavaScript.
Two ways to change a value over time
CSS can change a property value gradually instead of instantly. It offers two mechanisms for this: transitions and keyframe animations.
A transition moves a property from its old value to a new value when something changes the value. The change can come from a state such as :hover or from a class toggled by JavaScript. You write only the end state. The browser works out the in-between frames.
A keyframe animation runs on its own schedule. You describe the steps in an @keyframes rule and attach it to an element with the animation properties. It needs no trigger, so it can start when the page loads. It can have many steps, repeat, and run in reverse.
A transition in practice
This button changes color and moves up slightly when hovered or focused. When the state ends, it transitions back.
.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 sits on the base rule, not on the :hover rule. That placement makes the return trip animate too. If you put it only on :hover, the button would ease in but snap back.
An animation in practice
This badge pulses forever. Nothing triggers it, and it has a middle step that a transition cannot express.
@keyframes pulse {
0%, 100% {
transform: scale(1);
}
50% {
transform: scale(1.1);
}
}
.badge {
animation: pulse 1.5s ease-in-out infinite;
}How they differ
| Question | Transition | Keyframe animation |
|---|---|---|
| What starts it? | A change in a property value | The rule applying, with no trigger needed |
| How many steps? | Two: start and end | Any number |
| Can it repeat? | No | Yes, with `animation-iteration-count` |
| Can it reverse by itself? | Yes, when the state ends | Only with `animation-direction` |
| Where is the code? | One property on the element | A separate `@keyframes` rule plus properties |
A rule of thumb
Use a transition when the interface reacts to the user: hover, focus, open and close, selected and unselected. The browser handles interruption well. If the user moves the pointer away halfway through, the property turns around from its current value.
Use a keyframe animation when the motion has its own timeline. Examples are loading spinners, attention pulses, and entry effects that play once on load.
When both would work, start with the transition. It is less code, and it has less to go wrong.
Common mistakes
- Expecting a transition to run on first render. A transition needs a change. An element that appears with its final style has nothing to move from. Use an animation, or
@starting-style, which a later chapter covers. - Putting
transitiononly on the end state. The motion then plays in one direction only. - Using
transition: all. It animates every property that changes, including ones you did not plan for. Name the properties instead. - Animating properties that cannot interpolate. Some values, such as
display, do not have in-between values. A later chapter on animatable properties explains this.
Exercise
A menu item should fade to a darker background when the pointer is over it. A separate notification dot should pulse on a loop as soon as the page loads. Which mechanism fits each, and why?
Show answer
Use a transition for the menu item, because the effect responds to a state change (:hover) and has only a start and an end value. Use a keyframe animation for the notification dot, because it must start without a trigger and repeat, which transitions cannot do.
Exercise
A button eases to a new color when hovered, but snaps back instantly when the pointer leaves. The transition property is written inside the .button:hover rule. What is the fix?
Show answer
Move the transition declaration to the base .button rule. The transition that applies is taken from the element's style after the change. When the pointer leaves, the :hover rule no longer matches, so its transition no longer applies.