Sub topic 1
What a Cross-Document View Transition Is
A cross-document view transition animates the change from one page to another when the user follows a normal link on a multi-page site. Each page is still a separate HTML document and the browser still performs a full navigation. The browser adds a visual transition between the old and new pages. You don't need a single-page-app router or a JavaScript framework.
Prerequisites
This chapter assumes you already know:
- HTML and CSS basics, including selectors and
@keyframesanimations. - That clicking a link on a traditional site loads a new document from its own URL.
- Basic JavaScript event listeners (used in later chapters).
- What "same origin" means: the same scheme, host, and port.
The working model
The browser handles a transition in four steps:
- The user starts a navigation. The browser captures a snapshot of the current page. A snapshot is a static image of how the page looks at that moment.
- The old document is unloaded. The browser loads the new document but holds back its first display.
- When the new page is ready to render, the browser treats it as a live view. It keeps rendering the page and doesn't freeze it as an image.
- The browser builds a tree of pseudo-elements, which are CSS-addressable boxes. It animates from the old snapshot to the live new view. By default this is a short crossfade of the whole page.
The old side is a picture and the new side is live. This asymmetry explains much of the behavior you will see later.
A first look
Both pages must opt in. This rule is the whole setup for the default effect:
@view-transition {
navigation: auto;
}Put it in the stylesheet of every page that should take part. The next chapter covers it in detail. Here it shows how little the basic case needs: no JavaScript, no new markup.
What it is not
- It is not an SPA router. Each navigation still loads a new document, and its scripts run again. No JavaScript state survives from the old page.
- It does not keep the old DOM alive. The old page is only an image during the animation.
- It does not bypass the same-origin rule. Both pages must be on the same origin.
Common errors
- Only one page opts in. If the old or the new page lacks the rule, the navigation happens with no transition. Check both stylesheets first.
- Expecting interaction with the old page. The old side is a snapshot, so clicks and text selection don't work on it.
- Expecting a transition on every navigation. The browser skips a transition when it is not eligible, for example when the navigation is cross-origin. A later chapter lists the cases.
- Assuming every browser supports it. An unsupported browser ignores the rule and navigates normally. Navigation still works, and only the animation is missing.
- Animating a slow page. The browser waits for the new page to be ready. If it takes too long, the browser may skip the transition instead of showing an unfinished page.
Exercise
A site has a list page and a detail page on the same origin. You add the @view-transition { navigation: auto; } rule to the list page only, and clicking a link shows no animation. Why, and what do you change?
Show answer
A cross-document transition needs both the page being left and the page being opened to opt in. Add the same @view-transition rule to the detail page's stylesheet. Then the browser can capture the old page, render the new page, and animate between them.