Sub topic 7
Respecting reduced-motion preferences
Cross-document view transitions add motion to every navigation on your site. Some visitors get dizzy, nauseated, or distracted by sliding, zooming, and morphing. This chapter shows how to honor their operating-system setting without breaking navigation.
This chapter assumes you know how to opt in with @view-transition, how to style ::view-transition-old() and ::view-transition-new(), and how media queries work. It also assumes you have seen the pageswap and pagereveal events.
What the setting means
Operating systems let users ask for less motion. On macOS this is "Reduce motion". On Windows it is turning off "Animation effects". iOS and Android have similar switches. Browsers expose the choice through the prefers-reduced-motion media feature, which has two values:
no-preference: the user has not asked for less motion.reduce: the user wants non-essential motion removed or minimized.
"Reduce" does not always mean "none". A short opacity change is usually acceptable. The things to remove are large movement across the screen, scaling, spinning, parallax, and morphing shared elements across long distances. Those cause the vestibular discomfort the setting exists to prevent.
Why the default needs attention
The default root crossfade is only an opacity change, so it is often tolerable. The problems start when you add your own work:
- Slide animations such as the
slide-outandslide-inkeyframes from earlier chapters. - Shared-element morphs, where a thumbnail travels and grows into a hero image.
- Directional animations chosen with transition types.
The browser does not turn these off for you. Reduced motion is your responsibility.
Where the motion comes from
Several parts of the system contribute motion, so you can address it in more than one place.
The three intervention points are:
- Do not opt in when the user prefers reduced motion.
- Skip the transition, or avoid adding names and types, in the lifecycle events.
- Neutralize the animations in CSS.
Option 1: Remove the animations in CSS
This is the most direct approach and works with everything you have already written.
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}The (*) selector matches every named transition element, including root. The !important is needed because your own rules, such as ::view-transition-old(root) { animation: 200ms ease-in both slide-out; }, would otherwise win on specificity or source order when they appear later in the file.
With animations set to none, the browser still performs the navigation. The new page simply appears with no movement. Be aware that this removes the crossfade too, so the page change is abrupt. For most sites that is acceptable.
Option 2: Replace motion with a gentle fade
Some users who choose "reduce" still benefit from visual continuity. A short fade gives a hint that the page changed, without movement. Write your fancy animations only for no-preference, and let the default crossfade serve everyone else.
@view-transition {
navigation: auto;
}
@keyframes slide-out {
to {
opacity: 0;
transform: translateX(-40px);
}
}
@keyframes slide-in {
from {
opacity: 0;
transform: translateX(40px);
}
}
@media (prefers-reduced-motion: no-preference) {
::view-transition-old(root) {
animation: 200ms ease-in both slide-out;
}
::view-transition-new(root) {
animation: 300ms ease-out both slide-in;
}
}Here the motion is opt-in rather than opt-out. A visitor whose browser cannot evaluate the media query, or whose preference is reduce, receives the browser's default crossfade. This "motion only when allowed" pattern is safer, because a forgotten rule fails toward less motion instead of more.
For shared elements, the browser animates position and size between the old and new boxes by default. To stop the travel, give the group a zero duration inside the reduce query:
@media (prefers-reduced-motion: reduce) {
::view-transition-group(post-image) {
animation-duration: 0s;
}
}Option 3: Do not opt in at all
Because the transition only happens when both pages opt in, you can place the at-rule inside a media query:
@media (prefers-reduced-motion: no-preference) {
@view-transition {
navigation: auto;
}
}When the preference is reduce, the page does not opt in, no snapshots are taken, and navigation is exactly what it was before you added transitions. This is the strongest option. It also avoids the cost of capturing snapshots for users who will see no animation anyway.
Option 4: Skip it in JavaScript
If your pageswap or pagereveal handlers assign names or types, check the preference first so you do not do work that is not needed. You can also cancel a transition that has already been created.
const reducedMotion = () =>
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
window.addEventListener('pageswap', (event) => {
if (!event.viewTransition) return;
if (reducedMotion()) {
event.viewTransition.skipTransition();
return;
}
// Normal logic: add names or types for animated navigations.
event.viewTransition.types.add('forward');
});
window.addEventListener('pagereveal', (event) => {
if (event.viewTransition && reducedMotion()) {
event.viewTransition.skipTransition();
}
});skipTransition() ends the transition and shows the destination state immediately. Checking in both events matters. The two pages are separate documents, and either one could run different code or have a different cached stylesheet.
Comparing the options
| Approach | Effect for users who reduce motion | Cost | Best for |
|---|---|---|---|
| `animation: none !important` in a reduce query | No animation, abrupt change | Snapshots still taken | Quick retrofit of an existing site |
| Motion rules only inside `no-preference` | Default crossfade only | Snapshots still taken | New projects, safe by default |
| `@view-transition` inside `no-preference` | No transition at all | None | Strictest compliance |
| `skipTransition()` in events | No transition | Small script cost | Sites that already use the events |
You can combine them. A common setup is Option 3 for the opt-in and Option 2 for the animations, so that any animation rule that slips through is still gated.
Common errors
- Only gating one page. If the old page opts in but the new page's stylesheet has no reduce rules, the animations still play. Put reduce handling in the shared stylesheet used by every page.
- Losing the specificity fight. A reduce rule placed before your
::view-transition-old(root)rule can be overridden. Use!important, or place the animation rules insideno-preferenceso no conflict arises. - Forgetting shared elements. Disabling
rootanimations does not stop apost-imagemorph. The(*)selector in Option 1 covers them. Per-name rules do not. - Testing only your own machine. Most developers never turn on the setting. Make it part of your checks.
- Treating
noneas "faster". Zero movement is the goal. A very fast slide is still a slide.
Testing the setting
In Chromium DevTools, open the Rendering panel and set "Emulate CSS media feature prefers-reduced-motion" to reduce. Then navigate between pages and confirm that no sliding or morphing occurs. Test the reverse as well, with no-preference, to make sure animations still work. Because the preference is read when the transition is created, reload the page after changing the emulated value instead of expecting it to affect a page already loaded.
Checklist
- Put motion rules inside
@media (prefers-reduced-motion: no-preference). - Add a
reduceblock that neutralizesgroup,old, andnewfor all names. - Consider gating
@view-transitionitself. - Check the preference in
pageswapandpagerevealbefore adding names or types. - Test both emulated values in DevTools.
Exercise
Your site opts in with an unconditional @view-transition { navigation: auto; } and has a rule ::view-transition-old(root) { animation: 200ms ease-in both slide-out; } in the same stylesheet. You add @media (prefers-reduced-motion: reduce) { ::view-transition-old(root) { animation: none; } } above it, and users with reduced motion still see the slide. Why, and what are two ways to fix it?
Show answer
The later rule has the same specificity as the reduce rule and comes after it in the file, so it wins and the slide still plays. One fix is to add !important to the reduce rule, ideally using (*) selectors for group, old, and new so shared elements are covered too. Another fix is to move the slide rule inside @media (prefers-reduced-motion: no-preference), so it never applies for users who ask for reduced motion. You could also wrap the @view-transition at-rule in the no-preference query so no transition is created at all.
Exercise
A list page tags a clicked thumbnail with view-transition-name: post-image in a pageswap handler. How would you change the handler so that visitors with prefers-reduced-motion: reduce get no morph, and why should the check run inside the handler instead of once at page load?
Show answer
At the top of the handler, evaluate window.matchMedia('(prefers-reduced-motion: reduce)').matches. If it is true, return early (or call event.viewTransition.skipTransition()) without setting the name on the thumbnail. Running the check inside the handler means it uses the user's current setting for every navigation, so a change made in the operating system while the page is open is respected.