Sub topic 14
Sharing styles across names with view-transition-class
This chapter assumes you can call document.startViewTransition(), you know that view-transition-name gives an element its own snapshot, and you can style the ::view-transition-* pseudo-elements. Earlier chapters covered each of these.
The problem: one rule per name
A shared-element transition needs a unique view-transition-name on every element that should animate separately. In a list of 40 cards, that means 40 names such as card-1 and card-2.
Named pseudo-element selectors match one name at a time. To give every card the same motion, you would have to write one rule per name:
::view-transition-group(card-1) { animation-duration: 350ms; }
::view-transition-group(card-2) { animation-duration: 350ms; }
/* ...and so on for every card */This breaks down because the names are often generated at runtime from database IDs. A stylesheet cannot list names it does not know. The wildcard * matches every name, but it also catches the header, the sidebar, and anything else that has a name.
The view-transition-class property solves this. It tags elements with one or more class-like labels. A pseudo-element selector can then target all names that carry a label.
How view-transition-class works
You set view-transition-class on the same element that has a view-transition-name. The value is a space-separated list of identifiers, like the class attribute in HTML.
.card {
view-transition-class: card;
}In the pseudo-element selectors, write the name part first and then the class after a dot. To match any name, use *:
::view-transition-group(*.card) {
animation-duration: 350ms;
animation-timing-function: cubic-bezier(0.2, 0, 0, 1);
}Four details control how the property behaves:
- It does nothing on its own. The element must also have a
view-transition-name, because the class is attached to the named snapshot. - It does not have to be unique. Many elements can share the class
card, while each name stays unique. - It is not the HTML
classattribute. The two lists are independent, soclass="card"does not create a view-transition class. - It can be matched in the same state that produced the snapshot. The old snapshot uses the class from the old state, and the new snapshot uses the class from the new state.
The selector also works with a specific name. ::view-transition-group(card-7.card) matches only the group named card-7 that also has the class card. In practice you almost always use * or a bare name.
Which pseudo-elements accept a class
The class selector works on the four pseudo-elements that take a name argument:
| Selector | What it styles |
|---|---|
| `::view-transition-group(*.card)` | Position and size animation of the whole named element |
| `::view-transition-image-pair(*.card)` | The container holding the old and new images |
| `::view-transition-old(*.card)` | The outgoing snapshot |
| `::view-transition-new(*.card)` | The incoming snapshot |
The ::view-transition root pseudo-element has no name and no class.
Correction: the diagram must be a diagram fence, so here it is in the required form.
A worked example: a reorderable list
Suppose a list of tasks can be sorted. Each task gets its own name so the browser can move it from its old position to its new one. Every task also gets the class task, so one rule controls how all of them move.
function renderTasks(tasks) {
const list = document.querySelector('#tasks');
list.replaceChildren(
...tasks.map((task) => {
const li = document.createElement('li');
li.textContent = task.title;
li.style.viewTransitionName = `task-${task.id}`;
li.style.viewTransitionClass = 'task';
return li;
})
);
}
function sortBy(tasks, key) {
const sorted = [...tasks].sort((a, b) => a[key].localeCompare(b[key]));
if (!document.startViewTransition) {
renderTasks(sorted);
return;
}
document.startViewTransition(() => renderTasks(sorted));
}The names task-1, task-2, and so on are unique. They come from the IDs, so they match across the old and new states. The class task is the same for every item.
The CSS needs only two rules, no matter how many tasks exist:
::view-transition-group(*.task) {
animation-duration: 300ms;
animation-timing-function: ease-in-out;
}
::view-transition-old(*.task),
::view-transition-new(*.task) {
height: 100%;
object-fit: cover;
}Tasks that exist in both states slide to their new position. A task that exists only in the old state fades out, and a task that exists only in the new state fades in, using the default behavior for unpaired names.
Using several classes
An element can carry more than one class. This lets you build layers of styling, in the same way you combine utility classes:
.card {
view-transition-class: card;
}
.card.featured {
view-transition-class: card featured;
}::view-transition-group(*.card) {
animation-duration: 300ms;
}
::view-transition-group(*.featured) {
animation-duration: 500ms;
}Chain classes in a selector to require all of them, as in ::view-transition-group(*.card.featured).
Here the featured card has both labels, so both rules match it. When two rules set the same property, the usual cascade decides which wins. Rules with higher specificity win, and for equal specificity the later rule wins. If two class rules conflict, put the more specific case later in the stylesheet or chain both classes so the intent is explicit. Check the result in DevTools instead of guessing.
Combining with reduced motion
Class selectors fit well with the reduced-motion fallback from an earlier chapter, because the override does not need to know any names:
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*.task),
::view-transition-old(*.task),
::view-transition-new(*.task) {
animation: none !important;
}
}Common mistakes
- Setting only the class. Without
view-transition-name, the element is not captured separately, so the class has nothing to attach to. Symptom: your rule never matches. - Using the class in a bare selector.
::view-transition-group(.task)is invalid. You must write*.taskorname.task. - Setting the class after the snapshot. The old state's class must be on the element before the old snapshot is captured. Set it in the same place where you set the name, not inside the update callback only.
- Expecting the class to remove the need for unique names. The class groups styles. Two elements with the same
view-transition-namestill cause the transition to be skipped. - Assuming universal support. The property is newer than
view-transition-name. Check current browser support. In a browser without it, the declaration is ignored and your elements animate with the default timing, so the page still works.
When to prefer the class and when not to
Use view-transition-class when names are generated, when a family of elements should move alike, or when you want the stylesheet to stay the same size as the data grows. Use a plain name selector when exactly one element needs special motion, such as the single hero image from the shared-element chapter. Both can coexist: give the hero a name and a class, then style it by name for its unique behavior and by class for what it shares with other items.
Exercise
A gallery sets li.style.viewTransitionName = 'photo-' + id on every photo. You want all photo groups to animate for 400ms. A teammate writes ::view-transition-group(photo) { animation-duration: 400ms; } and nothing changes. What is wrong, and what two changes fix it?
Show answer
No element is named photo; the names are photo-1, photo-2, and so on, so the selector matches nothing. Fix it by (1) also setting li.style.viewTransitionClass = 'photo' where the name is set, and (2) changing the rule to ::view-transition-group(*.photo) { animation-duration: 400ms; }.
Exercise
Why does setting view-transition-class: card on an element without a view-transition-name have no effect?
Show answer
The class is attached to the named snapshot. An element without a name is not captured as its own pseudo-element group (it is part of the root snapshot), so there is no ::view-transition-group for the class selector to match.