Lesson 24 of 296
Transitions and animations
What you will learn
- The difference between transitions and animations
- How to write a transition
- transform: moving, scaling and rotating
- Which properties animate smoothly and which are slow
- Keyframe animations
- Animation and accessibility
On this page
Transitions
A transition smoothly changes a property from one value to another when something changes, for example on hover.
.button {
background: #1f5fbf;
transition: background-color 200ms ease;
}
.button:hover {
background: #174a96;
}Instead of jumping, the colour fades over 200 milliseconds.
The four parts of transition:
| Part | Example | Meaning |
|---|---|---|
| Property | background-color | What to animate. all is possible but less efficient. |
| Duration | 200ms or 0.2s | How long it takes |
| Timing function | ease, linear, ease-in, ease-out, ease-in-out | How the speed changes during the transition |
| Delay | 100ms | Wait before starting (optional) |
Several properties: transition: opacity 200ms ease, transform 300ms ease-out;
Put the transition on the normal state, not only on :hover. Then it animates both in and out.
Good durations: most interface animations feel right between 150ms and 300ms. Longer feels slow.
transform
transform changes how an element is drawn, without changing the layout around it.
| Function | Example | Effect |
|---|---|---|
translate() | translate(10px, -5px) | Moves it |
scale() | scale(1.05) | Makes it bigger or smaller |
rotate() | rotate(45deg) | Turns it |
skew() | skew(10deg) | Slants it |
.card {
transition: transform 200ms ease;
}
.card:hover {
transform: translateY(-4px) scale(1.02);
}The card lifts slightly when hovered.
What to animate: performance
Some properties are cheap for the browser to animate; others force it to recalculate the whole layout on every frame, which makes the animation jerky.
| Cheap and smooth | Expensive (avoid animating) |
|---|---|
transform | width, height |
opacity | top, left, margin, padding |
font-size |
Rule: animate transform and opacity whenever possible. To move something, use translate(), not left. To grow something, use scale(), not width.
This is a common interview question: "Why should you animate transform instead of left?" Answer: changing left makes the browser redo layout and paint on every frame; transform can be handled by the graphics card without changing the layout.
Keyframe animations
A transition needs a change to trigger it. An animation can run by itself, repeat, and have many steps.
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.loader {
animation: spin 1s linear infinite;
}Steps with percentages:
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.4; }
100% { opacity: 1; }
}Animation properties
| Property | Example | Meaning |
|---|---|---|
animation-name | spin | Which @keyframes to use |
animation-duration | 1s | Length of one cycle |
animation-timing-function | linear | Speed curve |
animation-delay | 0.5s | Wait before starting |
animation-iteration-count | 3 or infinite | How many times |
animation-direction | alternate | Go forwards then backwards |
animation-fill-mode | forwards | Keep the last frame's styles when finished |
Transition or animation?
| Use a transition when | Use an animation when |
|---|---|
| A state changes: hover, focus, open/closed | It should run by itself, such as a loading spinner |
| There are two states, start and end | There are several steps |
| It runs once per change | It repeats |
Animation and accessibility
- Always respect
prefers-reduced-motion(Lesson 23). Large movement can make some people feel sick. - Never flash content more than three times per second.
- Animation should help users understand what changed, not only decorate.
- Do not make users wait for long animations before they can act.
Common mistakes
transition: allon many elements, which animates things you did not mean to.- Animating
width,height,toporleft, which causes jerky movement. - Transitions only on
:hover, so the effect snaps back instantly. - Ignoring reduced-motion settings.
- Durations of 1 second or more for simple interface changes.
How to say it in an interview
"Transitions animate between two states when a property changes, like on hover. Keyframe animations run on their own with multiple steps and can loop. For performance I animate only transform and opacity, because changing layout properties like width or left forces layout recalculation on every frame. And I respect prefers-reduced-motion for accessibility."
Key words
| Word | Meaning |
|---|---|
| Transition | A smooth change between two values |
| Timing function | The speed curve of a transition, such as ease |
| transform | Moves, scales or rotates an element without changing layout |
| @keyframes | Defines the steps of an animation |
| Animation | A keyframe sequence that can run and repeat by itself |
| Reduced motion | A user setting asking for less animation |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.