In this lesson you will
- Write transitions with a property, duration, easing and delay
- Move, resize and rotate elements with transform without shifting the layout
- Build a keyframe animation and control how often and how it repeats
- Respect reduced motion settings and the WCAG rules on moving and flashing content
Transitions: smooth changes between two states
A transition runs when a property value changes, usually because of a pseudo-class such as :hover, :focus-visible or :checked, or because JavaScript added a class. You put the transition on the normal state, not the hover state, so the change is smooth in both directions. The shorthand takes the property, the duration, an optional easing function and an optional delay: transition: background-color 200ms ease-out 0s. List several properties with commas.
Easing decides how the speed changes during the transition. ease is the default and starts quickly then slows; ease-out slows at the end and suits things appearing; ease-in speeds up and suits things leaving; linear keeps a constant speed, which looks unnatural for movement but fine for colour. cubic-bezier() lets you design your own curve. Keep durations short: most interface feedback feels right between about 150 and 300 milliseconds, and anything near a second makes people wait.
Only properties with values in between can transition: colours, lengths, percentages, opacity and transforms. display: none to display: block jumps at once. The traditional way to fade something in is to animate opacity and visibility together. Avoid transition: all: it also animates properties you did not mean to, such as a width change when the window is resized, and it makes the browser watch every property.
Console
- Console messages, errors, form data and clicked links appear here.
Point at the button, then press Tab to reach it with the keyboard. Both get the same lift, and the keyboard also gets a clear outline. Change 200ms to 2s to see why long durations feel slow.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
What is the difference between a CSS transition and a CSS animation?
A transition animates a change from one value to another when something triggers it, such as hover or a new class. An animation uses @keyframes, can have many steps, can start on its own and can repeat. Use transitions for interface feedback and animations for loaders or attention cues.
How long should a CSS transition be?
For buttons, links and small changes, roughly 150 to 300 milliseconds feels responsive without making people wait. Larger movements, such as a panel sliding in, can take a little longer. If you notice yourself waiting for it, it is too long.
Why does my animation look jerky on phones?
Usually because it animates layout properties such as width, height, top, left or margin. Rewrite it with transform and opacity, animate fewer elements at once, and check it in the browser’s performance panel with CPU slowdown turned on.
What is prefers-reduced-motion in CSS?
It is a media feature that is true when the person has asked their operating system to reduce or remove animations. Use @media (prefers-reduced-motion: reduce) to turn off non-essential motion for them. It works in all current major browsers.
Can I animate an element from display: none?
Not with a plain transition, because display has no values in between. The dependable method is to keep the element displayed and transition opacity and visibility. Newer browsers add transition-behavior: allow-discrete for this, but check support before relying on it.
Sources
Facts in this lesson were checked against these official pages on 5 November 2026.
- MDN: Using CSS transitions
- MDN: Using CSS animations
- MDN: transform
- MDN: prefers-reduced-motion
- web.dev: How to create high-performance CSS animations
- W3C WAI: Understanding WCAG 2.2 Pause, Stop, Hide (2.2.2)
- W3C WAI: Understanding WCAG 2.2 Three Flashes or Below Threshold (2.3.1)
- W3C WAI: Understanding WCAG 2.2 Animation from Interactions (2.3.3)
- W3C: CSS Transitions Level 1
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.