The View Transitions API
By Flavio
Tagged: Web Platform
Create smooth same-document animations with the View Transitions API using startViewTransition, CSS pseudo-elements, and view-transition-name on elements.
The View Transitions API lets the browser animate a page from one visual state to another without a lot of manual setup. Instead of keeping two copies of your interface in the DOM or calculating every position yourself, you let the browser take a snapshot of the old state, run your DOM update, take a snapshot of the new state, and animate between the two.
That works well for tabs, filtered lists, image galleries, and full page navigations. Because it’s built around progressive enhancement, the content still updates normally in browsers that don’t support the animation.
Start with a small same-document transition
Say you’ve got a simple page with two buttons and a panel:
<nav>
<button data-view="inbox">Inbox</button>
<button data-view="archive">Archive</button>
</nav>
<main id="messages"></main>
And you already have a function that swaps the panel content:
function showMessages(view) {
const messages = view === 'inbox'
? ['Welcome', 'Your receipt']
: ['Old newsletter']
document.querySelector('#messages').innerHTML = messages
.map(message => `<article>${message}</article>`)
.join('')
}
To animate that swap, wrap the call in document.startViewTransition():
document.querySelector('nav').addEventListener('click', event => {
const button = event.target.closest('button')
if (!button) return
document.startViewTransition(() => {
showMessages(button.dataset.view)
})
})
Even without writing any extra CSS, you’ll get a quick crossfade. The real DOM change still happens inside your callback, and the browser just layers the transition on top of it, so get your state update working first and wrap the animation around it after.
Add a fallback
You don’t want your UI to break in a browser that doesn’t support startViewTransition(). Write a small helper that checks for the method and falls back to running the update directly:
function transition(update) {
if (!document.startViewTransition) {
update()
return
}
document.startViewTransition(update)
}
Then update your click handler to use that helper:
transition(() => {
showMessages(button.dataset.view)
})
Older browsers run showMessages() right away with no delay, while newer ones animate the swap.
What the browser creates
While a transition is running, the browser builds a temporary tree of CSS pseudo-elements. The three you’ll reach for most often are:
::view-transition-old(root)for the snapshot of the old page::view-transition-new(root)for the snapshot of the new page::view-transition-group(root)for the wrapper holding both
You can target those pseudo-elements in CSS to change the default timing:
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 250ms;
animation-timing-function: ease-out;
}
Once the animation finishes, the browser removes those pseudo-elements, so nothing stays duplicated in your DOM.
Animate one element independently
A full-page crossfade is useful on its own, though shared elements are what make a transition feel connected. Say you have a product thumbnail that turns into the big image on a detail view. Give both versions the same view-transition-name:
.product-image {
view-transition-name: product-image;
}
They don’t have to be the same DOM node. As long as they represent the same visual object before and after the update, the browser treats them as a pair and animates the captured image from its old size and position to the new one. You can customize that named transition on its own too:
::view-transition-group(product-image) {
animation-duration: 400ms;
animation-timing-function: ease-in-out;
}
One catch to watch for: every active view-transition-name on the page has to be unique. If two visible elements share product-image at the same time, the browser can skip the transition altogether, so only name the elements that need their own layer.
Wait for the DOM update
Your callback can return a promise, and the browser will wait for it before capturing the new state:
const transition = document.startViewTransition(async () => {
const response = await fetch('/api/messages')
const messages = await response.json()
renderMessages(messages)
})
try {
await transition.updateCallbackDone
} catch (error) {
console.error('Could not update messages', error)
}
Just be careful not to put slow work inside that callback. The browser pauses the visual update while it runs, so fetch your data first whenever you can, then start the transition around the fast DOM change.
startViewTransition() returns an object with three promises you can hook into if you need finer control:
updateCallbackDonesettles when your update callback finishesreadysettles when the new state is captured and the animation can beginfinishedsettles when the transition ends
Most of the time you won’t need them and the callback alone is enough. They’re handy when later code has to wait for a specific phase of the transition.
Cross-document transitions
You can use the same idea to animate navigation between two separate pages on the same origin. Opt both pages in with CSS first:
@view-transition {
navigation: auto;
}
After that, normal links keep working as usual:
<a href="/products/coffee-grinder/">View product</a>
In a browser that supports cross-document transitions, clicking a link captures the old page and the destination page and animates between them. Without support, the link just does a normal navigation.
Shared elements work across pages too: put the same view-transition-name on matching elements in both documents, and make sure both pages opt in. Since browser support for cross-document transitions still varies, check current compatibility before making the effect central to your design.
If you’re using Astro, take a look at Astro view transitions and dark mode for a framework-level setup.
Respect reduced motion
Some people turn on reduced motion in their operating system, so treat animations as an enhancement and disable them when that setting is active:
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}
Your DOM updates still run as usual, just without the movement.
Common mistakes
Don’t start with a complex animation. Get the state change working with the default crossfade first, then add one named element at a time once everything behaves.
Another easy trap is doing slow work inside the callback. Prepare your data ahead of time so the visual update stays short, and keep in mind that users interact with the updated document rather than the old captured image. Treating the snapshot like live UI, or using a long transition to hide an incomplete state, will trip you up quickly.
My advice is to save view transitions for moments where motion explains continuity, like a selected card opening, a tab switching, or a filtered list rearranging. If the movement is just decoration and doesn’t help the user follow what changed, skip it. And when you want an animation driven by scroll position instead of a DOM update, reach for scroll-driven CSS animations.
Want me to talk about your product? You can sponsor this site.
Related posts about platform: