How to Use the CSS View Transitions API for Native App Morphs
The CSS View Transitions API enables native app-like animated state changes by capturing a snapshot of the current DOM before an update, capturing a snapshot of the new DOM after the update, and cross-fading or morphing between them using CSS pseudo-elements (::view-transition-old and ::view-transition-new). In JavaScript, invoke it with 'document.startViewTransition(() => updateDOM())'.
Step 1: The Core Mechanism: document.startViewTransition()
To trigger a view transition in single-page workflows, wrap your DOM state mutation (like filtering a product list, switching tabs, or opening a modal) inside 'document.startViewTransition()'. The browser automatically freezes the screen, takes a screenshot of the old state, executes your callback to update the DOM, takes a screenshot of the new state, and animates the transition with GPU-composited cross-fades.
// Progressive enhancement check
function updateTheme(newTheme) {
if (!document.startViewTransition) {
applyNewTheme(newTheme);
return;
}
// Animate smoothly between old and new state
document.startViewTransition(() => {
applyNewTheme(newTheme);
});
}
Step 2: Naming Elements with view-transition-name for Shared Morphing
By default, the browser cross-fades the entire page. To morph specific elements individually (such as a thumbnail card expanding into a full hero banner), assign a unique 'view-transition-name' to both elements. The browser automatically calculates the bounding box difference and morphs size and position seamlessly.
/* Thumbnail in grid */
.product-thumb-42 {
view-transition-name: product-hero-42;
}
/* Expanded detail view on target page */
.product-detail-hero {
view-transition-name: product-hero-42;
}
Step 3: Cross-Document Transitions for Traditional Multi-Page Sites (MPA)
Starting in Chrome 126 and Safari 18, View Transitions work on traditional multi-page static websites without any JavaScript framework! Simply add '@view-transition { navigation: auto; }' to your stylesheet. When users click standard HTML links, the browser morphs elements across page loads natively.
/* Enable native multi-page transitions */
@view-transition {
navigation: auto;
}
/* Customize transition duration */
::view-transition-group(root) {
animation-duration: 0.35s;
animation-timing-function: cubic-bezier(0.2, 0, 0, 1);
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What happens in browsers that do not support the View Transitions API?
The View Transitions API is built for progressive enhancement. If 'document.startViewTransition' is not supported, you simply run your DOM update immediately. The user experiences an instantaneous page update with zero errors.
Can you use View Transitions with React, Vue, or Svelte?
Yes! In React 18+, wrap state updates inside 'document.startViewTransition(() => flushSync(() => setState(val)))'. Modern routers like Next.js App Router, Astro, and SvelteKit have built-in View Transition flags.
Can view-transition-name be shared by multiple elements at the same time?
No. At any given moment during a transition, a 'view-transition-name' must be unique in the DOM. If two simultaneous elements share the same name, the transition will skip animation to avoid ambiguous coordinate math.
How do you disable View Transitions for users with prefers-reduced-motion?
Use standard media queries: '@media (prefers-reduced-motion: reduce) { ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; } }'.
What browser versions support cross-document View Transitions?
Single-page View Transitions are supported in Chrome 111+, Edge 111+, and Safari 18+. Cross-document MPA '@view-transition' navigation is supported in Chrome 126+ and Safari 18.2+.