View transitions

Advanced · 14 min

How the View Transitions API snapshots old and new state and animates between them, within a page or across navigations, using view-transition-name.

Before thisScroll-driven animationsTransitions

camelCase · 14 min

View transitions

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by camelCaseReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A view transition takes a snapshot of the old state, lets the DOM update, takes a snapshot of the new state, then animates between the two images. You never have to keep both versions of the page alive at once yourself.
  • Within one page, wrap the update in `document.startViewTransition(() => { ... })`. Between pages of the same site, opt in with `@view-transition { navigation: auto; }` in CSS on both pages.
  • By default the whole page cross-fades over a fraction of a second. Giving an element a unique `view-transition-name` lifts it out into its own group, so it can move and resize between its old and new position.
  • You customise the animation with ordinary CSS on pseudo-elements such as `::view-transition-old(name)`, `::view-transition-new(name)` and `::view-transition-group(name)`.
  • Each `view-transition-name` must be unique in the document at the time of the snapshot, and the whole thing should be skipped for users who prefer reduced motion.

The hard part of page animation

Animating a change inside one element is easy. Animating a change across a whole page is hard. Say a card list reorders, or a thumbnail grows into a hero image on the next page, or the theme flips from light to dark. To animate that yourself, you need both the old and the new version of the elements on screen at once, positioned and cross-faded by hand. Frameworks built large libraries around this.

The View Transitions API makes the browser do the bookkeeping. It takes a picture of the old state, lets you change the page, takes a picture of the new state, and then animates between the two pictures. While it runs, the real page is not what you see, so you do not have to coordinate old and new elements.

Same-page transitions

function toggleTheme() {
  if (!document.startViewTransition) {
    document.documentElement.classList.toggle('dark');   // fallback
    return;
  }
  document.startViewTransition(() => {
    document.documentElement.classList.toggle('dark');
  });
}

The callback is where the DOM changes. With no other CSS, the whole page cross-fades from old to new. Same-document transitions are in Chrome 111, Safari 18 and Firefox 144 or later according to MDN’s compatibility data, so keeping the if fallback is still good practice.

Cross-document transitions

For ordinary links between pages on one site, no JavaScript is needed:

@view-transition {
  navigation: auto;
}

Include it on both pages. Cross-document support arrived later than same-document: Chrome 126 and Safari 18.2 per the data, with Firefox listed as in preview. Browsers without support load the next page as they always did.

Morphing one element: view-transition-name

A name pulls an element out of the root snapshot into its own group, and the browser matches old and new elements with the same name:

.product-card.active img { view-transition-name: hero; }
.detail img              { view-transition-name: hero; }

During the transition the image is moved and resized from the old position and size to the new one, while the rest of the page cross-fades. Names must be unique: if two elements have the same name when the snapshot is taken, the transition is skipped. In a list, set the name only on the item being opened.

The pseudo-element tree

While a transition runs, the browser builds this structure, which you can style:

::view-transition
└─ ::view-transition-group(name)
   └─ ::view-transition-image-pair(name)
      ├─ ::view-transition-old(name)
      └─ ::view-transition-new(name)

root is the name given to the whole page. Because the default animations are plain CSS animations, you override them with plain CSS:

::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 400ms;
}

@keyframes slide-out { to { translate: -100% 0; } }
::view-transition-old(root) { animation-name: slide-out; }

You can also group several names under one class with view-transition-class to style them together, which is useful for a list of cards.

Respect reduced motion

A page-wide movement is exactly what the reduced motion setting is for:

@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) { animation: none; }
}

Limits worth knowing

  • The page content is held on the old snapshot during the animation, so a slow callback makes the page feel frozen. Keep the update quick.
  • Snapshots are images, so text may look slightly different from live text for a moment, and elements inside a snapshot are not interactive.
  • Named groups are drawn in a layer above the page, so an element’s own z-index has no effect while its transition runs.

What the video covers

camelCase’s “CSS View Transitions Will Change Web Design Forever” (May 2025, about 14 minutes) has chapters: Intro, Opt-in to View Transitions, Your First View Transition, Under the Hood, Fine-tuning, and Same-document View Transitions. It shows both multi-page and single-page use, per its description, and links a demo repository. The channel is small, about 30 thousand subscribers at the time of checking. I could not read a transcript, so the specific code is not described. The pseudo-element tree and the reduced motion handling above are from the documentation, not from the video.

Where this fits

View transitions finish the animation group: transitions for a property, keyframes for a sequence, scroll-driven animations for scroll-bound motion, and this for whole-page changes. The next group, modern CSS, starts with custom properties, which make themes like the one above cheap to write.

Check yourself

  1. 01In a list of cards where clicking one opens its detail view, why can't every card share `view-transition-name: card`?

    Two elements with the same name in the same snapshot make the transition invalid and it is skipped. The name identifies which old element becomes which new element, so each must be unique. Set the name inline or in CSS only on the one card being opened, for example via a custom property per item.

  2. 02What does `document.startViewTransition(callback)` actually do between the old and new snapshots?

    It captures the current rendering, runs your callback (which should update the DOM, possibly asynchronously), waits for it, captures the new rendering, and then runs an animation between the two captured images using pseudo-elements. During the callback the page is held frozen so the user does not see a flash.

  3. 03How do you opt in to view transitions between two separate HTML pages on the same origin?

    Put `@view-transition { navigation: auto; }` in the CSS of both the page you leave and the page you arrive on. No JavaScript is required for the default cross-fade, and `view-transition-name` on matching elements lets them morph across the navigation.

  4. 04You want the default fade slower. Which selector do you target?

    The root snapshot's pseudo-elements: `::view-transition-old(root), ::view-transition-new(root) { animation-duration: 0.6s; }`. The default animations are ordinary CSS animations on those pseudo-elements, so you can override duration, easing or write your own keyframes.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark