Sub topic 2
Turning it on with @view-transition
This chapter covers the opt-in rule that enables cross-document view transitions. It assumes you know CSS selectors and @keyframes, and that you understand each URL on a multi-page site returns its own document. A basic grasp of the same-origin policy also helps.
What the rule does
By default, clicking a link on a multi-page site unloads the old page and paints the new one. Nothing connects the two. The @view-transition at-rule tells the browser to capture the old page, load the new one, and animate between the two states.
/* Include in the stylesheet of every page that should take part. */
@view-transition {
navigation: auto;
}The navigation descriptor has two values:
auto: the page takes part in view transitions for eligible navigations.none: the page does not take part. This is the default.
With auto and no other CSS, the browser runs a short crossfade of the whole page. You get a visible effect from three lines.
Both pages must opt in
A transition happens between two documents, so both must agree. The page being left and the page being opened each need the rule. If only one has it, the navigation happens as normal, with no animation and no error.
The simplest way to avoid this mistake is to put the rule in a stylesheet that every page loads. The rule must be in CSS the document applies. You can't enable it from an inline style attribute, and you can't set it with JavaScript.
<!-- Every page links the same shared stylesheet. -->
<link rel="stylesheet" href="/css/site.css">The sequence of events
The diagram shows what the browser does when a user follows a link between two opted-in pages.
The old page stays on screen as a frozen image while the new one loads. That is why a slow response makes the transition feel stuck. A later chapter covers how to control this timing.
Checking that it works
To test the setup, follow these steps:
- Create two HTML files,
index.htmlandabout.html, that link to each other. - Add the
@view-transitionrule to a stylesheet that both files load. - Serve the files from a local web server, such as
python3 -m http.server. Opening them withfile://does not work, because the files don't share an origin. - Open the site in a browser that supports the feature and click between the pages.
You should see the page fade between the two states instead of flashing.
Common mistakes
| Symptom | Likely cause |
|---|---|
| No animation at all | Only one of the two pages has the rule |
| Works locally, fails in production | The pages are on different origins, or the stylesheet fails to load |
| Works on one link, not another | The target page uses a different stylesheet without the rule |
| Rule appears to be ignored | The rule is inside an invalid block or a media query that does not match |
Because the rule is plain CSS, an unsupported browser ignores it and navigates normally. You don't need a feature check just to turn the feature on.
Exercise
You added @view-transition { navigation: auto; } to list.html, but clicking a link to detail.html shows no animation, and no errors appear in the console. What is the most likely cause, and how do you fix it?
Show answer
detail.html probably does not have the rule. Both the page being left and the page being opened must opt in. Move the rule into a shared stylesheet that both pages load, then test again from a local web server.