Skip to the content

Lesson 24 of 296

Transitions and animations

What you will learn

  • The difference between transitions and animations
  • How to write a transition
  • transform: moving, scaling and rotating
  • Which properties animate smoothly and which are slow
  • Keyframe animations
  • Animation and accessibility
On this page

Transitions

A transition smoothly changes a property from one value to another when something changes, for example on hover.

.button {
  background: #1f5fbf;
  transition: background-color 200ms ease;
}
.button:hover {
  background: #174a96;
}

Instead of jumping, the colour fades over 200 milliseconds.

The four parts of transition:

PartExampleMeaning
Propertybackground-colorWhat to animate. all is possible but less efficient.
Duration200ms or 0.2sHow long it takes
Timing functionease, linear, ease-in, ease-out, ease-in-outHow the speed changes during the transition
Delay100msWait before starting (optional)

Several properties: transition: opacity 200ms ease, transform 300ms ease-out;

Put the transition on the normal state, not only on :hover. Then it animates both in and out.

Good durations: most interface animations feel right between 150ms and 300ms. Longer feels slow.

transform

transform changes how an element is drawn, without changing the layout around it.

FunctionExampleEffect
translate()translate(10px, -5px)Moves it
scale()scale(1.05)Makes it bigger or smaller
rotate()rotate(45deg)Turns it
skew()skew(10deg)Slants it
.card {
  transition: transform 200ms ease;
}
.card:hover {
  transform: translateY(-4px) scale(1.02);
}

The card lifts slightly when hovered.

What to animate: performance

Some properties are cheap for the browser to animate; others force it to recalculate the whole layout on every frame, which makes the animation jerky.

Cheap and smoothExpensive (avoid animating)
transformwidth, height
opacitytop, left, margin, padding
font-size

Rule: animate transform and opacity whenever possible. To move something, use translate(), not left. To grow something, use scale(), not width.

This is a common interview question: "Why should you animate transform instead of left?" Answer: changing left makes the browser redo layout and paint on every frame; transform can be handled by the graphics card without changing the layout.

Keyframe animations

A transition needs a change to trigger it. An animation can run by itself, repeat, and have many steps.

@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

.loader {
  animation: spin 1s linear infinite;
}

Steps with percentages:

@keyframes pulse {
  0%   { opacity: 1; }
  50%  { opacity: 0.4; }
  100% { opacity: 1; }
}

Animation properties

PropertyExampleMeaning
animation-namespinWhich @keyframes to use
animation-duration1sLength of one cycle
animation-timing-functionlinearSpeed curve
animation-delay0.5sWait before starting
animation-iteration-count3 or infiniteHow many times
animation-directionalternateGo forwards then backwards
animation-fill-modeforwardsKeep the last frame's styles when finished

Transition or animation?

Use a transition whenUse an animation when
A state changes: hover, focus, open/closedIt should run by itself, such as a loading spinner
There are two states, start and endThere are several steps
It runs once per changeIt repeats

Animation and accessibility

  • Always respect prefers-reduced-motion (Lesson 23). Large movement can make some people feel sick.
  • Never flash content more than three times per second.
  • Animation should help users understand what changed, not only decorate.
  • Do not make users wait for long animations before they can act.

Common mistakes

  • transition: all on many elements, which animates things you did not mean to.
  • Animating width, height, top or left, which causes jerky movement.
  • Transitions only on :hover, so the effect snaps back instantly.
  • Ignoring reduced-motion settings.
  • Durations of 1 second or more for simple interface changes.

How to say it in an interview

"Transitions animate between two states when a property changes, like on hover. Keyframe animations run on their own with multiple steps and can loop. For performance I animate only transform and opacity, because changing layout properties like width or left forces layout recalculation on every frame. And I respect prefers-reduced-motion for accessibility."

Key words

WordMeaning
TransitionA smooth change between two values
Timing functionThe speed curve of a transition, such as ease
transformMoves, scales or rotates an element without changing layout
@keyframesDefines the steps of an animation
AnimationA keyframe sequence that can run and repeat by itself
Reduced motionA user setting asking for less animation

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.