Lesson 20 of 23

In progress

Course progress

HTML and CSS

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 transform and opacity instead of layout properties.
  • Disable motion for people who ask for it.

The four parts

HTML
Editable. Runs in your browser.





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.

HTML
Editable. Runs in your browser.





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.

HTML
Editable. Runs in your browser.





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. Use opacity and visibility, or the newer transition-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-change everywhere. 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
HTML
Editable. Runs in your browser.





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 transform and opacity; avoid layout properties and transition: 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?
The property may not be animatable, the start and end values may be identical, or the element may be re-created by JavaScript on each change. Check the computed styles in the Elements panel.
Should I animate on page load?
A short fade or slide can help orientation, but content that animates in on every visit becomes tedious. Never hide essential content behind an animation that might not run.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.