Sub topic 10
Respecting prefers-reduced-motion
This chapter assumes you can call document.startViewTransition(), you know the ::view-transition-* pseudo-elements, and you have written a media query. Some users get dizzy or nauseated from large animated movement, and they can tell their operating system to reduce it. Browsers expose that choice as the prefers-reduced-motion media feature. Your transitions should honor it.
Option 1: Disable the animations in CSS
The simplest approach keeps the DOM update inside the transition but removes the animation. The browser still captures snapshots and swaps states, and the swap happens immediately.
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}The * selector matches every transition name, including root and any names you assigned with view-transition-name. !important is there because your own rules, such as ::view-transition-old(root) with a custom duration, would otherwise win on specificity or source order.
Option 2: Skip the transition in JavaScript
CSS alone still runs the snapshot machinery. If you want to avoid it, or you drive animations with the Web Animations API, check the preference before you start the transition.
function updateView(nextId) {
document.querySelector('#content').innerHTML = renderItem(nextId);
}
function navigate(nextId) {
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!document.startViewTransition || reduceMotion) {
updateView(nextId);
return;
}
document.startViewTransition(() => updateView(nextId));
}This reuses the same fallback path that handles browsers without the API. The page behaves the same either way, only without motion.
Common mistakes
- Only disabling
root. Shared elements have their owngrouppseudo-elements. If you target only::view-transition-old(root), a named element still slides across the screen. - Hiding the update with the animation. Never put the DOM change behind a check that skips it. Reduced motion changes how the update appears, not whether it happens.
- Forgetting JavaScript-driven motion. Rule
animation: nonedoes not stop anelement.animate()call withpseudoElement. Gate that code with thematchMediacheck.
Reacting to a change while the page is open
The preference can change after load. Because matchMedia(...).matches is read on every call to navigate, the code above sees the current value each time. You don't need to store it or add a listener.
Exercise
A page's CSS disables ::view-transition-old(root) and ::view-transition-new(root) under prefers-reduced-motion: reduce, but a card that has view-transition-name: hero still glides to its new position for users with that setting. Why, and what is the fix?
Show answer
The moving part is ::view-transition-group(hero), which carries the position and size animation, and the rule does not target it. Either use ::view-transition-group(*) along with the old and new selectors, or skip startViewTransition() in JavaScript when matchMedia('(prefers-reduced-motion: reduce)').matches is true.