Sub topic 12
Reduced motion and graceful fallback
This chapter assumes you know the @starting-style rule, transition-behavior: allow-discrete, and the display and overlay transitions from the earlier chapters. It also assumes you can write a basic media query and some DOM JavaScript. Here you will make entry and exit animations respect the user's motion preference. You will also make them degrade cleanly in browsers that lack the newer features.
Why this matters
Entry and exit animations are decoration. The interface must work the same way with or without them.
Two groups of people need this to hold:
- Users who have asked their operating system to reduce motion. Large movement such as sliding and scaling can cause dizziness or nausea for people with vestibular disorders.
- Users on browsers that do not support
@starting-style,allow-discreteoroverlay. They should see the element appear and disappear instantly, not see something broken.
Both cases come down to the same rule. The final state of every show and hide action must be correct, whether or not any animation ran.
What prefers-reduced-motion reports
The prefers-reduced-motion media feature reflects an operating system setting such as "Reduce motion" on macOS and iOS, "Show animations" on Windows, or "Remove animations" on Android. It has two values:
no-preference: the user has not asked for less motion.reduce: the user wants less motion.
"Reduce" does not mean "remove every visual change". A short opacity fade has no spatial movement and is generally acceptable. Sliding, zooming, parallax and spinning are the effects to remove or soften.
Approach 1: make the transition effectively instant
The simplest policy keeps all your rules and shortens every duration:
@media (prefers-reduced-motion: reduce) {
.card,
.panel,
[popover],
dialog,
dialog::backdrop {
transition-duration: 0.01ms;
}
}A single value in transition-duration is repeated for every property in the transition-property list. That includes the display and overlay entries. The discrete flip still happens at the end of the duration, so the element is shown and hidden correctly, just without visible motion.
Why 0.01ms and not 0s
With a duration of exactly 0s (and no delay), the browser does not start a transition at all. This has two consequences:
- The element reaches its final state immediately. This is fine for display.
- No
transitionstartortransitionendevent fires, andgetAnimations()returns nothing for that element.
If your JavaScript waits for transitionend to remove an element, 0s leaves it waiting forever. A tiny non-zero duration keeps the event flow intact. Even so, do not make your logic depend on the event, as the JavaScript section below shows.
Specificity and order
The override only works if it wins the cascade. Check these two things:
- Put the media query after the rules that declare the transitions. With equal specificity, the later rule wins.
- Match or exceed the specificity of the original declaration. If your component uses
.layout .card.is-open { transition: ... }, then.card { transition-duration: 0.01ms }loses.
Approach 2: keep the fade, drop the movement
Removing every effect makes the interface feel abrupt. A better policy is to keep opacity changes and gate only the movement behind no-preference. Because a starting style only affects the properties it lists, you can add the transform starting value inside a media query:
.card {
opacity: 1;
transform: translateY(0);
transition: opacity 0.4s ease, transform 0.4s ease;
@starting-style {
opacity: 0;
}
}
@media (prefers-reduced-motion: no-preference) {
.card {
@starting-style {
transform: translateY(1rem);
}
}
}When the user prefers reduced motion, the starting style contains only opacity: 0. The card fades in and does not slide. The transform transition is still declared, but nothing triggers it, because the starting and final values are equal.
This approach is called motion-safe by default. The movement is opt-in, so a browser that cannot evaluate the media query shows the simpler version.
The same idea works for a popover. This version fades always and scales only when motion is acceptable:
[popover] {
opacity: 0;
transition:
opacity 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
[popover]:popover-open {
opacity: 1;
}
@starting-style {
[popover]:popover-open {
opacity: 0;
}
}
@media (prefers-reduced-motion: no-preference) {
[popover] {
transform: scale(0.95);
transition:
opacity 0.3s,
transform 0.3s,
overlay 0.3s allow-discrete,
display 0.3s allow-discrete;
}
[popover]:popover-open {
transform: none;
}
@starting-style {
[popover]:popover-open {
transform: scale(0.95);
}
}
}The transition list is repeated inside the media query because a later transition declaration replaces the earlier one. It does not merge with it. The overlay and display entries must stay in both lists, or the exit animation gets cut off in the reduced-motion case or in the full-motion case.
Choosing between the two approaches
| Question | Approach 1: shorten durations | Approach 2: gate movement |
|---|---|---|
| Code to maintain | One small block | A media block per animated component |
| Result for reduce users | No visible change at all | A fade remains, movement is gone |
| Risk of forgetting a component | Selector list must be kept in sync | Handled where each component is defined |
| Best for | Retrofitting an existing site | New components and design systems |
Do not make logic depend on the animation
Application code often removes an element after its exit finishes. If that code waits for transitionend, it breaks when the duration is 0s, when the browser lacks allow-discrete, or when the transition is interrupted. Instead, ask the browser whether any animation is running, and treat "none" as "done":
function removeAfterExit(el) {
el.classList.add('is-leaving');
// Reading getAnimations() forces a style update, so any transition
// triggered by the class change is already listed.
const running = el.getAnimations();
Promise.allSettled(running.map((animation) => animation.finished))
.then(() => el.remove());
}.card.is-leaving {
opacity: 0;
}With motion enabled, running holds a transition for opacity and the element is removed when it finishes. With 0s durations or no support, the array is empty, Promise.allSettled([]) resolves at once, and the element is removed immediately. allSettled also covers a cancelled transition, because a cancelled animation rejects its finished promise and allSettled does not throw on rejection.
You can also read the preference directly when JavaScript starts a motion, for example scrollIntoView:
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
function scrollToCard(card) {
card.scrollIntoView({
behavior: reduceMotion.matches ? 'auto' : 'smooth',
});
}Reading .matches at call time means a change of the system setting is picked up without a page reload.
Graceful fallback for older browsers
Support differs per feature, so consider each one separately. Check a current compatibility table before you ship, because browser support changes.
| Missing feature | What the user sees | Is the interface still correct? |
|---|---|---|
| `@starting-style` | Inserted elements and elements leaving `display: none` appear instantly | Yes |
| `transition-behavior: allow-discrete` | Exits are cut off because `display: none` applies immediately. Entries may still fade | Yes |
| `overlay` transition | The top-layer element may drop out of the top layer before the exit finishes | Yes, but the exit may look clipped |
In every row, the final state is right. The failure is only visual. That is the property you want from progressive enhancement.
The shorthand trap
A browser that does not recognise allow-discrete treats the whole transition declaration as invalid and drops it. That removes your opacity transition too. Declare a plain version first, then override it only where the keyword is understood:
.panel {
opacity: 1;
transition: opacity 0.3s ease;
}
@supports (transition-behavior: allow-discrete) {
.panel {
transition: opacity 0.3s ease, display 0.3s allow-discrete;
}
}
.panel.is-hidden {
display: none;
opacity: 0;
}
@media (prefers-reduced-motion: reduce) {
.panel {
transition-duration: 0.01ms;
}
}In a browser without allow-discrete, the first transition stays in force. Because display: none applies immediately on hide, the fade-out is not visible, but the panel still hides correctly. Entry still fades wherever @starting-style works.
The reduced-motion block comes last so that it wins over the @supports block. Both rules have the same specificity, and the later one applies.
Testing
You do not need to change your operating system setting to test.
- In Chrome or Edge DevTools, open the Rendering panel and set "Emulate CSS media feature prefers-reduced-motion" to
reduce. - In Firefox, use the Inspector's "Simulate prefers-reduced-motion" toggle, or set
ui.prefersReducedMotioninabout:config. - In Safari, enable "Reduce Motion" in system accessibility settings, because there is no dedicated simulation toggle.
- Toggle the component and confirm that it still appears and disappears correctly, with only the motion removed.
- To test without support, comment out the
@starting-styleblocks and reload. The component must still work.
Common errors
- Media query placed before the transition rule. Equal specificity means source order decides, so the override silently loses.
- Using
animation: noneortransition: nonefor reduce users. This also cancels thedisplayandoverlayentries. An exit animation that was holding a dialog open stops doing so, and a dialog may stay in the top layer or vanish at an odd moment. Shorten durations instead. - Only handling
transition-durationfor keyframe animations. Keyframe animations useanimation-durationandanimation-iteration-count. Set the duration to0.01msand the iteration count to1inside the reduced-motion block. - Waiting for
transitionendto continue application logic. It does not fire for zero-duration or unsupported cases. UsegetAnimations()as shown above. - Forgetting
::backdrop. The backdrop has its own transition, so includedialog::backdropin the selector list.
Exercise
A dialog uses transition: opacity 0.3s, overlay 0.3s allow-discrete, display 0.3s allow-discrete;. A reduced-motion rule sets transition: none; for it. A developer reports that, for reduce users, the dialog disappears with no fade, which is expected, but a click handler that waits for transitionend before navigating never runs. What is the cause, and what two changes fix it?
Show answer
With transition: none, no transition runs, so transitionend never fires and the handler waits forever. Fix it by (1) replacing transition: none with transition-duration: 0.01ms so the property list stays intact and events still fire, and (2) not relying on the event at all. Check el.getAnimations() and use Promise.allSettled(...) so that an empty list continues immediately.
Exercise
Rewrite a card entry that currently uses @starting-style { opacity: 0; transform: translateY(1rem); } so that users who prefer reduced motion get the fade but not the slide.
Show answer
Keep opacity: 0 in the main @starting-style block. Move transform: translateY(1rem) into a second @starting-style block inside @media (prefers-reduced-motion: no-preference) { .card { ... } }. Reduce users then have a starting style with only opacity, so only the fade runs.
Exercise
Why can transition: opacity 0.3s, display 0.3s allow-discrete; remove the fade in an older browser, and how do you prevent that?
Show answer
A browser that does not know allow-discrete treats the whole declaration as invalid and discards it, including the opacity part. Declare a plain transition: opacity 0.3s; first, then place the allow-discrete version inside @supports (transition-behavior: allow-discrete) so only supporting browsers use it.