Lesson 20 of 23
In progress
Transitions that feel immediate
Animate state changes with transitions and transforms, keep them short, and respect users who ask for less motion.
A transition explains what just changed. Done well, it makes an interface feel solid; done badly, it makes every click feel slow. The difference is usually duration and which properties you animate.
What you will learn
- Write a transition with property, duration and easing.
- Animate
transformandopacityinstead of layout properties. - Disable motion for people who ask for it.
The four parts
Editable. Runs in your browser.
<button class="button" type="button">Save changes</button>
Press Run to see the output.
The shorthand reads as property, duration, timing function, and optionally delay. Naming the properties is better than transition: all, which animates things you did not intend and can cause layout thrash.
Durations between 120 and 250 milliseconds feel responsive. Anything above 400ms starts to feel like waiting, unless it is a deliberate page-level animation.
Animate cheap properties
transform and opacity can be handled without recalculating layout, which keeps animation smooth. width, height, top, left and margin force layout work every frame.
Editable. Runs in your browser.
<article class="card">
<h2>Hover the card</h2>
<p>The movement is a transform, not a margin change.</p>
</article>
Press Run to see the output.
If a design calls for a growing element, transform: scale() is cheaper than animating width. It can blur text at large scales, so use it on decorative elements rather than paragraphs.
Easing
ease is a sensible default. ease-out starts fast and settles, which suits elements entering the screen. linear is right for continuous progress bars and wrong for almost everything else. cubic-bezier() lets you tune the curve, and steps() produces discrete movement.
transition-delay is occasionally useful to stagger a list of items entering the screen, but a delay on a button press makes the interface feel broken. Feedback for an action should begin immediately; decoration can wait. A 40-millisecond stagger is plenty for a list, and anything longer reads as lag. If feedback is delayed, the user may start a second action before seeing the result of the first, which is exactly how double form submissions happen in practice.
Do not use bounce or elastic curves on functional UI. A button that wobbles when pressed reads as a toy, and it delays the feedback the user needs.
Focus, not just hover
Transitions should apply to keyboard focus too. If a hover state reveals something, the focus state needs the same treatment.
Editable. Runs in your browser.
<p><a class="link" href="#">A link with a growing underline</a></p>
Press Run to see the output.
:focus-visible keeps the change for keyboard users without showing it after a mouse click. A transition on an outline is usually unnecessary; the ring should appear immediately.
Reduced motion
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
This is a blunt instrument, and it is the right default for a site that uses motion decoratively. Remove transforms and parallax as well, rather than merely shortening them.
What not to animate
- Do not animate
display; it is not interpolable. Useopacityandvisibility, or the newertransition-behavior: allow-discrete. - Do not animate text content changes; the result is unreadable.
- Do not add a transition to every element globally; it makes focus and error states feel laggy.
- Avoid
will-changeeverywhere. It consumes memory and is a hint for a specific, measured problem.
transition: all 0.3s on a card also animates its border, padding and text colour, and any property a later rule changes, including layout-affecting ones. Name the two or three properties you actually want, and the animation stays predictable.
Build a link whose underline grows on hover and focus, a card that lifts 2 pixels, and a reduced-motion rule that removes both effects. Keep every duration under 250 milliseconds.
Show solution
Editable. Runs in your browser.
.card {
padding: 20px;
border: 1px solid #d9d9e3;
transition: transform 0.18s ease;
}
.card:hover { transform: translateY(-2px); }
@media (prefers-reduced-motion: reduce) {
.link, .card { transition: none; }
.card:hover { transform: none; }
}
</style>
<p><a class="link" href="#">Read the lesson</a></p>
<div class="card">A card that lifts slightly.</div>
Press Run to see the output.
Only two properties animate, both cheap, and the reduced-motion query removes the effects rather than shortening them.
Recap
- A transition needs a property, a short duration and a sensible easing curve.
- Animate
transformandopacity; avoid layout properties andtransition: all. - Apply states to focus as well as hover, and disable motion under
prefers-reduced-motion.
Next, you will define colours and spacing once as custom properties, so a theme change is a few lines.
Why did my transition not run?
Should I animate on page load?
Progress is saved in this browser only. No account, no email.