Sub topic 2
Turning it on with @view-transition
This chapter covers the opt-in rule that enables cross-document view transitions. It explains what the rule does, where it goes, and what to check when nothing animates.
It assumes you know basic CSS (at-rules, selectors, @keyframes), that each link click on a multi-page site loads a new HTML document, and that same-origin means the same scheme, host, and port. Later chapters cover which navigations qualify, the pseudo-element tree, and custom animations. This chapter stays on the switch itself.
What the rule does
A cross-document view transition is off by default. Without an opt-in, the browser unloads the old page and paints the new one. The pages do not share any state, so the browser needs both of them to agree that a transition is wanted.
The opt-in is a CSS at-rule:
@view-transition {
navigation: auto;
}navigation is the only descriptor this rule has. It takes two values:
| Value | Meaning |
|---|---|
| `none` | The default. This page does not take part in cross-document transitions. |
| `auto` | This page allows a transition when the navigation is eligible. |
The word auto does not mean "always animate". It means "animate if the browser decides the navigation qualifies". Chapter 3 covers those conditions.
Both pages must opt in
A transition involves two documents: the page being left (the old page) and the page being opened (the new page). The browser starts a transition only if both documents have navigation: auto in effect.
Suppose / has the rule and /posts/intro/ does not. Navigating from / to the post gives you no animation. Navigating back gives you none either. The new page must opt in as well, because it takes part in the animation by supplying the "new" snapshot.
The simplest way to satisfy this is to put the rule in a stylesheet that every page loads.
<!-- Every page of the site includes the same shared stylesheet. -->
<head>
<meta charset="utf-8">
<title>My blog</title>
<link rel="stylesheet" href="/css/site.css">
</head>/* /css/site.css */
@view-transition {
navigation: auto;
}
body {
font-family: system-ui, sans-serif;
}You can also put the rule in a <style> element inside each page. A shared stylesheet is easier to keep consistent.
What happens when you navigate
With both pages opted in, a link click goes through these steps:
- The browser captures the old page as an image-like snapshot and keeps it on screen.
- The old document is unloaded and the new document is fetched and parsed.
- When the new page is ready to render, the browser captures its live state.
- The browser runs the default crossfade, or your own animation, between the two.
- When the animation finishes, the live new page is shown.
You write no JavaScript to get this far. The default animation is a short crossfade of the whole page.
Where the rule can go
The at-rule is a top-level rule. Place it at the top level of a stylesheet, or inside a <style> element. It cannot be nested inside a selector rule. It also is not meant to be wrapped in a media query for per-page logic. If you need to vary behavior per navigation, use the events and transition types from later chapters.
The rule is read when the page is rendered, so it needs to be present in the document's CSS at that time. A stylesheet that loads late, or one injected by script after the first render, can be too late for the first navigation to take part. Use a normal <link rel="stylesheet"> in the <head> for reliability.
Opting out on part of a site
If most of the site should animate but one section should not, you have two choices. You can leave the rule out of the pages in that section. Or you can include it everywhere and serve a different stylesheet to the section. A transition runs only between two opted-in pages, so a page without the rule on either end of the navigation prevents it.
Some teams want only certain pages to take part, for example a blog and its list page but not a checkout flow. Leaving the rule out of checkout pages gives you that without any extra code.
A minimal working example
This is a two-page site you can run locally. Save the files in one folder and serve them over HTTP. Opening them as file:// URLs does not work, because file URLs are not treated as same-origin to each other.
<!-- index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Home</title>
<link rel="stylesheet" href="site.css">
</head>
<body>
<h1>Home</h1>
<p>Welcome. <a href="about.html">About this site</a></p>
</body>
</html><!-- about.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>About</title>
<link rel="stylesheet" href="site.css">
</head>
<body>
<h1>About</h1>
<p>This is the second page. <a href="index.html">Back home</a></p>
</body>
</html>/* site.css */
@view-transition {
navigation: auto;
}
body {
margin: 2rem;
font-family: system-ui, sans-serif;
}
html {
background: #fff;
}Serve the folder with any static server, for example python3 -m http.server 8000. Open http://localhost:8000/index.html in a Chromium-based browser and click the link. The content crossfades instead of cutting. Click back and it crossfades again.
To confirm that the rule is what causes the animation, delete the @view-transition block from site.css and reload. The page now cuts instantly. Remove it from only one of the two pages, with separate stylesheets, and the result is the same.
Why a transition may not run
A missing animation usually has one of these causes. Check them in this order.
- The rule is missing on one page. Check the old page and the new page. Check pages you reach by redirect as well.
- The navigation is cross-origin. A link from
example.comtoother.examplenever gets a transition. A redirect chain that leaves the origin and comes back also disqualifies the navigation. - The stylesheet is not applied in time. A late or blocked stylesheet means the rule is not seen.
- The browser lacks support. Unsupported browsers ignore the at-rule and navigate normally. That is the intended fallback, and a later chapter covers it.
- The page is slow to render. The browser gives up on a transition if the new page takes too long to be ready. The navigation still works, but it is not animated.
- The user has asked for reduced motion and you disabled the animation. This is covered in its own chapter.
Opt-in and the rest of the feature
The opt-in rule is the first of several pieces. Everything else builds on it:
- The pseudo-element tree lets you style the snapshots. It exists only when a transition is running, so it needs the opt-in.
- The
pageswapandpagerevealevents fire on both pages. They give you aviewTransitionobject only when the navigation takes part in a transition. - Transition types and
view-transition-nameassignments apply to a transition that is already enabled.
Because of this, a broken opt-in looks like several problems at once. Your custom animations, shared element morphs, and events can all seem not to work. If any of them fails, verify the opt-in on both pages first.
Common mistakes
- Adding the rule to the list page and forgetting the detail page. The forward navigation fails, and so does back.
- Testing with
file://URLs. Use a local web server. - Putting the rule inside a selector block or inside an
@mediablock meant to control it per page. - Assuming a same-site link, for example
www.example.comtoshop.example.com, qualifies. Same-site is not same-origin, so it does not. - Expecting transitions on a page reached by a form POST or a reload. Those are covered with eligibility in the next chapter.
Exercise
You add @view-transition { navigation: auto; } to the stylesheet of your blog's list page, but clicking a post shows a plain cut with no animation. The post pages use a different stylesheet that does not contain the rule. What is wrong, and what is the smallest fix?
Show answer
Both documents in a navigation must opt in, and the post page, which is the new page, has not. Add the same @view-transition { navigation: auto; } rule to the post pages' stylesheet. A cleaner fix is a shared stylesheet that every page loads, so the two pages cannot drift apart.
Exercise
A teammate says that setting navigation: auto guarantees that every link click animates. Give two reasons this is wrong.
Show answer
Any two of these: the navigation may be cross-origin; the browser may skip the transition if the new page is slow to render; the browser may not support the feature; the stylesheet with the rule may not be applied in time; the user's reduced-motion setting may have disabled the animation. The value means the page allows a transition when the navigation is eligible.