Skip to content

Lesson 22 of 52Modern CSS

Transitions and Animations

Add motion with CSS transitions, transforms and keyframe animations, keep it smooth by animating transform and opacity, and respect reduced motion.

18 min read0/4 questions

Good motion makes an interface feel alive and easier to follow. A button that darkens smoothly when you hover it, a menu that slides in from the side, a spinner that shows something is loading: each one tells you what just happened. Bad motion does the opposite. It feels slow, it stutters, and for some people it causes real nausea. CSS gives you two tools for motion, transitions and keyframe animations, plus transforms to move things around cheaply. This lesson covers all three, how to keep them smooth, and how to switch them off for people who ask for less motion.

Transitions: smooth changes between two states

Without a transition, a style change is instant. Hover a button that changes from blue to dark blue, and it snaps. A transition tells the browser to animate the change over time instead.

.button {
  background: #2a6df4;
  transition: background-color 200ms ease;
}
.button:hover {
  background: #1747a6;
}

The transition shorthand takes up to four parts:

PartExampleMeaning
Propertybackground-colorWhich property to animate
Duration200ms or 0.2sHow long it takes
Timing functionease, linear, ease-outHow the speed changes over that time
Delay100msHow long to wait before starting

Notice that the transition goes on the normal state, not the :hover state. That way it runs in both directions: when the pointer arrives and when it leaves. Put it only on :hover and the colour fades in but snaps back.

You can animate several properties by separating them with commas: transition: background-color 200ms, transform 200ms;. You will also see transition: all 200ms, which animates every property that changes. It is convenient but risky: it can animate things you did not intend, like layout changes, and it makes the browser do more work.

Transforms: moving without touching layout

The transform property changes how an element is drawn without changing the space it takes up in the layout. Neighbours do not move. Common transform functions:

  • translate(20px, 10px) moves it right 20px and down 10px. translateY(-4px) lifts it up.
  • scale(1.1) makes it 10% bigger. scale(0.95) shrinks it slightly, like a pressed button.
  • rotate(45deg) turns it clockwise. turn units work too: rotate(0.5turn) is half a turn.

You can chain them: transform: translateY(-4px) scale(1.02);. Order matters when you mix them: rotate(45deg) translateX(50px) moves along the rotated direction, so it lands somewhere different from translateX(50px) rotate(45deg). Modern browsers also support separate translate, scale and rotate properties, which are handy when you want to change one without rewriting the others.

transform-origin sets the point that scaling and rotation happen around. It defaults to the centre; transform-origin: top left makes a box grow from its corner.

HTML & CSS
<style> .plans { display: flex; gap: 12px; padding: 20px 8px; } .plan { flex: 1; padding: 14px; border: 2px solid #222; border-radius: 10px; background: white; transition: transform 200ms ease-out, box-shadow 200ms ease-out; } .plan:hover { transform: translateY(-6px); box-shadow: 0 8px 18px rgb(0 0 0 / 0.18); } .plan button { transition: transform 100ms; } .plan button:active { transform: scale(0.94); }</style><div class="plans"> <div class="plan"><h4>Starter</h4><p>5 projects</p><button>Choose</button></div> <div class="plan"><h4>Studio</h4><p>Unlimited</p><button>Choose</button></div></div>

Preview

Hover a plan and it lifts; press a button and it squashes. Try changing 200ms to 800ms to feel how slow that is. Then change translateY(-6px) to rotate(-3deg) for a playful tilt, or scale(1.05).

Quick check
A card has transition: transform 300ms on its :hover rule only. What happens when the pointer leaves the card?
Need a nudge? A hint rules out a wrong answer.

Timing functions

A timing function (or easing) controls how speed changes during the motion. Real objects speed up and slow down; things moving at constant speed look mechanical.

  • ease (the default): starts quickly, slows at the end. A safe all-rounder.
  • ease-out: fast start, gentle stop. Good for things entering the screen, because they respond instantly.
  • ease-in: slow start, fast finish. Good for things leaving.
  • ease-in-out: slow at both ends. Good for things moving from one place to another.
  • linear: constant speed. Right for spinners and progress, wrong for most other things.
  • cubic-bezier(0.34, 1.56, 0.64, 1): a custom curve. Values above 1 make it overshoot and settle, a little bounce.
  • steps(4): jumps in four discrete steps, like a flip book.
HTML & CSS
<style> .track { position: relative; height: 34px; margin: 6px 0; background: #f1f1f1; border-radius: 6px; } .dot { position: absolute; top: 5px; left: 5px; width: 24px; height: 24px; border-radius: 50%; background: #e76f51; transition: transform 1.2s; } body.go .dot { transform: translateX(220px); } .a { transition-timing-function: linear; } .b { transition-timing-function: ease-out; } .c { transition-timing-function: ease-in; } .d { transition-timing-function: cubic-bezier(0.34, 1.56, 0.64, 1); } small { font-family: monospace; }</style><button onclick="document.body.classList.toggle('go')">Race</button><small>linear</small><div class="track"><div class="dot a"></div></div><small>ease-out</small><div class="track"><div class="dot b"></div></div><small>ease-in</small><div class="track"><div class="dot c"></div></div><small>overshoot</small><div class="track"><div class="dot d"></div></div>

Preview

Click Race a few times. All four dots take exactly 1.2 seconds, but they feel very different. Change the .a timing to steps(5) and watch it jump. If the dots run off the end in your preview, reduce 220px.

Keyframe animations

Transitions need a trigger, like a hover or a class change, and they only go from A to B. Keyframe animations can run by themselves, loop forever and pass through many steps. You describe the steps in an @keyframes rule and attach it with the animation property.

@keyframes pulse {
  0%   { transform: scale(1);    opacity: 1; }
  50%  { transform: scale(1.15); opacity: 0.6; }
  100% { transform: scale(1);    opacity: 1; }
}
 
.live-dot {
  animation: pulse 1.5s ease-in-out infinite;
}

The animation shorthand bundles several properties: the name, the duration, the timing function, and then options like the iteration count (3 or infinite), the direction (alternate plays it forwards then backwards), a delay, and the fill mode. animation-fill-mode: forwards keeps the final keyframe's styles after the animation ends; without it, the element jumps back to its normal styles. You can use the words from and to instead of 0% and 100%.

HTML & CSS
<style> @keyframes spin { to { transform: rotate(1turn); } } @keyframes slide-in { from { transform: translateY(16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } } .spinner { width: 32px; height: 32px; border: 4px solid #ddd; border-top-color: #2a6df4; border-radius: 50%; animation: spin 0.8s linear infinite; } .toast { margin-top: 16px; padding: 10px 14px; max-width: 220px; background: #1d3557; color: white; border-radius: 8px; animation: slide-in 400ms ease-out 300ms both; }</style><div class="spinner" aria-label="Loading" role="status"></div><div class="toast">Saved to your library</div>

Preview

Change the spinner's linear to ease-in-out and notice it now speeds up and slows down each turn. Change the toast's 300ms delay to 1.5s, or swap translateY(16px) for translateX(-40px) so it slides in from the left. (The both keyword is a fill mode: it applies the first keyframe during the delay and keeps the last one at the end.)

Match the pairs0/5 matched

Match each property or value to what it controls.

Choose an item, then its match on the other side.

Performance: animate transform and opacity

To draw a page, the browser goes through stages. Changing a property can restart the work from an early stage, and doing that 60 times a second is what makes animations stutter.

What the browser redoes for each frame
  1. Stylework out which CSS applies
  2. Layoutsizes and positions of every box
  3. Paintfill in pixels: text, colours, borders
  4. Compositestack the painted layers on screen

Animating width, height, top, left or margin changes layout. Every box after it may move, then everything is repainted and composited. That is the expensive path.

1/3

So the rule is: animate transform and opacity whenever you can. Want something to slide in? Animate translate, not left. Want it to grow? Animate scale, not width. Want it to appear? Animate opacity. You can see the cost for yourself in the Performance panel of your browser's devtools.

The will-change: transform property hints that an element is about to animate so the browser can prepare a layer. Use it sparingly, on a few elements, and only when you have a measured problem; each layer costs memory.

Quick check
A sidebar slides in from the left and stutters on cheap phones. It animates left from -300px to 0. What is the best fix?
Need a nudge? A hint rules out a wrong answer.

Respecting reduced motion

Some people get dizzy or sick from motion on screen, especially large movements, parallax and zooming. Operating systems have a "reduce motion" setting, and CSS can read it with the prefers-reduced-motion media query.

@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 blunt reset is common in starter stylesheets; it is one of the few places !important is reasonable. A more careful approach is to keep motion that helps understanding, like a gentle fade, and remove movement, zooming and looping. Either way, make sure the page still works and makes sense with every animation off. Never hide important content behind an animation that might not run.

Put it in order4 steps

Put the steps for adding a hover lift to a card in a good order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.
Key terms
transition
Animates a property smoothly when its value changes, for example on hover.
transform
Moves, scales or rotates how an element is drawn without changing layout.
timing function
Controls how speed changes during a transition or animation, like ease-out.
@keyframes
A rule that defines the steps of a named animation.
fill mode
Whether an animation's first or last keyframe styles apply before or after it runs.
compositing
The final rendering stage, where painted layers are stacked on screen.
prefers-reduced-motion
A media query that is true when the user asked their system for less motion.