Sub topic 12
Respecting prefers-reduced-motion
This chapter assumes you can write transitions and @keyframes animations, and that you know media queries and the :hover and :focus-visible pseudo-classes.
Why motion needs an off switch
Motion can cause real problems. Large movement, parallax, and looping effects can trigger dizziness, nausea, or migraines for people with vestibular disorders. Others find constant movement distracting. Operating systems let users say they want less motion, and browsers expose that choice to CSS through the prefers-reduced-motion media feature.
The feature has two values:
| Value | Meaning |
|---|---|
| `no-preference` | The user has not asked for reduced motion. |
| `reduce` | The user has asked the system to minimize motion. |
Turning motion off
The most direct approach is to override the motion inside a reduce query. This example removes the pulsing animation from a badge and the transition from a button:
@media (prefers-reduced-motion: reduce) {
.badge {
animation: none;
}
.button {
transition: none;
}
}Place this block after the rules it overrides. The query adds no specificity, so source order decides which rule wins when selectors are equal.
Reduce, don't always remove
Reduced motion does not have to mean no change at all. Users still benefit from feedback such as a color change on hover. What they want to avoid is large movement: sliding, scaling, spinning, and parallax. A good pattern keeps the color transition and drops the movement:
.button {
background-color: #2563eb;
transition: background-color 200ms ease-out, transform 200ms ease-out;
}
.button:hover,
.button:focus-visible {
background-color: #1d4ed8;
transform: translateY(-2px);
}
@media (prefers-reduced-motion: reduce) {
.button {
transition: background-color 200ms ease-out;
}
.button:hover,
.button:focus-visible {
transform: none;
}
}The button still changes color, but it no longer moves. A fade is usually safe, so you can replace a slide-in with a plain fade.
Motion-first or reduced-first
You can write the query in two directions. The first starts with motion and removes it, as above. The second starts without motion and adds it only for users who have not asked to avoid it:
.card {
opacity: 1;
}
@media (prefers-reduced-motion: no-preference) {
.card {
animation: fade-in 400ms ease-out both;
}
}
@keyframes fade-in {
from {
opacity: 0;
transform: translateY(8px);
}
to {
opacity: 1;
transform: none;
}
}The second pattern is safer. If a browser does not support the media feature, it ignores the block, and the user gets no motion.
Common mistakes
- Forgetting JavaScript motion. CSS queries do not affect
Element.animate()or scripted scrolling. Checkwindow.matchMedia('(prefers-reduced-motion: reduce)').matchesbefore starting them. - Hiding content with the animation. If an element starts at
opacity: 0and only the animation reveals it, settinganimation: nonecan leave it invisible. Make sure the final state is the default state. - Relying on
transitionend. Withtransition: none, the event never fires, so code that waits for it can hang. - Using
0sdurations carelessly. Setting a duration of0sstill runs the events, whilenonedoes not. Pick one on purpose.
Testing
Chrome and Firefox developer tools can emulate the preference, so you do not have to change your operating system. In Chrome, open the Rendering panel and set "Emulate CSS media feature prefers-reduced-motion" to reduce. Reload and confirm that large movement is gone and that nothing is stuck invisible.
Exercise
A hero image fades and slides up on load, using an animation that starts at opacity 0. You add animation: none inside a prefers-reduced-motion: reduce query, and now the image never appears for those users. Why, and how do you fix it?
Show answer
The base rule probably sets opacity: 0 and relies on the animation's final keyframe to make the image visible. With animation: none, nothing ever raises the opacity. Fix it by making the visible state the default and applying the animation, with opacity: 0 in its from keyframe, only inside @media (prefers-reduced-motion: no-preference). Alternatively, keep a plain fade and remove only the transform.