Skip to content

CSS Transitions, Transforms and Keyframe Animations (with reduced motion)

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

A button that darkens instantly when you point at it feels mechanical. The same change spread over a fifth of a second feels responsive and finished. Motion in CSS is small and cheap to add, and done well it tells people what happened: this button is pressed, this menu opened from here, this message is new. Done badly it slows people down, drains phone batteries and can make some people feel physically sick. This lesson teaches both halves: how to animate, and when to hold back.

Quick answer

Use transition to animate a change between two states, for example transition: background-color 200ms ease, transform 200ms ease on a button with a :hover style. Use transform (translate, scale, rotate) and opacity for movement, because browsers can animate them smoothly without recalculating the layout. For motion that runs by itself, define @keyframes and apply them with animation. Always add @media (prefers-reduced-motion: reduce) to turn non-essential motion off.

25 min read and practice · Content checked on 5 November 2026

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.

Try it: A button with hover and keyboard focus transitions
Code
ResultUpdates as you type
Console
  1. 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.