Sub topic 6
Morphing shared elements between pages
This chapter assumes you have already enabled cross-document transitions with @view-transition { navigation: auto } on both pages. It also assumes you know the default root crossfade and the pseudo-element tree from the earlier chapters. Here you will make one element appear to travel from the old page to the new one.
What "shared" means
By default the browser snapshots the whole page as one image named root. The old snapshot and the new snapshot crossfade, so a thumbnail on page A and a hero image on page B simply fade past each other.
A shared element transition changes that. You give an element on each page the same view-transition-name. The browser lifts that element out of the root snapshot and captures it separately. It then matches the old capture to the new capture by name and animates one into the other. The animation covers position, size, and a crossfade of the pixels.
The matching is by name only. The browser does not check that the two elements are the same tag, have the same content, or sit in the same part of the layout.
Naming an element
view-transition-name takes a custom identifier. Its initial value is none, which means the element is not captured separately.
.site-header {
view-transition-name: site-header;
}
.post-hero img {
view-transition-name: post-image;
}Both pages must load this rule, or an equivalent one. The name does not have to come from a shared stylesheet. It only has to be present on the old page when it is snapshotted and on the new page when it first renders.
Here is a minimal pair of pages. The home page shows one featured image. The post page shows the same image as a hero.
<!-- index.html -->
<a href="/posts/launch/">
<img class="featured" src="/img/launch.jpg" width="400" height="300" alt="Launch day">
</a><!-- posts/launch/index.html -->
<figure class="post-hero">
<img src="/img/launch.jpg" width="1200" height="600" alt="Launch day">
</figure>/* Shared stylesheet, loaded by both pages */
@view-transition {
navigation: auto;
}
.featured,
.post-hero img {
view-transition-name: post-image;
}When you click the link, the image grows from 400 by 300 at its position on the home page to 1200 by 600 at its position on the post page.
What the browser builds
Every name produces its own subtree under ::view-transition. The tree for this example has two groups: one for root and one for post-image.
Each pseudo-element has a job:
| Pseudo-element | What it does |
|---|---|
| `::view-transition-group(name)` | Moves and resizes from the old box to the new box. The browser generates this animation. |
| `::view-transition-image-pair(name)` | Holds the old and new images and isolates their blending. |
| `::view-transition-old(name)` | The captured image of the element on the old page. It fades out. |
| `::view-transition-new(name)` | A live rendering of the element on the new page. It fades in. |
The group animation is what makes the element look like it moves. You can change its timing without losing the generated motion:
::view-transition-group(post-image) {
animation-duration: 450ms;
animation-timing-function: cubic-bezier(0.2, 0, 0, 1);
}This works because you only override timing properties. The generated keyframes for width, height, and transform stay in place.
Keeping an element still
Naming an element also exempts it from the root animation. This is useful for a header that should not slide with the rest of the page. If you have a custom slide on ::view-transition-old(root) and ::view-transition-new(root), the named header is no longer part of that snapshot, so it does not move with it.
.site-header {
view-transition-name: site-header;
}
::view-transition-group(site-header) {
animation-duration: 0s;
}Setting the group's duration to 0s skips the move. If the header is identical on both pages, the old and new images are identical too, so the crossfade is invisible and the header appears to stay put.
Fixing mismatched aspect ratios
The thumbnail in the example is 4:3 and the hero is 2:1. The group resizes smoothly, but the old and new images inside it are by default sized to the group's width with automatic height. During the move, one image can look stretched or leave a gap.
A common fix makes both snapshots fill the group and crop like object-fit: cover:
::view-transition-old(post-image),
::view-transition-new(post-image) {
height: 100%;
object-fit: cover;
overflow: clip;
}Use cover when cropping is acceptable. If you need the whole image visible at all times, use contain and expect letterboxing during the move.
Rules and gotchas
Names must be unique
Within one document, each view-transition-name must be used by only one rendered element at the time of capture. If two rendered elements share a name, the browser skips the whole transition and the navigation proceeds normally. The error appears in the console of the page that has the duplicate.
This is the most common reason a list page fails. If you name every card's thumbnail post-image, you have duplicates on the list page. The fix is to name only the one element that is actually involved, or to give each item its own name. A later chapter shows how to set names at navigation time with the pageswap and pagereveal events. That approach avoids both problems.
Unmatched names still animate
If a name exists on the old page but not on the new one, the old element fades out in its own group. If the name exists only on the new page, it fades in. Nothing breaks, but you do not get the morph.
Naming creates side effects
An element with a view-transition-name other than none becomes a stacking context and a containing block for fixed and absolute descendants. It also paints as part of the transition overlay and not in the page's normal paint order. If a named element contained a position: fixed child, the child now positions relative to the named element. Check for layout shifts after adding names.
Hidden elements are not captured
An element with display: none, or one that is not rendered for another reason such as content-visibility: hidden, cannot be captured. A name on it has no effect. Check that the element is really rendered on both pages when the snapshots are taken.
Names are not selectors for content
The name is an identifier for matching and for CSS selectors. It carries no meaning about content. Two unrelated elements with the same name will morph into each other without complaint.
Matching the two pages' timing
The old page is snapshotted when the browser is about to leave it. The new page is captured after it first renders. If the new page's named element is still loading, for example an image without dimensions, the "new" side may be empty or have a different size from the final layout, and the morph ends on a jump.
Give images width and height attributes, or an aspect-ratio, so the box is correct before the bytes arrive. For content that really must exist before the first frame, a later chapter covers render blocking.
Checking your work
A quick checklist when a shared element does not morph:
- Does
@view-transition { navigation: auto }appear on both pages? - Does exactly one rendered element on each page carry the same name?
- Is the navigation same-origin?
- Is the console free of duplicate-name messages?
- Is the element rendered, and not hidden, on both pages?
Exercise
A blog list page gives every card image view-transition-name: post-image in its stylesheet. Clicking any card produces a plain page load with no animation at all, not even the crossfade. What is the cause, and what is the smallest change to the list page that lets one card morph?
Show answer
Several rendered elements on the list page share the name post-image. A duplicate name makes the browser skip the entire view transition, so even the root crossfade does not run. The smallest fix is to remove the name from the shared card rule so that at most one element carries it. For example, set view-transition-name: post-image only on the clicked card's image, either by giving each card a unique name or by assigning the name in a pageswap handler just before the old page is captured.
Exercise
You add view-transition-name: site-header to the header on both pages and a custom slide animation on ::view-transition-old(root) and ::view-transition-new(root). The header no longer slides with the page. Why, and how would you make it fade slightly instead of staying perfectly still?
Show answer
Naming the header removes it from the root snapshot, so the root animations no longer affect it. It gets its own group, whose images are the old and new header. To make it fade, leave the group's motion alone and style ::view-transition-old(site-header) and ::view-transition-new(site-header) with their own opacity animation. Only setting animation-duration: 0s on the group, as in the earlier example, makes the header appear fixed.