Sub topic 1
What a Cross-Document View Transition Is
A cross-document view transition animates the move from one page of a multi-page site to another. The browser does the animation. You don't need a single-page-app router or a JavaScript framework. This chapter explains the working model: what the browser captures, when it captures it, and what the animation is made of. Later chapters cover the details of opting in, styling, and debugging.
Prerequisites
This chapter assumes you already know:
- HTML and CSS basics, including selectors and
@keyframesanimations. - How a multi-page site works. A link click makes the browser request a new URL, discard the current document, and render the new one.
- Basic JavaScript event listeners and DOM querying.
- What "same origin" means: the same scheme, host, and port.
The problem: navigation is a hard cut
On a traditional site, clicking a link tears down the current document and builds a new one. The user sees the old page, sometimes a blank flash, and then the new page. Nothing connects the two states.
Single-page apps avoid this by keeping one document alive and swapping content with JavaScript. That brings costs: a client-side router, history management, focus handling, and more JavaScript to ship. Many sites don't want those costs only to get a smooth transition.
Cross-document view transitions give a multi-page site a third option. Each page stays a normal document with its own URL, and the browser animates between the two documents.
The core idea: snapshots
The browser can't animate two live documents at once, because only one document is on screen at a time. Instead it works with snapshots, which are rendered images of what each page looks like.
- Just before the old page goes away, the browser captures an image of it. This is the old snapshot.
- The new page loads and is prepared for its first render. The browser holds back that first paint.
- The browser builds a tree of temporary pseudo-elements on top of the new page. These pseudo-elements hold the old snapshot and a live view of the new page.
- By default the old image fades out while the new view fades in. This is a short crossfade.
- When the animation ends, the pseudo-elements are removed and the new page is shown normally.
The two snapshots differ in kind:
- The old snapshot is a static image. It is frozen at the moment of capture.
- The new snapshot is a live representation of the new page. If the new page contains a playing video or a running CSS animation, that content keeps updating during the transition.
The animation is ordinary CSS. The pseudo-elements can be selected and animated with @keyframes, animation, and transition properties like any other element. The next chapters cover that.
The sequence of events
The old and new documents each take part, in a fixed order. Each page fires an event at the point where it can influence the transition. Those events are covered in detail later in the tutorial. Here is the order in which they occur:
The key point is that the capture happens in the old document, and the animation runs in the new one. The old document is already gone by the time the animation plays. Only its image remains.
Minimal working example
The following two pages and one stylesheet are enough to see a transition. Save them in one folder and serve the folder over HTTP, for example with python3 -m http.server. The pages must be same-origin, and opening them as file:// URLs will not work.
style.css:
@view-transition {
navigation: auto;
}
body {
font-family: system-ui, sans-serif;
margin: 2rem;
}
.home {
background: #e8f0fe;
}
.about {
background: #fde8e8;
}index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home</title>
<link rel="stylesheet" href="style.css">
</head>
<body class="home">
<h1>Home</h1>
<p><a href="about.html">Go to About</a></p>
</body>
</html>about.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>About</title>
<link rel="stylesheet" href="style.css">
</head>
<body class="about">
<h1>About</h1>
<p><a href="index.html">Back to Home</a></p>
</body>
</html>In a browser that supports cross-document view transitions, clicking either link crossfades between the blue and pink pages instead of cutting. The @view-transition rule is the opt-in. Both pages load the same stylesheet, so both opt in. The next chapter explains why both pages need it.
How it differs from same-document transitions
View transitions first shipped for single-page apps, where JavaScript calls document.startViewTransition(). The cross-document version uses the same rendering model but a different trigger.
| Aspect | Same-document | Cross-document |
|---|---|---|
| How it starts | JavaScript calls `document.startViewTransition()` | CSS `@view-transition` rule on both pages |
| What changes | DOM of one document | One document replaced by another |
| Where the old snapshot is taken | Same page, before the DOM update | Old page, as it is being left |
| Where the animation runs | Same page | New page |
| Scope | Any DOM update you wrap | Navigations between same-origin pages |
| JavaScript required | Yes | No, except for per-navigation logic |
The pseudo-element tree and the styling approach are the same in both. What you learn about ::view-transition-old() and ::view-transition-new() applies to both.
What the model implies
Because the transition is built from snapshots, several behaviors follow directly.
- The old page is an image. You can't interact with it, and scripts on the old page no longer run during the animation. Anything you want to change about how the old page looks must happen before it is captured.
- The new page is live but covered. Its content renders underneath the pseudo-elements. Late-loading content (for example, an image that arrives after first paint) can pop in during the animation or be missed by it.
- Timing is bounded. The browser can't wait forever for the new page. If the new page takes too long to become ready, the browser skips the transition and shows the new page directly. The user still gets a working navigation.
- Snapshots cost memory and GPU work. A snapshot of the whole page is one large image. Each element you give its own name adds another snapshot pair. Chapter 14 returns to this.
- The default is the whole page. Without further work, the entire viewport is treated as one element named
root. That is why the default effect is a crossfade of the whole page.
Common misunderstandings
A few mistaken ideas come up often.
- "It turns my site into a single-page app." It doesn't. Each navigation is still a full document load with its own URL, history entry, and fresh JavaScript state.
- "Elements persist across pages." They don't. A shared element looks continuous only because the old and new snapshots of the same named element are animated into one another. The old DOM node is gone, and a different node in the new document takes its place.
- "Scripts keep running across the transition." They don't. State kept in JavaScript variables is lost on navigation, as it always was.
- "I can animate anything I like in the old page after the click." The old page is captured before it leaves. Animation happens afterward, using that image.
Summary of the working model
A cross-document view transition has three parts: a capture of the old page, a live view of the new page, and a pseudo-element tree that animates between them using ordinary CSS. The browser controls the timing. Your code decides whether to opt in, what the animation looks like, and which elements are matched across pages.
Exercise
A user clicks a link from page A to page B. Both pages are same-origin and both include the @view-transition { navigation: auto; } rule. Which document is the old snapshot taken from, which document plays the animation, and is the old snapshot live or static?
Show answer
The old snapshot is taken from page A as the browser leaves it. The animation plays on page B, which is the new document. The old snapshot is a static image, while the new side is a live view of page B.
Exercise
You add @view-transition { navigation: auto; } to the stylesheet of your list page only. The detail page has no such rule. You click a link from the list page to the detail page. What do you expect to see, and why?
Show answer
You see an ordinary hard-cut navigation with no transition. Both the page being left and the page being opened must opt in, so one-sided opt-in does nothing. The navigation itself still works normally.