Sub topic 10
Respecting prefers-reduced-motion
This chapter assumes you can call document.startViewTransition(), you know the ::view-transition pseudo-element tree, and you can use matchMedia in JavaScript. Feature detection from the previous chapter is the starting point here.
Why view transitions need a reduced-motion path
Some people get dizzy, nauseous, or distracted by large on-screen movement. Vestibular disorders, migraines, and attention disorders are common reasons. Operating systems let users express this with a "reduce motion" setting. Browsers expose it to pages as the prefers-reduced-motion media query.
View transitions are a likely source of that kind of motion. A shared-element transition can fly an image across the whole viewport. Even the default root cross-fade flashes a large area.
Browsers do not turn view transitions off for you when the user asks for reduced motion. The default animations run unless your code or CSS stops them. Respecting the setting is your job.
The media query
The query has two useful values:
no-preference: the user has not asked for less motion.reduce: the user has asked for less motion.
In CSS you write @media (prefers-reduced-motion: reduce). In JavaScript you use window.matchMedia('(prefers-reduced-motion: reduce)').matches.
Strategy 1: Disable the animations in CSS
The simplest approach keeps the transition running but removes its animation. Every pseudo-element in the tree gets animation: none.
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}The * argument matches every view-transition-name, including root. With no animation, the new snapshot is shown at once and the old one is not shown at all. The DOM update still happens, so the page works the same.
The !important is there for a reason. If you wrote a rule such as ::view-transition-old(hero) { animation: 300ms ... } earlier, equal specificity means the later rule wins. !important makes the reduced-motion rule win wherever it sits in the file. Placing the media query last is still good practice.
Strategy 2: Skip the transition in JavaScript
CSS-only disabling still pays for the transition machinery. The browser captures snapshots, and rendering is paused while the update callback runs. If the user wants no motion, you can skip all of that and call the update directly:
const reducedMotionQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
async function runUpdate(update) {
const canTransition = Boolean(document.startViewTransition);
if (!canTransition || reducedMotionQuery.matches) {
await update();
return;
}
const transition = document.startViewTransition(update);
await transition.updateCallbackDone;
}This function handles two cases with one branch. Browsers without the API and users who prefer reduced motion both get a plain DOM update. The update function can return a promise, because both await and the view transition callback accept one.
Read reducedMotionQuery.matches each time you navigate. Do not copy it into a constant at load time. A user can change the OS setting while your page is open, and the MediaQueryList object stays current.
Strategy 3: Reduce motion instead of removing change
"Reduce" does not mean "remove all visual feedback". A sudden swap can be disorienting, especially for sighted users who rely on a transition to see what changed. A common middle path removes movement and scale but keeps a short fade.
A shared element has a group pseudo-element that animates position and size. A cross-fade lives on the old and new image pseudo-elements. You can switch off the first and keep the second:
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*) {
animation: none;
}
::view-transition-old(*),
::view-transition-new(*) {
animation-duration: 150ms;
}
}Now a hero element no longer travels. It appears at its final position and fades from the old image to the new one. Opacity changes are generally much easier to tolerate than motion across the screen.
Choosing a strategy
The diagram shows where each decision happens.
| Approach | Where it lives | Cost when reduced | Best for |
|---|---|---|---|
| `animation: none` on all pseudo-elements | CSS | Snapshots still captured | Simple sites, a safe baseline |
| Skip `startViewTransition()` | JavaScript | None | Heavy pages, or when you want no flicker at all |
| Remove movement, keep a short fade | CSS | Snapshots still captured | Apps where users need to see what changed |
You can combine them. Many apps skip in JavaScript and also keep the CSS rule as a safety net for any transition started elsewhere.
JavaScript-driven animations are not covered by CSS
The CSS media query only affects CSS animations. If you animate pseudo-elements with the Web Animations API, as in the clip-path reveal from the earlier chapter, that animation ignores your CSS rule. You must check the preference in code:
async function revealView(update) {
const transition = document.startViewTransition(update);
await transition.ready;
const reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (reduce) return;
document.documentElement.animate(
{ clipPath: ['inset(0 0 100% 0)', 'inset(0)'] },
{
duration: 400,
easing: 'ease-in',
pseudoElement: '::view-transition-new(root)'
}
);
}When reduce is true the function returns before animating. The default root cross-fade still applies unless your CSS removes it. The CSS media query above handles that part.
Testing the setting
You do not have to change your OS settings to test.
- Open DevTools and open the Rendering panel.
- Find "Emulate CSS media feature prefers-reduced-motion" and choose
reduce. - Trigger your transition. Confirm the motion is gone or softened.
- Switch back to
no-preferencewithout reloading, and confirm the full animation returns. This checks that you read the query at call time.
Also test with the real OS setting at least once, because the emulation does not exercise the operating system's path.
Common mistakes
- Checking only at page load. The user can change the setting later. Read it on each transition.
- Only handling CSS. Web Animations API code and any animation libraries need their own check.
- Removing the update with the animation. The DOM update must run in every branch. Skipping the transition should never skip the update.
- Forgetting the unnamed elements.
::view-transition-old(*)covers all names, but a rule that only targetsrootmisses shared elements likehero. - Treating reduced motion as an error state. Test that the reduced path looks intentional, not broken.
Exercise
A page calls document.startViewTransition(update) directly. The CSS has the rule @media (prefers-reduced-motion: reduce) { ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; } }. Name one thing this does not prevent, and one change that avoids it.
Show answer
The browser still captures snapshots and pauses rendering while the update callback runs, so the transition machinery still costs time. It also does not stop any Web Animations API animation started on a pseudo-element. To avoid the cost, check matchMedia('(prefers-reduced-motion: reduce)').matches before calling startViewTransition() and run the update directly. To stop JavaScript animations, check the same query before calling animate().
Exercise
You want reduced-motion users to see a 150ms fade but no movement of a shared element named hero. Which pseudo-element do you set to animation: none, and which do you give a shorter duration?
Show answer
Set animation: none on ::view-transition-group(hero) (or (*)), because that pseudo-element animates position and size. Give ::view-transition-old(hero) and ::view-transition-new(hero) an animation-duration of 150ms, because they carry the cross-fade.