Sub topic 12
Respecting prefers-reduced-motion
This chapter assumes you can write CSS transitions and @keyframes animations, you know how selectors and the cascade decide which rule wins, and you can toggle a class with JavaScript. It covers how to detect a user's motion preference, how to reduce motion without breaking your interface, and the mistakes that most often undo the work.
Why motion needs an off switch
Motion is not neutral for everyone. Large movement, parallax, zooming and spinning can trigger dizziness, nausea, headaches or migraines in people with vestibular disorders. Constant movement also makes it hard for some people to concentrate on text. Operating systems let users say "I want less motion", and browsers pass that choice to your CSS.
Respecting it is part of accessibility. WCAG 2.2 success criterion 2.3.3, Animation from Interactions, asks that motion triggered by user interaction can be disabled, unless it is essential. The media query described below is the standard way to meet it.
The prefers-reduced-motion media query
prefers-reduced-motion is a media feature. It reports whether the user has asked the operating system to minimize non-essential motion. It has two values:
| Value | Meaning |
|---|---|
| `no-preference` | The user has not expressed a preference. |
| `reduce` | The user wants less motion. |
Users turn the setting on in system preferences. Examples are "Reduce motion" on macOS and iOS, "Show animations" off on Windows, and "Remove animations" on Android. Browsers read that setting and expose it to your stylesheet.
The simplest use is to switch motion off, as in the topic's example:
@media (prefers-reduced-motion: reduce) {
.badge {
animation: none;
}
.button {
transition: none;
}
}With this rule, a user who asked for reduced motion sees the badge sit still, and the button change color and position instantly on hover. The state change still happens. Only the travel between states is removed.
Two strategies: opt out or opt in
You can write the query in two directions.
Opt out (reduce on request). Write your normal motion first, then override it inside @media (prefers-reduced-motion: reduce). This is what the example above does. It is easy to add to an existing project, but every new animation needs a matching override. If someone forgets, the animation plays for everyone.
Opt in (motion only on no-preference). Put motion inside @media (prefers-reduced-motion: no-preference). Without that match, no motion exists.
.card {
opacity: 1;
}
@media (prefers-reduced-motion: no-preference) {
.card {
animation: fade-in 400ms ease-out both;
}
}The opt-in approach is safer by default. A browser that does not support the query also skips the block, so users on old browsers get no motion. Most current browsers support the query, so this rarely matters in practice. Choose opt-in for new code. Choose opt-out when you are retrofitting a large stylesheet and need a single place to override.
Reduce, do not always remove
"Reduced" does not have to mean "none". The aim is to remove motion that can cause harm and keep the feedback that helps people understand the interface.
Motion that is most likely to cause problems:
- Large movement across the screen, such as slides that travel a long distance.
- Zooming and scaling of big areas.
- Spinning and rotation.
- Parallax, where layers move at different speeds during scroll.
- Looping animation that never stops.
Motion that is usually acceptable in reduced mode:
- Short fades of opacity.
- Small color changes.
- Subtle changes that do not move content.
A common pattern replaces movement with a fade. The card below slides and fades for most users, and only fades for users who asked for less motion.
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes slide-fade-in {
from {
opacity: 0;
transform: translateY(24px);
}
to {
opacity: 1;
transform: none;
}
}
.card {
animation: fade-in 200ms ease-out both;
}
@media (prefers-reduced-motion: no-preference) {
.card {
animation: slide-fade-in 400ms ease-out both;
}
}The base rule is the reduced version. Only users with no-preference receive the sliding version. The cascade does the work here: both selectors have equal specificity, and the later rule wins when the media query matches.
Setting durations globally
Some teams add a blanket rule that shortens everything. It is a useful safety net, but it is a blunt tool.
@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 sets durations to a tiny value instead of none. The reason is events. If you set animation: none, the animation never runs, so animationend never fires. Any JavaScript or animation-fill-mode logic that depends on the animation finishing may break. A near-zero duration still completes, so events fire and fill modes apply.
The rule also sets scroll-behavior: auto, which turns off smooth scrolling. Smooth scrolling is motion too.
Treat this as a fallback and not as your design. It uses !important, it affects third-party widgets, and it cannot replace motion with something better, such as a fade.
Handling cases that CSS alone cannot cover
Animations that are essential
Some motion carries meaning. A progress indicator or a loading spinner tells the user that work is in progress. If you remove it, the page can look frozen. For these, reduce the intensity and keep a signal. Options include slowing a spinner, replacing a rotation with a pulse of opacity, or showing text such as "Loading".
Reading the preference in JavaScript
JavaScript animation, including the Web Animations API, is not controlled by your CSS media query. Read the preference with matchMedia:
const query = window.matchMedia('(prefers-reduced-motion: reduce)');
function playEntrance(card) {
if (query.matches) {
card.animate(
[{ opacity: 0 }, { opacity: 1 }],
{ duration: 150, easing: 'ease-out', fill: 'both' }
);
} else {
card.animate(
[
{ opacity: 0, transform: 'translateY(24px)' },
{ opacity: 1, transform: 'none' }
],
{ duration: 400, easing: 'ease-out', fill: 'both' }
);
}
}
query.addEventListener('change', () => {
console.log('Reduced motion is now', query.matches ? 'on' : 'off');
});
playEntrance(document.querySelector('.card'));The change event matters because users can flip the setting while your page is open. Do not read the value once at load and assume it stays the same. For the other direction, a CSS @media rule already updates live without any code.
Autoplaying and looping content
Videos, animated GIFs and carousels that start by themselves are outside the CSS query, but the same principle applies. When query.matches is true, do not autoplay. Show a still frame and a visible play button, and give users a way to pause anything that runs longer than a few seconds.
Common errors
- Using
animation: nonewhere an event depends on the animation. Code waiting foranimationendnever continues. Use a near-zero duration, or write the logic so it works without the event. - Reducing only transitions. Teams often cover
transitionbut forgetanimation, scroll effects and JavaScript-driven motion. Search for all of them. - Removing state feedback along with motion. Hover and focus styles must still show. Remove the movement, not the color change or outline.
- Hiding content in the animation's start state. If a card begins at
opacity: 0and relies on an animation to become visible, then removing the animation can leave it invisible. Usebothfill mode carefully, and make sure the final state is the one that applies when animation is off. - Testing only on your own machine. Your setting is probably
no-preference, so the reduced branch never runs. - Treating the setting as a performance option. It expresses a user need. Do not ignore it because the animation is cheap or "only a little movement".
Testing the preference
You do not need to change your operating system to test. Browser developer tools can emulate the setting. In Chrome and Edge, open the Rendering panel from the command menu and set "Emulate CSS media feature prefers-reduced-motion" to reduce. Firefox offers a similar toggle in the Inspector's style panel, which has a button for print and color-scheme simulation. Safari lets you do this through the Develop menu's responsive design options. After switching, reload and check three things:
- Large movement and looping motion are gone.
- Interactive elements still show clear hover, focus and active states.
- No content is stuck invisible or in the wrong position.
You can also test the real setting at least once on a physical device, since emulation does not cover every browser path.
Exercise
A modal uses animation: slide-in 500ms ease-out both and a script waits for the animationend event before it focuses the first form field. A developer adds @media (prefers-reduced-motion: reduce) { .modal { animation: none; } }. Users with reduced motion report that the form field never gets focus. What causes it, and how would you fix it?
Show answer
With animation: none, the animation never runs, so animationend never fires and the script never focuses the field. Fix it by keeping the animation but replacing it with a short opacity-only version, for example animation: fade-in 1ms both, or by changing the script so it focuses the field immediately when matchMedia('(prefers-reduced-motion: reduce)').matches is true instead of waiting for the event.
Exercise
Rewrite this rule so that it follows the opt-in strategy: .menu { transition: transform 300ms ease-out; }.
Show answer
Put the transition inside a no-preference query: @media (prefers-reduced-motion: no-preference) { .menu { transition: transform 300ms ease-out; } }. Without a match, the menu changes position instantly.