Sub topic 14
Sharing styles across names with view-transition-class
This chapter assumes you can already name elements with view-transition-name and style the pseudo-element tree from earlier chapters.
The problem with per-name rules
Every element in a shared-element transition needs a unique view-transition-name. Styling by name therefore means one rule per element:
::view-transition-group(card-1) { animation-duration: 350ms; }
::view-transition-group(card-2) { animation-duration: 350ms; }
::view-transition-group(card-3) { animation-duration: 350ms; }This does not scale to a list whose items are generated at runtime. The view-transition-class property fixes it. Names must be unique, but classes can be shared.
How the property works
view-transition-class takes one or more space-separated identifiers. It is set on the same element as view-transition-name, and it has no effect without a name. The browser copies the classes onto that element's pseudo-elements. You select them by appending the class to the name part of the pseudo-element selector. The * matches any name.
| Selector | Matches |
|---|---|
| `::view-transition-group(*.card)` | The group of every named element with class `card` |
| `::view-transition-old(*.card)` | The old snapshot of those elements |
| `::view-transition-new(*.card)` | The new snapshot of those elements |
| `::view-transition-group(card-2)` | The one element named `card-2` |
Example: a reorderable list
Each item gets a unique name and a shared class. This code sets both from JavaScript, then reverses the list inside a view transition:
document.querySelectorAll('#list li').forEach((item) => {
item.style.viewTransitionName = `card-${item.dataset.id}`;
item.style.viewTransitionClass = 'card';
});
function reverseList() {
const list = document.querySelector('#list');
const update = () => list.append(...[...list.children].reverse());
if (!document.startViewTransition) {
update();
return;
}
document.startViewTransition(update);
}One rule now styles every card, however many exist:
::view-transition-group(*.card) {
animation-duration: 350ms;
animation-timing-function: ease-in-out;
}The group pseudo-element carries the position and size animation, so each card slides to its new place using these values.
Combining classes
An element can carry several classes, such as view-transition-class: card featured;. A rule for *.featured can then override a rule for *.card, using normal cascade order and specificity. A name and a class can also be combined, as in ::view-transition-group(card-2.featured), but that rarely adds anything beyond the class alone.
Common errors
- Setting a class without a name. The element is not captured, so no pseudo-elements exist to match.
- Treating the class as a replacement for unique names. Duplicate names still break the transition.
- Forgetting that browser support is newer than for
view-transition-name. A browser that does not recognize the selector ignores the whole rule, so the default animation applies.
Exercise
Your page renders a product grid with hundreds of items. Each item has view-transition-name: product-<id>. You want every product's group animation to last 500ms, but only the item with class sale should use ease-out. Which CSS and property would you use?
Show answer
Set view-transition-class: product on every item, and view-transition-class: product sale on sale items. Then write ::view-transition-group(*.product) { animation-duration: 500ms; } and ::view-transition-group(*.sale) { animation-timing-function: ease-out; }. Names stay unique, and two rules cover the whole grid.