Sub topic 3
The pseudo-element tree you can style
This chapter assumes you can call document.startViewTransition() and know the capture, update, animate sequence from the previous chapter. It also assumes you are comfortable with CSS pseudo-elements and @keyframes. Here you learn what the browser builds while a transition runs, and how to select each part of it.
Where the tree comes from
When a transition starts, the browser captures snapshots of the old state, runs your update callback, and captures the new state. It then builds a tree of pseudo-elements and plays animations on it. The tree is not part of your DOM, so querySelector cannot find it and the Elements panel does not list it as normal nodes. You reach it only through CSS selectors or the Web Animations API.
The tree hangs off the document element (html). It exists only while the transition is active, from just before the ready promise resolves until the finished promise resolves. Outside that window, rules that target these pseudo-elements match nothing, so they cost nothing.
The shape of the tree
The root of the tree is ::view-transition. It is a fixed-position overlay that covers the viewport and sits in the top layer, above all page content. While it exists, you see the snapshots in it, not the live page.
Each captured name gets one group. Every group holds one image pair, and the image pair holds an old image and a new image.
The table lists what each part does.
| Pseudo-element | What it is | Default behavior |
|---|---|---|
| `::view-transition` | The overlay that holds all groups | Fixed, covers the viewport |
| `::view-transition-group(name)` | A wrapper for one named element | Animates position and size from old to new |
| `::view-transition-image-pair(name)` | Holds the old and new images | Isolates blending between the two images |
| `::view-transition-old(name)` | A static snapshot of the element before the update | Fades from opacity 1 to 0 |
| `::view-transition-new(name)` | A live rendering of the element after the update | Fades from opacity 0 to 1 |
What each layer is for
The group carries geometry. Its width, height, and transform match the named element, and the browser animates them from the old box to the new box. If you want to change how something moves or resizes, style the group.
The image pair is a plain container. Its main job is isolation, so the old and new images blend only with each other and not with the groups behind them. You rarely style it, but it is a good place to set properties such as isolation or border-radius shared by both images.
The old and new pseudo-elements behave like replaced elements, similar to an img. That means you can use object-fit, object-position, and mix-blend-mode on them. The old image is a frozen bitmap. The new image is live, so if the new element animates or changes during the transition, you see that change.
The default stylesheet also sets mix-blend-mode: plus-lighter on both images. This makes the cross-fade add light values so overlapping images do not dip in brightness midway. If you replace the fade with another effect, you often need to reset the blend mode, as the JavaScript animation chapter shows.
The root group
Every transition has a group named root. The browser assigns the name root to the document element, so the root snapshot holds the whole page. Any element that has its own view-transition-name is painted in its own group, and it is left out of the root snapshot.
That is why ::view-transition-old(root) and ::view-transition-new(root) are the selectors for a full-page change. A named element on the page is not part of the root cross-fade. Without a name, it is part of it.
Selecting parts of the tree
Each functional pseudo-element takes one argument: a name, or * for every name. The wildcard is useful for global rules such as slowing everything down while you debug.
::view-transition-group(*) {
animation-duration: 3s;
}A selector with a specific name wins over * in the usual way, through specificity and source order. The next rules change only the root and leave named elements alone.
@keyframes slide-out { to { transform: translateX(-40px); opacity: 0; } }
@keyframes slide-in { from { transform: translateX(40px); opacity: 0; } }
::view-transition-old(root) {
animation: 250ms ease-in both slide-out;
}
::view-transition-new(root) {
animation: 250ms ease-out both slide-in;
}These animations run on the old and new images only. The group for root keeps its default behavior, which for a viewport-sized box means it does not move.
When only one side exists
A name does not have to appear in both states. The browser builds the parts it can.
- If a named element exists only in the old state, the image pair has only
::view-transition-old(name). The default animation fades it out. - If it exists only in the new state, the pair has only
::view-transition-new(name), and it fades in. - If it exists in both states, the group animates between the two boxes while the images cross-fade.
Your CSS can target both pseudo-elements without checking which exist. A rule for a pseudo-element that is not in the tree simply matches nothing.
Looking at the tree from JavaScript
You can read computed styles from the pseudo-elements once the tree exists. Wait for the ready promise first. Before that, the pseudo-elements are not yet created.
async function inspectGroup(update) {
const transition = document.startViewTransition(update);
await transition.ready;
const style = getComputedStyle(
document.documentElement,
'::view-transition-group(root)'
);
console.log(style.width, style.height);
return transition.finished;
}The same pseudoElement approach works for animating, which a later chapter covers. For everyday debugging, the browser's animation panel is easier, because it lets you pause a transition and step through it.
Stacking and clipping
The overlay lives in the top layer, so z-index on your page content has no effect on it. Two surprises follow from this.
First, the snapshots are bitmaps. A page with overflow: hidden ancestors does not clip them, because the pseudo-elements are not inside those ancestors. A named element that was clipped in the live page can look larger during the transition, since its snapshot is captured at its full painted size.
Second, border-radius on a named element is not applied to its snapshots by default. If a rounded card transitions and the corners look square mid-flight, add a matching radius to the old and new images.
::view-transition-old(card),
::view-transition-new(card) {
border-radius: 12px;
overflow: clip;
}Summary of the working model
- The browser builds
::view-transitionon the document element after capture and removes it when the transition finishes. - Each name becomes a group that animates geometry, containing an image pair with old and new images.
- The
rootname covers everything that has no name of its own. - You style the tree with plain CSS selectors, using a name or
*. - The old image is static and the new image is live. Both are replaced elements.
Exercise
A page has a sidebar with view-transition-name: nav and no other names. You write ::view-transition-old(root) { animation: none; }. Which pseudo-element still fades the sidebar out if the sidebar is removed in the update, and why?
Show answer
::view-transition-old(nav) still fades it out. The sidebar has its own name, so it is captured in the nav group and excluded from the root snapshot. Your rule targets only root, so the default fade on the nav old image is unchanged.
Exercise
You want to slow every transition animation to 4 seconds so you can inspect the tree. Which selector do you use, and which property do you set on it?
Show answer
Use ::view-transition-group(*) with animation-duration: 4s. For a full-page fade you would also need the same duration on ::view-transition-old(*) and ::view-transition-new(*), because those carry the opacity animations.