Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Reduce Motion Without Removing It

In this lesson, you toned down the Hawthorn Seed Library catalogue’s motion for visitors who ask their system to reduce motion. The “Returns week” banner and the filter panel now fade in instead of sliding, and the “New this week” badge keeps its pulse, only calmer.

Key Points

  • Some people get dizzy or sick when parts of a page slide across the screen, so their system has a setting to reduce motion. CSS reads it through the prefers-reduced-motion media feature.
  • To try it without changing your system, open DevTools, press Ctrl+Shift+P (Cmd+Shift+P on macOS) and choose Show Rendering. In the Rendering panel, set Emulate CSS media feature prefers-reduced-motion to prefers-reduced-motion: reduce. Reload to replay animations that run on load.
  • The browser reports the setting, but nothing changes until your stylesheet asks for it with @media (prefers-reduced-motion: reduce).
  • Each animation shorthand names its keyframes, then a duration and an easing. The badge’s last value, 4, is how many times it runs.
  • What troubles people is things traveling across the screen, zooming and parallax. A fade doesn’t move anything, so swap each slide for a fade. The fade-in keyframes have only a from. With no to, the fade ends at the element’s own opacity.
  • Both rules select the banner, and the media query comes later in the layer, so its animation-name: fade-in wins. It sets only the keyframes, one part of the animation shorthand. The duration and easing still come from the shorthand, so the banner fades in where it stands, over the same 0.6 seconds.
  • Keep gentle feedback. The badge’s pulse only changes opacity, and it’s the cue that draws the eye to a new seed. A quick, repeated blink can still pull at someone who asked for less, so under reduced motion it pulses twice over 2.4 seconds each, and still stops after 4.8 seconds.
  • Watch out for a popular shortcut, one rule that sets every animation and transition to none. It stops the slides, but it takes the badge’s cue away too.
  • Stopping within five seconds also keeps the badge clear of WCAG 2.2.2 Pause, Stop, Hide. It asks for a way to pause, stop or hide moving or blinking content that starts by itself, lasts more than five seconds and sits alongside other content, unless the movement is essential.
  • Set the emulation back to No emulation and the page follows your own system’s setting again. If your system doesn’t ask for less motion, the slides return, because the media query only applies to people who ask.

The Final Code

The catalogue’s motion, as the starting project has it, in the components layer of styles.css:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Bea’s catalogue now has colors that work in both themes. It also moves. The Returns week banner slides in from the left as the page loads. Open the filter panel, and its options slide down.

00:11 And the New this week badge pulses a few times, then stops. Some people get dizzy or sick when parts of a page slide across the screen. So their system has a setting to reduce motion, and CSS reads it through prefers-reduced-motion, a media feature.

00:28 In this video, you’ll use it to reduce the catalogue’s motion without removing it.

00:35 Open DevTools, then open its Rendering panel from the Command Menu, the same way you emulated a dark system. Find the emulation for prefers-reduced-motion, and set it to reduce.

00:47 The browser now answers as if your system asked for less motion. Reload the page. The banner still slides in. Open the filter panel, and it still slides down.

00:59 The browser reports the setting, but your stylesheet never asks for it.

01:04 Each animation names its keyframes, then a duration and an easing. The badge’s last value is how many times it runs.

01:14 What troubles people is things traveling across the screen, zooming and parallax. A fade doesn’t move anything. The element appears where it already stands.

01:25 So, at the end of the components layer, add keyframes called fade-in that start from an opacity of zero. With no to block, the fade ends at the element’s own opacity.

01:39 Under them, add a media query that asks whether prefers-reduced-motion is set to reduce. Inside it, give the banner and the filter options fade-in as their animation name.

01:52 Here’s the banner’s own rule beside your media query. Now reload, with the emulation still on. Your turn: pause the video and predict what the banner does as the page loads.

02:08 It fades in where it stands, over the same 0.6 seconds. Both rules select the banner, and the media query comes later in the layer, so its animation-name wins.

02:19 It sets only the keyframes, one part of the shorthand. The duration and easing stay as they were. Open the filter panel, and the options fade in too.

02:31 The badge still pulses. Its pulse only changes opacity, so nothing moves. And it’s the cue that draws the eye to a new seed. Watch out for a popular shortcut, one rule that sets every animation and transition to none: it stops the slides, but it takes the badge’s cue away too.

02:51 A quick, repeated blink can still pull at someone who asked for less, so make the pulse gentler instead of switching it off. In the media query, give the badge a duration of 2.4 seconds and two iterations.

03:04 Now it pulses twice, more slowly, and still stops after 4.8 seconds. Stopping within five seconds also keeps it clear of WCAG 2.2.2, Pause, Stop, Hide, which asks for a pause control on longer motion that starts by itself.

03:25 Here’s the rule: when someone asks for reduced motion, swap movement for a fade and keep the gentle feedback.

03:34 Set the emulation back to No emulation and reload. Now the page follows your own system’s setting. If it doesn’t ask for less motion, the banner slides in again, because the media query only applies to people who ask.

03:48 Next, in Adapt to More Contrast and Forced Colors, you’ll see what happens to Bea’s colors when a visitor’s contrast settings overrule them.

Become a Member to join the conversation.