Sub topic 4
Understanding the default cross-fade
This chapter assumes you can call document.startViewTransition() and know that the browser captures an old snapshot, runs your update, then captures a new one. Here you will see what the default animation does, and why it behaves that way.
What you get without writing CSS
Call startViewTransition() and the page cross-fades. The browser builds this effect from a few pseudo-elements, which are generated nodes you can style but not select in the DOM.
For the unnamed page content, the browser creates a pair called root. ::view-transition-old(root) shows the old snapshot, and ::view-transition-new(root) shows the new one. Both sit inside a ::view-transition-group(root) and a ::view-transition-image-pair(root).
How the default is built
The user agent stylesheet gives the old and new images these animations:
::view-transition-old(*)runs a built-in-ua-view-transition-fade-outanimation.::view-transition-new(*)runs-ua-view-transition-fade-in.- The image pair uses
isolation: isolate, and both images usemix-blend-mode: plus-lighter. - The default duration is 0.25 seconds.
The plus-lighter blend adds the colors of the two layers. While one image fades out and the other fades in, their combined opacity stays at 100%. Without it, a normal blend would show a dip where the page looks slightly see-through in the middle of the fade.
Why the default only cross-fades
The snapshots are static images of each state. The browser cannot tell which parts of the page moved. For an element without a view-transition-name, it can only blend the whole picture of the old page into the whole picture of the new page. The root group does animate size and position, but both are normally the viewport, so you see nothing move.
Common errors
- Expecting layout motion. A list item that changes position will not slide. It cross-fades in place, because it is part of the root snapshot. Giving it a name is covered in a later chapter.
- Trying to select the pseudo-elements with
querySelector. They are not in the DOM. Use CSS rules, orgetComputedStyle(el, '::view-transition-old(root)')after the transition is ready. - Shortening only one side. If you change the duration of
old(root)and leavenew(root)at 0.25 seconds, the two fades go out of step and the page looks wrong. - Setting
animation-durationon:root. That does not reach the pseudo-elements. Target them directly.
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 600ms;
}This slows both sides together and keeps the built-in fade keyframes and blend mode.
Exercise
Why does the default cross-fade not show a dip in brightness halfway through, even though one image fades out while the other fades in?
Show answer
The old and new images use mix-blend-mode: plus-lighter, which adds their colors. As one image's opacity falls, the other's rises by the same amount, so the combined result stays at full opacity.
Exercise
You set animation-duration: 600ms only on ::view-transition-new(root). What happens?
Show answer
The new snapshot fades in over 600ms, while the old snapshot still fades out over the default 250ms. The two fades are out of step, so you will see the old page disappear early and the new page appear partly transparent. Set the same duration on both pseudo-elements.