Sub topic 1
What a cross-document view transition is
Prerequisites
You should know basic HTML and CSS, including selectors and @keyframes. You should also understand that a multi-page site loads a new document when a link changes the URL. Basic JavaScript event listeners are useful for later chapters.
What a cross-document view transition is
A cross-document view transition animates between two separate HTML documents. It does not require a single-page application router.
During navigation, the browser captures a snapshot of the old page and prepares a snapshot of the new page. It displays these snapshots in a temporary transition layer, then animates between them. The browser can therefore animate a full page change instead of showing an abrupt replacement.
Turning it on with `@view-transition`
Both the page being left and the page being opened must opt in. Add this rule to the stylesheet used by both documents:
@view-transition {
navigation: auto;
}With this opt-in, the browser uses its default animation, usually a short crossfade. Without it, the navigation behaves like a normal full page load.
Which navigations get a transition
The transition is progressive enhancement. Supported browsers can animate eligible navigations; other browsers continue to load pages normally.
Typical eligible navigations include:
- Same-origin links, where both URLs belong to the same origin.
- Back and forward history navigation.
- Navigations where the browser can capture both documents successfully.
Cross-origin navigations generally cannot use this cross-document transition. Downloads, redirects, unsupported browser features, or navigations that do not produce compatible documents can also skip it.
The pseudo-element tree and the default crossfade
The browser exposes transition snapshots through a special pseudo-element tree. The root is ::view-transition. Under it, the default page transition uses a root group, an image pair, and old and new snapshots:
::view-transition-group(root)controls the transition group.::view-transition-image-pair(root)contains both snapshots.::view-transition-old(root)represents the outgoing page.::view-transition-new(root)represents the incoming page.
The default animation fades the old snapshot out while fading the new snapshot in. Later, you can style these pseudo-elements with ordinary CSS animations to create slides, fades, or other effects.
Exercise
What must be true for a default cross-document view transition to run?
Show answer
The browser must support the feature, the navigation must be eligible, and both documents must opt in with @view-transition { navigation: auto; }.