Use Media Queries to Adapt to a User's Motion Preferences

Kayla Sween
InstructorKayla Sween
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

We have three animating sections: one in the header and one for each of the two images. To honor the system preferences of the user, we can use the prefers-reduced-motion media query to turn off those animations. In this example, we'll change the animation-duration of the stretch, spin, and bounce classes to 0.01ms. This will allow us to effectively remove the animations without disrupting any JavaScript events that are watching for animationend. If you use the transition property, you could add transition-duration: 0.01ms to ensure the transitionend event isn't disrupted as well.

You can certainly stop there, but today, I don't want to completely remove all animation from the page. To add a less-obtrusive animation, we can rename the animation. In this example, we'll rename the stretch animation to pulse and allow that to move from half opacity to full opacity over 1.5 seconds.

It's worth noting that the prefers-reduced-motion media query is not compatible with Internet Explorer. If you support Internet Explorer, please consider removing animations that are prone to disrupt those with vestibular disorders and learning disabilities. If you're not sure if an animation would be distressing to someone, it's best option may be to remove it entirely.

Instructor: [0:00] We're going to start with three classes that are animating. Our header box is stretching. One image is spinning, and one image is bouncing. If we move over to our styles.css, we can add the media query prefers-reduced-motion and set that to reduce.

[0:21] Then we'll add our three classes that are animating -- so, our stretch class, our spin class, and our bounce class. We'll set the animation duration to .01 milliseconds. This allows us to effectively remove animations without disrupting any JavaScript events that are watching for animationend.

[0:41] We'll also add animation-iteration-count and set that to 1 to make sure that our animation only plays one time. If you turn on Reduce Motion in your System Preferences, you can see that all the animations turn off.

[0:54] We could stop there, but I don't necessarily want to remove every animation from the page. I just want to make it less jarring for people who may have certain disabilities, including vestibular disabilities.

[1:06] In this case, I want the stretch animation to do something different. We'll remove that from our list of classes, add it below, and then change the animation-name: pulse to give the box a pulsing effect.

[1:22] We need to add @keyframes at-rule for pulse to describe what the animation is going to do. Zero percent or when the animation starts, I want to set the opacity to half or .5. When the animation is at 100 percent or when it's finished one iteration, then, I want to set the opacity: one.

[1:45] Now you can see our box has a pulsing effect. If we turn reduce-motion back off, you can see all of our animations still work as they did before. As soon as you toggle reduce-motion back on, everything stops except the pulsing header box.

~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today