- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- View Transitions
CSS Animations & Transitions
View Transitions
Create coordinated transitions between page states and documents with named snapshots and view-transition pseudo-elements. You will name transition participants, style snapshot pseudo-elements, enable document navigation, and account for reduced motion.
View transitions animate the change from one page state to the next: a product photo that grows smoothly into the detail page, a list that slides into its new order. The browser takes a picture of before and after, and animates between the two.
You do not move elements by hand. The browser captures snapshots, shows them in a temporary layer of pseudo-elements, and cross-fades them by default. You decide which parts move on their own and how. For simpler effects on single elements, see Entry & Exit Transitions and CSS Animations.
Give an element its own animation
Example
css
.product-image {
view-transition-name: product-image;
display: grid;
min-height: 10rem;
place-items: center;
background: #dbeafe;
color: #172554;
transition: background-color 200ms;
}
.product-image.is-updated {
background: #bbf7d0;
}
::view-transition-old(product-image),
::view-transition-new(product-image) {
animation-duration: 300ms;
animation-timing-function: ease;
}By default the whole page cross-fades. An element with a view-transition-name is animated separately: if an element with the same name exists on the next page, the browser moves and resizes it from its old position to its new one. Each name must be unique on the page at any moment; two elements with the same name cancel the transition.
Only name the few elements that really need it, like the main image or the title. Naming everything makes transitions slower and confusing.
Transitions between pages
Example
css
@view-transition {
navigation: auto;
}
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*) {
animation-duration: 0.01ms;
}
}These few lines make normal link navigation between pages of your own site animate, with no JavaScript. Both pages need the @view-transition rule. The media query turns the animation off for people who prefer reduced motion.
For changes inside one page, such as filtering a list, JavaScript starts the transition with document.startViewTransition() around the code that updates the page.
While a transition runs
Example
css
:root:active-view-transition {
cursor: progress;
}
:root:active-view-transition button {
pointer-events: none;
}:active-view-transition matches only during the animation. Here it shows a "busy" cursor and stops buttons from being clicked twice.
View Transition Reference
| Syntax | Purpose | Example |
|---|---|---|
| view-transition-name | Creates an independently animated snapshot. | view-transition-name: hero |
| @view-transition | Enables cross-document navigation transitions. | @view-transition { navigation: auto; } |
| ::view-transition-old() | Selects the old snapshot. | ::view-transition-old(hero) |
| ::view-transition-new() | Selects the new snapshot. | ::view-transition-new(hero) |
| :active-view-transition | Matches the root while a view transition is active. | :root:active-view-transition |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>View Transitions example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
.product-image {
view-transition-name: product-image;
display: grid;
min-height: 10rem;
place-items: center;
background: #dbeafe;
color: #172554;
transition: background-color 200ms;
}
.product-image.is-updated {
background: #bbf7d0;
}
::view-transition-old(product-image),
::view-transition-new(product-image) {
animation-duration: 300ms;
animation-timing-function: ease;
}
@view-transition {
navigation: auto;
}
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*) {
animation-duration: 0.01ms;
}
}
:root:active-view-transition {
cursor: progress;
}
:root:active-view-transition button {
pointer-events: none;
}
</style>
</head>
<body>
<main><article><h1>View transition card</h1><button id="change-view" type="button">Change product</button><div class="product-image" id="product-image">Blue product</div><p>The named element receives an independent snapshot during the state change.</p></article></main><script>const button = document.querySelector("#change-view"); const image = document.querySelector("#product-image"); button.addEventListener("click", () => { const update = () => { image.classList.toggle("is-updated"); image.textContent = image.classList.contains("is-updated") ? "Green product" : "Blue product"; }; if (document.startViewTransition) { document.startViewTransition(update); } else { update(); } });</script>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| view-transition-name | 111 | 111 | 144 | 18 |
| @view-transition | 126 | 126 | No | 18.2 |
| :active-view-transition | 125 | 125 | 144 | 18 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Good to know
- If a browser does not support view transitions, the page simply changes without animation. Nothing breaks, so it is safe to add.
- Keep transitions short, around 200 to 400ms. A navigation that animates for a second feels slow.
View Transitions exercises
Name it
Give the image the view transition name hero.
The HTML your code works on
<img class="hero" src="https://html5andcss3.org/samples/star.svg" alt="Star" width="80">
<h1 class="title">Products</h1>Result
