Sub topic 3
The pseudo-element tree you can style
This chapter assumes you can wrap a DOM update in document.startViewTransition() and know that the browser snapshots the old and new states. You should also know basic CSS selectors and pseudo-elements. Here you will learn the structure the browser builds during a transition and how to target each part.
Where the tree lives
When a transition starts, the browser captures the old state. After the update callback runs, it captures the new state. It then builds a tree of pseudo-elements attached to the root html element and plays animations on it. This tree sits in a layer above the page, so it covers everything else while the transition runs. When the animations end, the tree is removed.
Each element that has a view-transition-name gets its own branch. The page itself is captured under the implicit name root.
What each part does
| Pseudo-element | What it is | Typical use |
|---|---|---|
| `::view-transition` | The top-level container, sized to the viewport | Set a background behind all snapshots |
| `::view-transition-group(name)` | Holds one named element. It animates position and size | Change movement timing for shared elements |
| `::view-transition-image-pair(name)` | Wraps the old and new images and isolates their blending | Rarely styled |
| `::view-transition-old(name)` | A static image of the element before the update | Animate the exit |
| `::view-transition-new(name)` | A live rendering of the element after the update | Animate the entrance |
The old image is a frozen picture. The new image is live, so video or animations inside it keep playing.
Selecting by name
Each selector takes a name in parentheses. Use root for the page, your own name for a named element, or * to match every name.
::view-transition-group(*) {
animation-duration: 250ms;
}
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 400ms;
}The first rule changes the movement of every group. The second changes the fade of the page snapshot only. The * form is handy for global defaults. A specific name overrides it when the specificity is equal and the rule appears later.
Seeing the tree
You can only style these pseudo-elements while a transition is running. To inspect them, run a transition with a long duration so the tree stays in place.
::view-transition-group(root) {
animation-duration: 5s;
}Then open the Elements panel in DevTools and look under html for ::view-transition. Remove the override when you finish.
Common mistakes
- Styling
::view-transition-old(hero)when no element has the namehero. The rule matches nothing and fails silently. - Trying to style the pseudo-elements before
startViewTransition()runs. They do not exist yet. - Putting the animation on
::view-transition-group(root)and expecting a fade. The group moves and resizes. The images fade.
Exercise
You want only the page's outgoing snapshot to fade out over 150ms, leaving every other part of the transition unchanged. Which selector do you use, and why not ::view-transition-group(root)?
Show answer
Use ::view-transition-old(root) with animation-duration: 150ms. The old pseudo-element is the frozen image of the page before the update, and its fade-out is the animation you want to change. The group only animates position and size, so changing its duration would not affect the fade.