View transitions
Definition and purpose
View transitions animate between visual snapshots of an old and a new UI state. The underlying state update must work even without the animation.
Named label
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { view-transition-name: status; } </style> </head> <body> <button id="toggle" type="button">Change label</button> <p id="a">Draft</p> <script> const item=document.querySelector("#a");document.querySelector("#toggle").addEventListener("click",()=>{const update=()=>{item.textContent=item.textContent==="Draft"?"Saved":"Draft"};if(!document.startViewTransition||matchMedia("(prefers-reduced-motion: reduce)").matches)update();else document.startViewTransition(update);}); </script> </body></html>Click to change the text. The named label can transition separately from the root.
Syntax and mental model
For a same-document transition, document.startViewTransition receives a DOM update callback. A unique view-transition-name identifies a participating element. The browser creates pseudo-elements such as ::view-transition-old(name) and ::view-transition-new(name), grouped under ::view-transition-group(name). For eligible cross-document transitions, both same-origin documents opt in with @view-transition { navigation: auto; }. Navigation does not use the same-document callback. The example bypasses animation when unsupported or reduced motion is requested.
Control snapshot duration
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { view-transition-name: status; } ::view-transition-group(status) { animation-duration: 250ms; } </style> </head> <body> <button id="toggle" type="button">Change label</button> <p id="a">Draft</p> <script> const item=document.querySelector("#a");document.querySelector("#toggle").addEventListener("click",()=>{const update=()=>{item.textContent=item.textContent==="Draft"?"Saved":"Draft"};if(!document.startViewTransition||matchMedia("(prefers-reduced-motion: reduce)").matches)update();else document.startViewTransition(update);}); </script> </body></html>The group duration is set to 250ms; the update callback still handles state.
Common mistakes
Giving two simultaneously rendered participants the same name can skip the transition. Do not wait for animation completion before making essential state changes.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Starting styles and discrete transitions. Next lesson: Animation performance.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
View transitions — knowledge check
Five questions on view transitions.
What triggers same-document transitions?
What must participant names be?
What represents the old captured state?
What is required for eligible cross-document transitions?
What if support is absent?