Sub topic 4
The pseudo-element tree and the default crossfade
This chapter assumes you know CSS selectors and @keyframes, and that you have already enabled cross-document transitions with @view-transition { navigation: auto; } on both pages. Here you will see what the browser builds during a transition, and why the default result is a crossfade.
Snapshots, not live pages
When a transition starts, the browser does not animate your real DOM. It captures an image of the old page, loads the new page, and captures a live view of the new one. It then builds a temporary tree of pseudo-elements that displays these captures. You can style that tree with CSS.
The tree hangs off the root element (html). It sits in a layer above the page and exists only while the transition runs.
What each pseudo-element does
| Pseudo-element | Role |
|---|---|
| `::view-transition` | Top-level container. It covers the viewport and holds one group per named element. |
| `::view-transition-group(name)` | Animates position and size from the old state to the new state. |
| `::view-transition-image-pair(name)` | Wraps the old and new images and isolates their blending. |
| `::view-transition-old(name)` | The captured image of the outgoing state. |
| `::view-transition-new(name)` | A live rendering of the incoming state. |
The name in parentheses identifies which captured element you are targeting. Everything that has no view-transition-name of its own belongs to one capture named root. That is why a plain page transition is addressed as (root). You can also use * to match every name.
Where the default crossfade comes from
The browser's built-in stylesheet defines the default behavior. You do not write it, but knowing it explains what you see:
- The group animates from the old size and position to the new ones. For
root, both are the full viewport, so nothing appears to move. - The old image animates from
opacity: 1to0. - The new image animates from
opacity: 0to1. - The animations last about 0.25 seconds.
- The two images blend with
mix-blend-mode: plus-lighter, so overlapping pixels add up to full brightness. This prevents a dip in brightness halfway through the fade.
The result is a short crossfade of the whole page. Note that the old image is a static snapshot, while the new image is live. The new page can keep updating, such as playing a video, while it fades in.
Inspecting the defaults yourself
The default animations are ordinary CSS animations on these pseudo-elements. You can override one piece without replacing the rest. This rule only lengthens the fade:
@view-transition {
navigation: auto;
}
::view-transition-group(root) {
animation-duration: 0.6s;
}Setting animation-duration on the group affects the group's own animation. The old and new images have their own fade animations, so this rule alone leaves the fades at 0.25 seconds. To change the fade, target ::view-transition-old(root) and ::view-transition-new(root). The next chapter covers that.
Pseudo-elements in this tree cannot be matched from the page's normal selectors, and you cannot reach them with document.querySelector. You style them from the stylesheet of the page that is running the transition. The old page styles the outgoing side, and the new page styles the incoming side, so both pages should include the same rules.
Common mistakes
- Expecting
::view-transition-old(root)to match the old page's live elements. It matches a flat image. - Styling the tree on only one page. Both pages need the opt-in rule, and each styles its own side of the animation.
- Forgetting that elements without a
view-transition-nameare part ofroot. They fade together with the rest of the page.
Exercise
A page opts in with @view-transition { navigation: auto; } and has no view-transition-name anywhere. Which pseudo-elements animate, and what does the user see?
Show answer
Only the root capture exists, so the tree contains ::view-transition, ::view-transition-group(root), ::view-transition-image-pair(root), ::view-transition-old(root) and ::view-transition-new(root). The old image fades out while the new image fades in, which looks like a quick crossfade of the whole page.
Exercise
You write ::view-transition-group(root) { animation-duration: 1s; } and the page still fades in about 0.25 seconds. Why?
Show answer
The fade is run by the animations on ::view-transition-old(root) and ::view-transition-new(root), not by the group. The group animates size and position, which do not change for root. Set the duration on the old and new pseudo-elements instead.