Opt Into Page Transitions

Bea’s catalogue now tones its motion down for visitors who ask for less, and its colors hold up when a contrast theme takes over. One moment still feels abrupt, though. When you move from the catalogue to a seed’s page, the catalogue vanishes and the new page snaps into place.

In this lesson, you’ll ask the browser to cross-fade from one page to the next with CSS alone and predict when that fade will happen and when it won’t. You’ll also slow the fade down, calm it for reduced motion, and check what visitors see in browsers that don’t support view transitions. Start with a link to follow.

Opt in on Both Pages

A cross-fade needs two pages and a link between them, and the catalogue doesn’t link to its seed pages yet. In index.html, find the Russian Giant card and wrap the text of its heading in a link to the seed page:

Language: HTML
        <h3><a href="russian-giant.html">Russian Giant</a></h3>

Then give russian-giant.html a way back as the first thing inside its <main> element:

Language: HTML
    <p><a href="index.html">Back to the catalogue</a></p>

Start Live Preview if it isn’t running, and open the catalogue in Chrome or Edge at the address it serves, such as http://127.0.0.1:3000/index.html. Click Russian Giant, then Back to the catalogue. Each click swaps one whole page for the other in a single frame.

That hard cut is the browser’s default for every navigation. To ask for something smoother, you can opt a page in to a view transition, an animation the browser plays from the old page to the new one. In styles.css, add this rule to the components layer, right above the reduced-motion media query you added in Reduce Motion Without Removing It:

Language: CSS
  @view-transition {
    navigation: auto;
  }

The @view-transition at-rule has one setting you need here. Its navigation descriptor is none by default, and auto asks for a transition when the visitor moves to another page from the same origin, meaning the same scheme, host, and port. Two pages that Live Preview serves share one, while a link to another site still cuts.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.