CSS prefers-reduced-motion Media Query
Reduced motion without JavaScript detection
Respecting a user's reduced motion preference required JavaScript to check window.matchMedia and conditionally skip animations. The prefers-reduced-motion media query lets CSS respond directly to the OS accessibility setting.
The old hack vs the modern fix
View demo// JS: check OS preference, disable animations manually
const mq = window.matchMedia('(prefers-reduced-motion: reduce)');
if (mq.matches) {
document.querySelectorAll('.animated').forEach(
el => el.style.animation = 'none'
);
}@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
}
}<!-- motion-safe: only applies when user has NOT requested reduced motion --><div class="motion-safe:animate-bounce">Animated</div><!-- motion-reduce: override for reduced-motion users --><div class="animate-spin motion-reduce:animate-none">Spinner</div>Prefers reduced motion browser support
Live demo
Why the modern way wins
Accessibility without JavaScript
Users who experience motion sickness or vestibular disorders can set reduced motion in their OS. This media query lets CSS respond directly, with no JavaScript needed.
Instant, no flash of animation
JavaScript runs after page load, so animations can briefly play before being disabled. CSS applies before paint, so reduced-motion users never see the animation at all.
Reduce, not necessarily remove
Use 0.01ms instead of 0 so transitions still fire transitionend. A 0s transition never starts. Or slow animations down with a longer duration rather than disabling them entirely.
- Old Approach
- JS matchMedia check
- Runs after paint, animations flash briefly
- Modern Approach
- prefers-reduced-motion
- CSS applies before paint, zero flash
- OS Setting
- Accessibility
- Reflects system-level preference automatically
How it works
Animations and transitions can cause problems for users with vestibular disorders or motion sensitivity. The traditional approach was to check window.matchMedia('(prefers-reduced-motion: reduce)') in JavaScript and remove animation classes. Because JavaScript runs after the page renders, animated elements could briefly play before being stopped.
@media (prefers-reduced-motion: reduce) applies before paint. Styles inside are active immediately when the OS setting is enabled. Setting animation-duration and transition-duration to 0.01ms effectively disables motion. The tiny value matters for transitions: a 0s transition never starts, so transitionend never fires. Animations fire animationend either way.