How to respect prefers-reduced-motion: sort every animation, in CSS and JavaScript


The global prefers-reduced-motion rule that developers copy from project to project leaves script-driven animations running. The prefers-reduced-motion media query detects a device setting that asks for less motion, which people turn on when movement makes them sick or distracts them. Each effect therefore gets its own fix: motion written under no-preference in CSS, a matchMedia check before every script starts, and Chrome's emulation to test it all.
Sorting each effect, from CSS to script
#What does the global prefers-reduced-motion rule let through?
The global rule shortens CSS animations and transitions to 0.01 ms, but it leaves their delays untouched, along with everything a script animates.
prefers-reduced-motion is a media query, a CSS condition that reads a device setting. Its value is reduce when someone has asked their system to minimize non-essential motion, and no-preference when they have made no preference known. The setting is there for people with vestibular disorders, which affect the inner ear. For them, an element that moves as the page scrolls can bring on dizziness, nausea and headaches, even migraines. It also helps some people with attention deficits, who find constant movement distracting.
The global rule fits in a few lines:
@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;
}
}The 0.01 ms value is deliberate. Without a delay, a zero-second transition never starts, so its end event, transitionend, never fires. A script that waits for that event to close a menu waits forever. At 0.01 ms, the event fires.
#How to test prefers-reduced-motion in Chrome DevTools?
Chrome can emulate the media query, simulating the setting for the page without touching the computer's own. The test takes a few clicks:
- On the page you want to test, open DevTools.
- Press Cmd+Shift+P on a Mac or Ctrl+Shift+P on Windows and Linux, type “Show Rendering”, then press Enter: the Rendering tab opens.
- In the “Emulate CSS media feature prefers-reduced-motion” list, choose
prefers-reduced-motion: reduce. - Once the test is over, switch back to “No emulation”.
Marker 1. The Emulate CSS media feature prefers-reduced-motion list, set to prefers-reduced-motion: reduce.Faster still, type “reduce” in the menu from step 2, then choose “Emulate CSS prefers-reduced-motion: reduce”.
#What happens to an animated home page under the global rule?
Under reduce, an accessible page removes or replaces every non-essential motion. We checked each effect on the Spindrift Hotel home page, our demo, under reduce with the global rule in place:
- The banner parallax. The lighthouse photo still scrolls at half the speed of the headline, moved by a script.
- The carousel. It still changes photos every 6 seconds, and the script's
element.animate()still slides each one in over 1,000 ms. - The room cards. They appear without sliding, but the second and third still wait 0.2 and 0.4 s.
- The loading spinner. It makes a single turn lasting 0.01 ms, then freezes.
The rule shortened every CSS duration alike. What still moves comes from a script or a delay.
#Which animations to cut or replace when prefers-reduced-motion is reduce?
Anything that moves, enlarges or spins an element gets cut or replaced. A fade in place, which changes neither the element's size nor its position, doesn't count as motion animation under WCAG, the web accessibility guidelines.
As MDN points out, people who turn the setting on expect motion animation to be switched off unless it's essential. Decorative motion goes. Motion that carries information is replaced by a simpler animation or another visual cue, since without one the interface can become confusing, even unusable. Scrolling the page, which the person controls, stays. On the demo, that means three verdicts:
- Cut the parallax. The W3C, which writes WCAG, lists parallax among animations that are often non-essential. Without it, the photo scrolls with the page.
- Replace the cards' slide and the spinner's rotation. The cards appear with a 0.3 s fade in place, with no delay, and the spinner pulses without turning. Frozen, it could make the page look stuck. The W3C accepts a loading animation as essential when nobody can interact during the wait. Showing no progress must also risk confusing people or making them think the content is frozen or broken.
- Stop the carousel's automatic rotation, and keep its previous and next buttons.
#How to write animations in CSS with no-preference?
The W3C's C39 technique describes two ways to write them. One cuts the motion under reduce. The other writes the still state first and adds the motion only under no-preference, when the person hasn't turned the setting on. We recommend the second. With the first, every motion property has to be undone one by one, and any property left out keeps moving.
Once every effect is sorted, remove the global rule. On the demo, we put it back to check: the cards pop in all at once and the spinner freezes, because the rule spares neither the fade nor the pulse. Smooth scrolling goes under no-preference too, as in the skip link guide, since the global rule no longer resets it to auto.
Avoid
Marker 1. The third card's slot, still empty..card { opacity: 0; transform: translateY(2rem); transition: opacity 0.6s, transform 0.6s; }
.card:nth-child(2) { transition-delay: 0.2s; }
.card:nth-child(3) { transition-delay: 0.4s; }
.card.visible { opacity: 1; transform: none; }
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { transition-duration: 0.01ms !important; /* … */ }
}Correct
Marker 1. The three room cards, shown together..card { opacity: 0; transition: opacity 0.3s; }
.card.visible { opacity: 1; }
.spinner { animation: pulse 1.2s ease-in-out infinite alternate; }
@keyframes pulse { from { opacity: 1; } to { opacity: 0.4; } }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: no-preference) {
.card { transform: translateY(2rem); transition: opacity 0.6s, transform 0.6s; }
.card:nth-child(2) { transition-delay: 0.2s; }
.card:nth-child(3) { transition-delay: 0.4s; }
.card.visible { transform: none; }
.spinner { animation: spin 1s linear infinite; }
html { scroll-behavior: smooth; }
}#How to check prefers-reduced-motion in JavaScript with matchMedia?
A script reads the preference with matchMedia() before starting its animation, and the change event tells it when the setting flips in the middle of a visit. This check, the guard, follows the W3C technique for scripts. On the demo, it never restarts a carousel the visitor has paused.
A script that calls scrollIntoView({ behavior: "smooth" }), or scrollTo() with the same option, requests a smooth scroll that overrides the CSS, global rule included. Only a call with no option, or with the value auto, follows the CSS. Put these calls behind the guard too.
Avoid
Marker 1. The carousel counter, on 2 / 4.setInterval(nextPhoto, 6000);
window.addEventListener("scroll", movePhoto);Correct
Marker 1. The carousel counter, still on 1 / 4.const motionOK = window.matchMedia("(prefers-reduced-motion: no-preference)");
function followPreference() {
if (motionOK.matches) {
startParallax();
startCarousel(); // unless the visitor paused it
} else {
stopParallax();
stopCarousel(); // stays on the current photo
}
}
followPreference();
// the older addListener() method is deprecated
motionOK.addEventListener("change", followPreference);We tested the guard in Chrome with the page open. Switching the emulation to reduce stops the carousel and the parallax at once, and “No emulation” starts the carousel again. The change event fires when the emulation flips, just as it does for a visitor who changes the setting mid-visit. A guard that only reads the preference on load would stay stuck in its starting state.
#Why does the carousel still need a pause button with matchMedia?
Under reduce, the demo carousel no longer moves, and the case looks settled. The guard only stops it for visitors who turned the setting on, though, while WCAG asks that every visitor be able to control it.
The carousel starts on its own, rotates for more than five seconds and sits alongside other content. It therefore falls under WCAG success criterion 2.2.2, Pause, Stop, Hide, at level A, the minimum. There must be a way to pause, stop or hide it, unless the motion is part of an activity where it is essential. In the European Union, the EN 301 549 standard requires every web page to meet this criterion. It was written as one voluntary way to meet the European Accessibility Act.
Our guard and CSS do follow W3C techniques. The W3C, however, counts them as sufficient only for another criterion, Animation from Interactions, at level AAA, the highest.
We see two compliant options and recommend the first when the client agrees:
- A carousel that never rotates on its own. There is no motion to pause and no button to maintain. The rotation the site wanted goes away.
- Automatic rotation with its pause button, as on the demo. The rotation stays, at the cost of one more component to test.
WCAG vs EAA: what each one does, and which one is mandatory for an online store
#How should the carousel's pause button work?
This button changes how the page looks, and that's intended: only a visible control helps the visitor who never touched the setting. It follows a few rules taken from the W3C carousel pattern and its example:
- a visible button, placed before the photos, stops and restarts the rotation, from the keyboard as well as with the mouse;
- the rotation stops when keyboard focus enters the carousel and resumes only when the person asks. This stop never replaces the button, since assistive technologies can run in a mode that moves neither focus nor the mouse;
- it also stops while the mouse hovers over the carousel.
Marker 1. The button that pauses the carousel's rotation.In Chrome, under reduce, no script moves the corrected demo anymore. The carousel waits on its photo and the banner photo scrolls with the page, while the cards fade in and the spinner pulses. Without emulation, the carousel starts again, and its button stops it for every visitor.
Focus visible: how to style the focus indicator in CSS


