Sub topic 6
Naming elements with view-transition-name
This chapter assumes you can call document.startViewTransition() and know that the browser snapshots the old and new states. It also assumes you can read the ::view-transition pseudo-element tree and write basic CSS selectors and @keyframes. Here you will learn what view-transition-name does to an element, how names pair old and new snapshots, and which side effects to expect.
What the property does
By default, a view transition captures the whole page as one picture. That picture is named root, and it belongs to the <html> element. The browser cross-fades the old picture into the new one, so every part of the page moves in the same way.
view-transition-name splits an element out of that picture. An element with a name is captured on its own, and it gets its own set of pseudo-elements. You can then animate it separately from the rest of the page.
.site-header {
view-transition-name: site-header;
}The value is a <custom-ident>, which is an unquoted identifier like a CSS class name without the dot. The initial value is none, which means the element is not captured separately. You cannot use none as a name, because it is the keyword for "no name".
Names pair old and new
The browser matches snapshots by name, not by DOM identity. Before the update callback runs, it records every named element and its name. After the callback finishes, it records the named elements again. A name that appears in both states becomes one pair that animates from the old picture to the new one.
| Name appears in | What the browser does |
|---|---|
| Old and new state | Animates the old image into the new image. Position and size animate too. |
| Old state only | Fades the old image out. |
| New state only | Fades the new image in. |
The match works even if the element is a different DOM node in the new state. Your update callback can destroy and recreate the element. If the new node has the same name, the browser treats the two as the same visual thing.
The pseudo-element tree for a named element
Each name adds a branch to the tree. The pseudo-element selectors take the name as an argument.
The roles of the three levels are:
::view-transition-group(name)animates position and size. Its animation moves the group from the old box to the new box.::view-transition-image-pair(name)is a wrapper that isolates the two images. It keeps the cross-fade blend from leaking into other branches.::view-transition-old(name)and::view-transition-new(name)are the two images. The old one is a static picture of the old state. The new one is a live view of the new element.
Because the header now has its own branch, you can give it its own timing without touching the rest of the page.
::view-transition-group(site-header) {
animation-duration: 250ms;
animation-timing-function: ease-out;
}A complete example
This page swaps the main content. The header has a name, so it is not part of the root cross-fade. The header's text stays crisp, and only the content area fades.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Named header</title>
<style>
.site-header {
view-transition-name: site-header;
background: #123;
color: #fff;
padding: 1rem;
}
::view-transition-group(site-header) {
animation-duration: 250ms;
}
::view-transition-old(root),
::view-transition-new(root) {
animation-duration: 500ms;
}
</style>
</head>
<body>
<header class="site-header">My site</header>
<main id="content"><p>First page</p></main>
<button id="swap">Swap content</button>
<script>
let showFirst = true;
document.querySelector('#swap').addEventListener('click', () => {
const update = () => {
showFirst = !showFirst;
document.querySelector('#content').innerHTML =
showFirst ? '<p>First page</p>' : '<p>Second page, with different text</p>';
};
if (!document.startViewTransition) {
update();
return;
}
document.startViewTransition(update);
});
</script>
</body>
</html>The header appears in both states with the same box, so its group has nothing to move. The old and new images are identical, and the header looks still during the transition.
Side effects of naming an element
Giving an element a name changes how it is rendered, even outside a transition. Check these when a page looks different after you add a name.
- It becomes a stacking context. The element paints as one layer. Its
z-indexno longer interleaves with elements outside it in the way it did before. - It becomes a containing block for absolutely and fixed positioned descendants. A
position: fixedchild can stop being positioned relative to the viewport. - It is removed from its parent's picture. The
rootsnapshot has a hole where the element was. The element is drawn on top in its own group. - Its capture is its own box. Overflow clipping by ancestors is not applied to the group's image the way it was in the page. Content that an ancestor clipped can show during the transition.
- Paint order is set by the tree, not the DOM. Named groups are layered above
root, in DOM order of the named elements. A named element that sat under another element on the page can end up above it during the animation.
Names exist only while capturing
The name matters at two moments: when the old state is captured, and when the new state is captured. At other times the property has no visible effect, apart from the stacking context side effects above. This is why you can set the name in a stylesheet for always-present elements, such as headers and navigation. For elements that should only pair in one particular transition, set the name from JavaScript just before capture and remove it afterward. The next chapter builds on that technique.
Common errors
- Duplicate names. Two rendered elements with the same name in one state make the browser skip the transition. The update callback still runs, but nothing animates. The
readypromise rejects, and DevTools reports a duplicate name in the console. - Elements that are not rendered. An element with
display: nonehas no box, so it cannot be captured. The same is true of an element whose name only exists in a stylesheet rule that does not match. - Setting the name too late. If you set the name inside the update callback, the old state has no name. You get a fade-in only. Set the name before calling
startViewTransition()for the old state, and set it again in the callback for the new state. - Using a quoted string.
view-transition-name: "hero"is invalid. Writehero. - Expecting a name to be selectable in the page.
site-headeris not a class. You can only use it inside the::view-transition-*selectors.
Exercise
Your header has view-transition-name: site-header and a position: fixed tooltip inside it. After you add the name, the tooltip jumps to a different position. Why does this happen, and how can you fix it?
Show answer
A named element becomes a containing block for fixed and absolute descendants, so the tooltip is now positioned relative to the header instead of the viewport. Fix it by moving the tooltip outside the named element in the DOM, or by positioning it relative to the header on purpose.
Exercise
A page transition shows the new content fading in, but a card with the name hero also appears only as a fade-in, with no movement. The old card was named hero in a stylesheet rule that only matches after a class is added inside the update callback. What is wrong, and what should change?
Show answer
The old state was captured before the callback ran, so the old card had no name. With a name only in the new state, the browser fades the new image in and does not pair it. Add the class (or set the style) on the old card before calling startViewTransition(), and make sure the new element also has the name once the callback finishes.