Sub topic 7
Respecting reduced-motion preferences
This chapter assumes you can enable a cross-document transition with @view-transition and that you know the ::view-transition-old() and ::view-transition-new() pseudo-elements from earlier chapters. You also need basic familiarity with CSS media queries.
Why motion needs a switch
Sliding and zooming whole pages can cause dizziness, nausea, or headaches for people with vestibular disorders. Operating systems let users say they prefer less motion. Browsers expose that choice through the prefers-reduced-motion media feature, which has two values: no-preference and reduce.
View transitions animate the entire viewport, so they are a common source of large movement. Honoring the setting is part of using them responsibly.
Turning the animations off
The transition runs through ordinary CSS animations on pseudo-elements. Because of that, you can disable the movement with a media query:
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}The * matches every named transition, including root and any elements you gave a view-transition-name. The !important is there because your own per-name rules, such as ::view-transition-old(root), can be more specific than the wildcard and would otherwise win.
With animation: none, the page change still happens. The old snapshot is replaced by the new one without movement, so navigation keeps working.
Reduce motion, don't always remove it
A crossfade is gentle and usually acceptable. Large translation, scaling, and rotation are what cause problems. Instead of turning everything off, you can keep a fade and drop the movement:
@keyframes fade-out {
to { opacity: 0; }
}
@keyframes fade-in {
from { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
::view-transition-old(root) {
animation: 150ms linear both fade-out;
}
::view-transition-new(root) {
animation: 150ms linear both fade-in;
}
}Place this block after your slide rules so it overrides them at equal specificity. Shared elements named with view-transition-name still morph position and size through their group animation. If you want no movement for them, use the animation: none rule above for ::view-transition-group(*).
Applying it on both pages
The CSS must be present in the stylesheet of each page that takes part. The browser reads the rules from the page that owns each pseudo-element during the transition, so a missing block on one page means the old or new side animates anyway. Keep the media query in a shared stylesheet to avoid drift.
Testing the setting
- In Chrome DevTools, open the Rendering panel and set "Emulate CSS media feature prefers-reduced-motion" to
reduce. - Or change the operating system setting, such as "Reduce motion" on macOS and iOS or "Show animations" on Windows.
- Navigate between two pages and confirm that no sliding or morphing occurs.
Common errors
- The media query is only in one page's stylesheet, so one side still animates.
- A more specific selector overrides the wildcard rule because
!importantis missing. - Only
rootis handled, and named elements still move through their group animation. - Duration is set to
0sin one place but abothfill-mode animation is still defined elsewhere.
Exercise
Your site slides pages with ::view-transition-old(root) and ::view-transition-new(root), and the product photo has view-transition-name: product. Write a reduced-motion rule that removes all movement, including the product photo's morph. Why is !important used?
Show answer
Use @media (prefers-reduced-motion: reduce) { ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; } }. The wildcard covers root and product. !important ensures the rule beats the more specific ::view-transition-old(root) and ::view-transition-new(root) animations declared earlier.