Sub topic 4
The pseudo-element tree and the default crossfade
This chapter assumes you can write CSS animations with @keyframes, and that you know the @view-transition { navigation: auto; } opt-in from the previous chapter. Here you will learn what the browser builds when a transition runs, why a bare opt-in produces a crossfade, and how to inspect and slow down that crossfade so you can customize it later.
Where the pseudo-elements come from
A cross-document view transition does not animate your real DOM. The browser works in four steps:
- It captures a bitmap snapshot of the old page.
- It loads the new page and holds back its first paint.
- It builds a tree of pseudo-elements that stand in for the page.
- It runs animations on that tree, then removes it and shows the real page.
A pseudo-element is a selector-addressable box that exists only in the browser's rendering, with no node in your HTML. You can style these boxes with CSS, but you cannot query them with document.querySelector.
The pseudo-elements are created in the new document. That has a practical consequence: the ::view-transition-* rules that matter are the ones in the new page's CSS. If both pages load the same shared stylesheet, you never have to think about this. If each page has its own inline styles, put the transition rules in both.
The tree
Every named element contributes one group of pseudo-elements. With no names assigned, the only name in play is root. The browser's built-in stylesheet gives the :root element view-transition-name: root, so the whole page is captured as one unit.
Each level has a distinct job:
| Pseudo-element | What it is | Default behavior |
|---|---|---|
| `::view-transition` | The overlay that contains everything | Fixed to the viewport, drawn above the page in the top layer |
| `::view-transition-group(name)` | A box for one named element | Animates its size and position from the old state to the new state |
| `::view-transition-image-pair(name)` | A wrapper around the old and new images | Creates an isolated blending context |
| `::view-transition-old(name)` | The captured image of the old state | Fades from opacity 1 to 0 |
| `::view-transition-new(name)` | A live rendering of the new state | Fades from opacity 0 to 1 |
The old snapshot is a frozen bitmap. The new image is not frozen. It tracks the real new page as it renders, so a CSS animation or a loading image on the new page keeps moving during the transition.
Because the overlay sits above the page, the page below it does not receive clicks or hover while the transition runs. Keep transitions short so users do not notice this.
Why the default is a crossfade
You wrote no animation, yet the page fades. The browser's built-in stylesheet supplies the animations. A simplified version looks like this:
/* Simplified from the browser's built-in stylesheet. You do not write this. */
:root {
view-transition-name: root;
}
::view-transition-group(*) {
animation-duration: 0.25s;
animation-fill-mode: both;
}
::view-transition-image-pair(*) {
isolation: isolate;
}
::view-transition-old(*),
::view-transition-new(*) {
mix-blend-mode: plus-lighter;
animation-duration: inherit;
animation-fill-mode: inherit;
}
::view-transition-old(*) {
animation-name: -ua-view-transition-fade-out;
}
::view-transition-new(*) {
animation-name: -ua-view-transition-fade-in;
}The real rules use internal animation names that start with -ua-, so you cannot reference them yourself. The names are not part of the API, but the behavior is.
Two details explain how the default looks:
- Timing. The default duration is 0.25 seconds, applied to the group. The old and new images inherit it.
- Blending.
mix-blend-mode: plus-lighteradds the colors of the two images instead of layering one over the other. While old fades out and new fades in, the sum stays at full brightness. Ordinary alpha blending would make the page look slightly see-through halfway through, because 50% of one image over 50% of another does not add up to a solid surface.
The group also animates width, height and transform. For root the old and new boxes are normally identical, so nothing visibly moves. That group animation matters later, when a named element changes position or size between pages.
Making the transition visible
At 0.25 seconds you cannot see the structure. The usual way to study a transition is to slow it down. Put this in a stylesheet that both pages load:
@view-transition {
navigation: auto;
}
::view-transition-group(root) {
animation-duration: 3s;
}
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 3s;
}Setting only the group duration is a common mistake. The old and new images inherit duration through animation-duration: inherit, but only when you do not override them. In the built-in stylesheet that inheritance is written with the * wildcard on the same pseudo-elements. Once you write a more specific (root) rule for the group, the old and new images still pick up the group's value in current implementations. Setting all three explicitly avoids depending on that detail, and it keeps working when you later replace one of them with your own keyframes.
Use the wildcard when you want a rule to cover every named element at once:
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation-duration: 400ms;
}(*) matches every name, including root and any names you add later. (root) matches only the page-level snapshot.
Inspecting the tree
Because the tree exists only while the transition runs, you normally cannot catch it by looking at the Elements panel. Two approaches work:
- Set a long duration such as 3 seconds, as above, and open DevTools during the transition.
- Use the Animations panel in Chromium DevTools. It can pause and replay animations, and it lists the pseudo-element animations while they run.
Later chapters cover debugging in more detail. For now, treat the long duration as a standard step when you start working on a transition.
Common errors with the default tree
- Rules in the wrong stylesheet. The pseudo-elements are built in the new document. A
::view-transition-old(root)rule that exists only on the old page has no effect. - Removing the animation instead of replacing it.
animation: noneon old and new leaves both images fully opaque, with the new image on top. The page then swaps without a fade, but the group animation still keeps the overlay alive for its duration. - Forgetting
both. When you write your own animation, useanimation-fill-mode: both. Without it, the old image can flash back to full opacity before its animation starts, or the new image can show early. - Styling real elements. A selector like
.card::view-transition-old(root)does nothing. The pseudo-elements attach to the::view-transitionoverlay, so write them on their own or afterhtml. - Assuming the new image is a still. The new image is live. A video or a CSS animation on the new page keeps playing during the transition.
Summary of the model
The opt-in tells the browser to snapshot the old page and build a tree on the new one. With no names set, the tree has one branch, root. The built-in stylesheet fades the old image out and the new image in over 0.25 seconds using additive blending, while the group handles geometry. Everything you customize from here is a change to one of those five pseudo-element levels.
Exercise
Your site opts in with @view-transition { navigation: auto; } on every page and has no view-transition-name rules. You want the default crossfade to last one second. Which selectors do you style, which property do you set, and why does a rule on only ::view-transition-group(root) risk leaving the fade at a different length?
Show answer
Set animation-duration: 1s on ::view-transition-group(root), ::view-transition-old(root) and ::view-transition-new(root), or use (*) to cover every name. The fade itself is animated on the old and new images, not on the group. The group only animates size and position, which does not change for root. A group-only rule can leave the old and new images on the browser's default duration if they do not inherit it, so the fade stays 0.25 seconds. Put the rules in a stylesheet that the new page loads, because the pseudo-elements are built in the new document.