Motion and Interaction · Labs · 4 min read
Build a page transition
A guided lab: pick two pages with a clear hierarchy, decide the direction and the tokens, build a cross-document view transition in CSS only, give it a reduced-motion version and test it. About an hour and a half, with two HTML pages on any local server.
A good page transition tells people where they went, and then gets out of the way. Going deeper should feel like moving forward; going back should feel like returning. Browsers can now do this between two real pages with a few lines of CSS. You'll build one, keep it short, and make sure it still makes sense with the motion taken away.
What you'll do:
- Pick a forward navigation with a clear hierarchy
- Decide the direction, the distance and the tokens
- Build it with
@view-transition, CSS only - Add the reduced-motion version and test it
You'll need: two HTML pages on the same site, served from a local server (any will do), a Chromium-based browser for the first test, about an hour and a half.
1. Pick the navigation
Your system asks for reduced motion, so the pages only cross-fade: no sliding.
@view-transition code in the lab, run with transitions on two stacked panels, because a lesson page cannot navigate inside itself. Marcus Andrey, CC BY 4.0.Choose a step down a hierarchy: a list to one item, a folder to a file, an order list to one order. The figure uses a recipe list and one recipe. Write one sentence:
Going from ______ to ______ is a step deeper, so the new page comes from the ______.
Navigation transitions and The spatial model of an interface explain why: deeper comes from the right in left-to-right languages, and back returns to the left.
2. Decide the values
Fill in four lines before any code. The worked example is the figure's:
| Decision | Worked example |
|---|---|
| New page in | Slides from the right, 8% of the width, and fades in: motion.enter, 250 ms, cubic-bezier(0, 0, 0, 1) |
| Old page out | Slides 8% to the left and fades out: motion.exit, 150 ms, cubic-bezier(0.3, 0, 1, 1) |
| What stays still | The header, if both pages share it (a named element, see step 5) |
| Reduced motion | Cross-fade only, no sliding |
Why only 8%? A full-width slide makes people's eyes travel across the whole screen for no extra meaning. A short slide gives the direction; the fade does the rest.
3. Opt in on both pages
Cross-document view transitions need no JavaScript. Both pages, on the same origin, opt in with one rule (MDN, Using the View Transition API):
@view-transition {
navigation: auto;
}
Load page one, click through to page two. You'll already see a cross-fade: that's the browser's default animation. Browsers that don't support view transitions just navigate as usual, so nothing breaks.
4. Write the animation
Add this to the CSS both pages share:
@keyframes mo-out { to { opacity: 0; transform: translateX(-8%); } }
@keyframes mo-in { from { opacity: 0; transform: translateX(8%); } }
::view-transition-old(root) { animation: 150ms cubic-bezier(0.3, 0, 1, 1) both mo-out; }
::view-transition-new(root) { animation: 250ms cubic-bezier(0, 0, 0, 1) both mo-in; }
::view-transition-old(root) is a snapshot of the page you're leaving; ::view-transition-new(root) is the page arriving. Only opacity and transform move, the two properties that stay smooth (see Motion performance).
The figure runs the same durations, curves and distances with CSS transitions on two stacked panels, because a lesson page can't navigate inside itself. On your two pages, the code above does it for real.
5. Keep the shared parts still
If both pages have the same header, it shouldn't slide with everything else. Give it a name on both pages:
.site-header { view-transition-name: site-header; }
Now the browser animates the header on its own, and since it's in the same place on both pages, it stays still while the content moves. A name must be unique on the page, or the transition is skipped (MDN, Using the View Transition API). Moving one element from a list into a detail page this way is the subject of Shared element transitions.
6. Add the reduced-motion version
@keyframes mo-fade-out { to { opacity: 0; } }
@keyframes mo-fade-in { from { opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
::view-transition-old(root) { animation-name: mo-fade-out; }
::view-transition-new(root) { animation-name: mo-fade-in; }
}
Durations and curves stay; the slide goes. A cross-fade is not motion in WCAG's sense, because nothing changes position (W3C, Understanding 2.3.3). Switch on reduced motion in your system and click through again.
7. Test it
- Back button. Your animation plays the same way going back, which says "deeper" when you're returning. Note it: the Back direction needs the mirrored animation, and the browser has to be told which way you're going (see Web Animations and View Transitions).
- Slow network. In DevTools, throttle to a slow connection. The transition waits for the new page; is the wait acceptable?
- Ten clicks in a row. Does 250 ms start to feel slow? If yes, shorten it.
- Another browser. Where it isn't supported, the pages should just change, with nothing broken.
8. Check your work
Reflection questions: What did the Back button tell you about the transition? Would a list-to-detail move feel better with a shared element instead of a slide?
Related
Navigation transitions · The spatial model of an interface · Shared element transitions · Web Animations and View Transitions · Motion tokens