Animation Beginner

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.

βœ“ Modern
5 lines
1@media (prefers-reduced-motion: reduce) {
2  *, *::before, *::after {
3    animation-duration: 0.01ms !important;
4  }
5}
βœ• Old JS required
1// JS: check OS preference, disable animations manually
2const mq = window.matchMedia('(prefers-reduced-motion: reduce)');
3if (mq.matches) {
4  document.querySelectorAll('.animated').forEach(
5    el => el.style.animation = 'none'
6  );
7}
Widely available Since 2020 96% global usage

This feature is well established and works across many devices and browser versions. It has been available across browsers since 2020.

74+
63+
10.1+
79+
right ball stops if OS reduced motion is on
Always animates
No OS check
Respects OS
prefers-reduced-motion
@media (prefers-reduced-motion: reduce) β€” pure CSS, no JS
β™Ώ

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 β€” 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 to preserve animation events that JavaScript might listen for. 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 to 0.01ms effectively disables animations while preserving any JavaScript that listens for animationend events β€” using 0 skips the event entirely.

New CSS drops.

Join 400+ readers who've survived clearfix hacks.

ESC