DEV Community

Abdulqudus Abubakre
Abdulqudus Abubakre

Posted on

4 2 2

Building Accessible Animations with prefers-reduced-motion

Animations can be beautiful, but some animations can also be triggering for some users, and that's why electronic devices provide ways to turn off animations, e.g:

  • Android => Settings > Accessibility > Remove Animations
  • iOS => Settings > Accessibility > Motion
  • Windows 10 => Settings > Ease of Access > Display > Show animations in Windows
  • Windows 11 => Settings > Accessibility > Visual Effects > Animation Effects
  • MacOS => System Preferences > Accessibility > Display > Reduce motion

To ensure the animations in our web application respect these settings, we need to make use of the prefers-reduced-motion CSS feature to detect the user settings and carry out appropriate actions. In the example below, we have a simple button that by default has a pulse animation;

<button class="btn btn--animated">Fancy Animation</button>
Enter fullscreen mode Exit fullscreen mode
@keyframes pulse {
  50% {
    transform: scale(1.2);  
  }

  to {
    transform: scale(1);
  }
}

.btn {
  background: #577FAE;
  color: #FFF;
  padding: 10px 20px;
  margin: 30px;
  border: none;
}

.btn--animated {
  animation: pulse 1s linear infinite;
}
Enter fullscreen mode Exit fullscreen mode

To disable any animation if the user has it turned off using the prefers-reduced-motion feature, we can add the following block to our CSS;

@media (prefers-reduced-motion) {
  .btn--animated {
    animation: none;
  }
}
Enter fullscreen mode Exit fullscreen mode

And that's it, with just a few lines of code, we're able to make our animated button accessible, which is sufficient to pass the WCAG Criterion 2.3.3

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️