Skip to the main content

All tutorials

CSS transitions and animations with reduced motion

  • Level: Beginner
  • About 5 minutes to read (an estimate from the word count: 855 words at 200 words a minute)
  • Last checked:

Animate a state change with a transition, loop with a keyframe animation, and switch both off for people who ask for less motion.

What you will be able to do

  • Choose what to animate and name transform and opacity as the safe pair
  • Write a transition that runs for hover and for keyboard focus
  • Write a keyframe loop that only runs when motion is welcome
  • Use prefers-reduced-motion in both the opt-out and the opt-in form
  • Show a change without motion, so nothing depends on it

What to animate

Animation should explain a change, not decorate one. A card lifts when you point at it, so you can see what the pointer is on. A message slides in, so you notice that it arrived. If an effect explains nothing, leave it out.

Choose the properties with care. Animating width, height or margin moves other elements around while it runs. Animating transform and opacity changes how an element is drawn without moving its neighbours. The web.dev guide to animations recommends these two properties, and this tutorial uses only them. It makes no claim about speed, and it has not measured any.

A transition on hover and focus

A transition animates the change between two states. You name the properties and a duration on the element's normal state, and you write the changed values in the state rule. The browser fills in the frames between.

.card {
  transition: transform 180ms ease-out;
}
.card:hover,
.card:focus-within {
  transform: translateY(-4px);
}
The same change for a pointer and for the keyboard.

Write the transition on the normal state so that it plays in both directions, going in and coming back. List the properties you animate, as in the example, and do not use transition: all, which would animate any property that changes later. Keep durations short: 150 to 250 milliseconds is typical for a response to a pointer.

Write the changed state for :focus-within or :focus-visible as well as for :hover. Without it, a keyboard user does not get the feedback that a mouse user gets.

HTML

<section class="tr-demo" aria-labelledby="tr-demo-title">
  <h2 class="tr-demo__title" id="tr-demo-title">A change of state, eased</h2>
  <ul class="tr-demo__list">
    <li class="tr-demo__card">
      <h3>Hover or focus</h3>
      <p>The card lifts with <code>transform</code>. Keyboard focus inside it does the same.</p>
      <a class="tr-demo__link" href="#tr-demo-title">A link inside the card</a>
    </li>
    <li class="tr-demo__card">
      <h3>Second card</h3>
      <p>Only <code>transform</code> and <code>opacity</code> change; nothing else moves.</p>
      <a class="tr-demo__link" href="#tr-demo-title">Another link</a>
    </li>
  </ul>
</section>

CSS

.tr-demo {
  --td-bg: #ffffff;
  --td-fg: #1b1f2a;
  --td-line: #c9ced9;
  --td-card: #f3f4f8;
  --td-accent: #3730a3;
  padding: 1rem;
  color: var(--td-fg);
  background: var(--td-bg);
}
.tr-demo__title { margin: 0 0 1rem; font-size: 1.25rem; }
.tr-demo__list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
  gap: 1rem;
  margin: 0;
  padding: 0;
  list-style: none;
}
.tr-demo__card {
  padding: 1rem;
  background: var(--td-card);
  border: 1px solid var(--td-line);
  border-radius: 8px;
  transition: transform 180ms ease-out, opacity 180ms ease-out;
}
.tr-demo__card h3 { margin: 0 0 0.25rem; font-size: 1.0625rem; }
.tr-demo__card p { margin: 0 0 0.5rem; }
/* The same change for the mouse and for the keyboard. */
.tr-demo__card:hover,
.tr-demo__card:focus-within { transform: translateY(-4px); }
.tr-demo__list:hover .tr-demo__card:not(:hover) { opacity: 0.8; }
.tr-demo__link { color: var(--td-accent); }
.tr-demo__link:focus-visible {
  outline: 3px solid var(--td-accent);
  outline-offset: 2px;
}
/* For a person who asked the system for less motion: the state still changes, with no movement or fading in between. */
@media (prefers-reduced-motion: reduce) {
  .tr-demo__card { transition: none; }
}
@media (prefers-color-scheme: dark) {
  .tr-demo {
    --td-bg: #12141c;
    --td-fg: #eceef5;
    --td-line: #444b5c;
    --td-card: #1d2130;
    --td-accent: #b4b8ff;
  }
}

A keyframe loop

A transition goes from one state to another. A keyframe animation can go through several states and repeat. You define the steps with @keyframes, then attach them with animation and set animation-iteration-count to say how often. A number repeats that many times and infinite repeats without end.

Use loops sparingly. WCAG success criterion 2.2.2 asks for a way to pause, stop or hide moving content that starts by itself, lasts more than five seconds and sits beside other content. There is an exception for movement that is essential, and a loading indicator can count as essential when nothing else can be done during the wait. A loop that only decorates the page does not. If yours decorates, give it a pause button or let it stop after a few cycles. The example below does the second: it runs three times, 4.2 seconds in all, and stops by itself. That is under the five seconds in the criterion, so it needs no pause button. The criterion counts the whole animation, all three runs together, not one run.

prefers-reduced-motion

Some people get discomfort or nausea from motion on screen. Their device has a setting for reduced motion, and the media feature prefers-reduced-motion tells your CSS about it. It has two values: reduce, when the person asked for less motion, and no-preference.

There are two ways to use it. The first removes motion when asked: write the animation as usual, then switch it off inside @media (prefers-reduced-motion: reduce). The first example does this for its transition. The second adds motion only when it is welcome: write the animation inside @media (prefers-reduced-motion: no-preference). If the media feature is unsupported, nobody gets the motion, which is the safer failure. The loop example does this.

@media (prefers-reduced-motion: no-preference) {
  .dot { animation: pulse 1.4s ease-in-out 3; }
}
The motion is opt-in and finite. Without the media feature the dot stays still.

HTML

<div class="loop-demo">
  <span class="loop-demo__dot" aria-hidden="true"></span>
  <p class="loop-demo__text">The dot pulses three times and then stops. This message stays on screen, with or without the pulse.</p>
</div>

CSS

.loop-demo {
  --ld-bg: #ffffff;
  --ld-fg: #1b1f2a;
  --ld-dot: #3730a3;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  max-width: 30rem;
  padding: 1rem;
  color: var(--ld-fg);
  background: var(--ld-bg);
}
.loop-demo__dot {
  flex: none;
  width: 1rem;
  height: 1rem;
  background: var(--ld-dot);
  border-radius: 50%;
}
.loop-demo__text { margin: 0; }
@keyframes loop-demo-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.6); opacity: 0.45; }
}
/* The loop is opt-in: it exists only for people who have not asked for less motion. It runs three times (4.2 seconds)
   and stops by itself, so it never lasts the five seconds that WCAG 2.2.2 counts as long enough to need a pause control. */
@media (prefers-reduced-motion: no-preference) {
  .loop-demo__dot { animation: loop-demo-pulse 1.4s ease-in-out 3; }
}
@media (prefers-color-scheme: dark) {
  .loop-demo {
    --ld-bg: #12141c;
    --ld-fg: #eceef5;
    --ld-dot: #b4b8ff;
  }
}

A transition triggered by a click or a hover is covered by a stricter, optional criterion, 2.3.3, which asks that motion triggered by interaction can be disabled unless it is essential. Switching it off with prefers-reduced-motion is one way to meet it.

What to show instead of motion

Reduced motion does not mean reduced information. If a slide-in tells the visitor that something new has arrived, take away the slide and keep the message. Say it with something that does not move: a label, a thick border, a change of colour that is not the only signal.

The last example shows a notice that slides in when you follow its link. With reduced motion the notice appears in place, and a "New" label and a thick border mark it. The page opens the notice with the :target pseudo-class, so it needs no script.

HTML

<section class="rm-demo" aria-labelledby="rm-demo-title">
  <h2 class="rm-demo__title" id="rm-demo-title">What to show instead of motion</h2>
  <p><a class="rm-demo__link" href="#rm-demo-notice">Show the notice</a></p>
  <div class="rm-demo__notice" id="rm-demo-notice" role="status" tabindex="-1">
    <p class="rm-demo__tag">New</p>
    <p class="rm-demo__text">The notice slides in for most people. With reduced motion it appears in place, and the word New and the thick border show that it just arrived.</p>
  </div>
</section>

CSS

.rm-demo {
  --rm-bg: #ffffff;
  --rm-fg: #1b1f2a;
  --rm-line: #c9ced9;
  --rm-accent: #3730a3;
  --rm-card: #f3f4f8;
  min-height: 14rem;
  padding: 1rem;
  color: var(--rm-fg);
  background: var(--rm-bg);
}
.rm-demo__title { margin: 0 0 0.75rem; font-size: 1.25rem; }
.rm-demo__link { color: var(--rm-accent); }
.rm-demo__link:focus-visible,
.rm-demo__notice:focus-visible {
  outline: 3px solid var(--rm-accent);
  outline-offset: 2px;
}
/* The notice is shown by :target, so it needs no script. It has tabindex="-1" so that, when the link is followed, the browser
   moves keyboard focus to it (the fragment navigation target), and a keyboard user starts reading at the notice. */
.rm-demo__notice {
  display: none;
  max-width: 32rem;
  padding: 0.75rem 1rem;
  background: var(--rm-card);
  border: 1px solid var(--rm-line);
  border-radius: 8px;
}
.rm-demo__notice:target { display: block; }
.rm-demo__tag { display: none; margin: 0 0 0.25rem; font-weight: 700; color: var(--rm-accent); }
.rm-demo__text { margin: 0; }
@keyframes rm-demo-slide {
  from { transform: translateY(1.5rem); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
@media (prefers-reduced-motion: no-preference) {
  .rm-demo__notice:target { animation: rm-demo-slide 300ms ease-out; }
}
/* Instead of movement: a label and a thick border that do not depend on motion. */
@media (prefers-reduced-motion: reduce) {
  .rm-demo__notice:target { border: 3px solid var(--rm-accent); }
  .rm-demo__notice:target .rm-demo__tag { display: block; }
}
@media (prefers-color-scheme: dark) {
  .rm-demo {
    --rm-bg: #12141c;
    --rm-fg: #eceef5;
    --rm-line: #444b5c;
    --rm-accent: #b4b8ff;
    --rm-card: #1d2130;
  }
}

To test your own page, turn on the reduced-motion setting of your operating system, or emulate it in your browser's developer tools. The verification script for this tutorial checks, in Chrome 143, that no animation longer than 10 milliseconds is still running when reduced motion is requested.

All examples on this page passed their checks in Chrome 143 on 3 December 2026. They were not tested in other browsers.

References

These snippets in the gallery use the same techniques: