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’sseed-russian-giant. - A rule in the stylesheet could set these names too. The lesson uses a
styleattribute 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 h1position: relativeandz-index: 1lifts 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(*)withanimation-name: nonestops 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-nameon 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: