Emmanuel Akinfulubi

HTML and CSSPractical III

Respect prefers-reduced-motion in CSS

35 Honouring reduced motion

fair

Animate something, then make it respectful: no movement for a reader who has asked their system for less. Cover transitions and animations, and keep the interface still usable.

Stuck? Three hints, smallest first
  1. 1. @media (prefers-reduced-motion: reduce) matches when the system setting is on.
  2. 2. Turn off animation as well as transition. They are separate mechanisms.
  3. 3. Reduce, do not remove: the state change still has to be visible, just not animated.

index.html

<button class="pulse" type="button">Hover or focus me</button>
<p class="tip">The lift and the pulse both stop when reduced motion is on. The
  colour change does not, because that is the feedback.</p>

style.css

.pulse {
  padding: 10px 18px;
  border: 0;
  border-radius: 8px;
  background: #3055d8;
  color: #ffffff;
  font: inherit;
  cursor: pointer;
  transition: transform 180ms ease, background 180ms ease;
  animation: breathe 2.4s ease-in-out infinite;
}

.pulse:hover,
.pulse:focus-visible {
  transform: translateY(-3px);
  background: #2543b4;      /* the feedback that survives reduced motion */
}

.pulse:focus-visible {
  outline: 2px solid #17171a;
  outline-offset: 3px;
}

@keyframes breathe {
  0%, 100% { box-shadow: 0 0 0 0 rgb(48 85 216 / 45%); }
  50% { box-shadow: 0 0 0 6px rgb(48 85 216 / 0%); }
}

.tip { margin: 10px 0 0; font-size: 0.8rem; color: #5c5c66; max-width: 40ch; }

/* Movement off, feedback intact. */
@media (prefers-reduced-motion: reduce) {
  .pulse {
    transition: none;
    animation: none;
    transform: none;
  }
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 35

const reduced = (/@media \(prefers-reduced-motion: reduce\)\s*\{([\s\S]*?)\n\}/.exec(css) || [, ''])[1];

test('there is a reduced-motion query', /prefers-reduced-motion: reduce/.test(css), true);
test('it turns transitions off', /transition:\s*none/.test(reduced), true);
test('and animations too, since they are separate', /animation:\s*none/.test(reduced), true);
test('and cancels the transform', /transform:\s*none/.test(reduced), true);
test('the colour feedback is NOT inside that query, so it survives', /background/.test(reduced), false);
test('the base state animates transform and background only', /transition:\s*transform[^;]*background/.test(css), true);
test('keyboard focus gets the same treatment as hover', /\.pulse:hover,\s*\.pulse:focus-visible/.test(css), true);
test('a real button element, with an explicit type', /<button class="pulse" type="button">/.test(html), true);

Output


      

The setting exists because motion makes some people genuinely unwell — vestibular disorders, migraine — so this is not a preference in the decorative sense. Two details. transition: none does not stop a @keyframes animation; they are separate mechanisms and both need switching off. And the instruction is reduce, not remove: the fifth check makes sure the colour change stays outside the query, because a button that gives no feedback at all is a different accessibility problem. Nothing here reads the system setting during the build; the assertions check the rule and your own system shows you the behaviour.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.