Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Morph a Seed Photo Between Pages

In this lesson, you made the Russian Giant’s photo grow from its card in the Hawthorn Seed Library’s catalogue into the large photo on its seed page, while the rest of the page cross-fades around it.

Key Points

  • Without a name, the card’s photo and the seed page’s photo are two unrelated pictures to the browser. The small one fades out with the catalogue while the large one fades in with the seed page.
  • Give both elements the same view-transition-name, one on the page you leave and one on the page you arrive at, and the browser treats them as the same thing. The name is yours to choose, as long as both pages use the same one. Here it’s seed-russian-giant.
  • A rule in the stylesheet could set these names too. The lesson uses a style attribute to keep each name next to its photo.
  • Each card gets its own name, matching its page. Watch out: a name must be unique on a page. If two cards share one, the browser skips the whole transition.
  • When both pages have an element with the same name, the browser lifts it out of the page’s picture and gives it a group of its own, ::view-transition-group(seed-russian-giant), with an old and a new image in its ::view-transition-image-pair(), as the root has. Then it moves and resizes that group from the card’s box to the photo’s box.
  • During the move, the group sits above the rest of the page, so it hides the white name over the large photo. But a named element is painted on its own layer, above its unnamed neighbors, even with no transition playing, so the name stays hidden after the move too. Giving .seed-hero h1 position: relative and z-index: 1 lifts the name back above the photo.
  • The browser matches names on the page you leave and the page you arrive at, whichever way you go. When you click Back to the catalogue, the photo shrinks back into its card.
  • The moving photo travels across the screen, so under reduced motion, ::view-transition-group(*) with animation-name: none stops every group from moving. The star matches every group. A group’s own animation is the move, so the photo’s group starts in its final box. The fades belong to each group’s old and new images, so they still run, the root’s page fade included. The photo is in its place from the first frame while the page fades in around it.
  • The rule: give an element the same view-transition-name on both pages, once on each page, and the browser moves it from one box to the other.

The Final Code

In index.html, the Russian Giant card gets its photo, named:

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

00:00 Bea’s catalogue now cross-fades into each seed’s page, but the sunflower’s photo fades like everything else. Bea would like a small photo in the catalogue to grow into the large one at the top of its page.

00:12 In this video, you’ll make it do that with one CSS property, set on both pages. First, the card needs a photo. In the catalogue’s HTML, add the sunflower image to the Russian Giant card, above its heading, with the same width, height and alt text as on the seed page.

00:32 Then, in the stylesheet, make images in a card fill its width, with an automatic height.

00:40 Click Russian Giant. The small photo fades out in the card while the large one fades in at the top. To the browser, they’re two unrelated pictures.

00:52 You tell the browser they’re the same thing by giving both one name, with the view-transition-name property. On the card’s image, add a style attribute that sets view-transition-name.

01:04 The name is yours to choose, as long as both pages use the same one. Here it’s seed-russian-giant.

01:12 Then, in the seed page’s HTML, give its large photo the same attribute, with the same name.

01:21 A rule in the stylesheet could set these names too. The style attribute keeps each name next to its photo. Each card gets its own name, matching its page. Watch out: a name must be unique on a page.

01:34 If two cards share one, the browser skips the whole transition.

01:40 Save, and click Russian Giant. The photo grows out of its card into place at the top of the page, while the rest of the page cross-fades around it. Here it is again, slowed down.

01:54 When the page you leave and the page you arrive at both have an element with the same name, the browser lifts it out of the page’s picture. It gives that element a group of its own, named after it, with an old and a new image, as the root has.

02:10 Then it moves and resizes that group from the card’s box to the photo’s box. During the move, the group sits above the rest of the page, so it hides the white name.

02:22 But the name doesn’t come back when the move ends. A named element is painted on its own layer, above its unnamed neighbors, even with no transition playing.

02:32 In the stylesheet, give the seed page’s heading a relative position and a z-index of one.

02:39 The name is back. You’re on the seed page now. Click Back to the catalogue. Your turn: pause the video and predict what the large photo does.

02:56 It shrinks back into its card. The browser matches names on the page you leave and the page you arrive at, whichever way you go. The photo now travels across the screen.

03:08 A visitor who asked for less motion shouldn’t see it move. In the stylesheet’s reduced-motion query, under the root’s rule, select view-transition-group with a star in its parentheses.

03:20 The star matches every group. Set its animation name to none.

03:27 A group’s own animation is the move, so the photo’s group starts in its final box. The fades belong to each group’s old and new images, so they still run, the root’s included.

03:40 In the Rendering panel, set the reduced-motion emulation to reduce, and click Russian Giant. The photo is in its place from the first frame, while the page fades in around it.

03:51 Set the emulation back to No emulation when you’re done. Here’s the rule: give an element the same view-transition-name on both pages, once on each page, and the browser moves it from one box to the other.

04:06 Next, in Exercise: Name the Seed Photos, you’ll name the photos of the other seeds in Bea’s catalogue, so each one grows into its own page.

Become a Member to join the conversation.