Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Transforms, Transitions and Animations

The view-transition-class property

Reference for the CSS view-transition-class property: give view-transitioning elements shared class names so one rule can style many transition groups.

The view-transition-class property gives an element one or more class names for a view transition. Every element that takes part in a transition needs its own unique view-transition-name, so you cannot target "all the cards" with one name. Classes solve that: give each card a different name but the same class, and style them all with a single rule.

You use the class inside the transition pseudo-elements, in the form ::view-transition-group(*.card). The property only has an effect on elements that also have a view-transition-name (other than none).

CSS
.card {
  view-transition-name: var(--card-id); /* unique per element */
  view-transition-class: card;          /* shared by all cards */
}

::view-transition-group(*.card) {
  animation-duration: 0.6s;
}

Try it

The script below reorders three tiles inside a view transition. Each tile has its own name, but all share the class tile, so one ::view-transition-group(*.tile) rule slows every move down. Click the button; in a browser without view transitions the tiles simply jump.

One rule for many transition groups

Syntax

CSS
view-transition-class: none | <custom-ident>+
  • Initial value: none
  • Applies to: all elements
  • Inherited: no
  • Animatable: no
  • Computed value: as specified

Values

Value Description
none The element has no transition classes.
<custom-ident>+ One or more class names, separated by spaces. Each is an identifier, written without a dot.

In the pseudo-element selectors, write the class with a dot, optionally after a name or *: ::view-transition-group(*.card) matches every group whose element has the class card, and ::view-transition-group(hero.featured) matches only the group named hero that also has the class featured.

Usage notes

  • Needs a name. Without a view-transition-name the element is not captured separately, and its classes are never used.
  • Applies to all four pseudo-elements. Use the same class selector in ::view-transition-group(), ::view-transition-image-pair(), ::view-transition-old() and ::view-transition-new().
  • Classes come from the new state when it exists. For an element that exists in both the old and the new page, the classes captured for the new state are used. Keep the classes identical on both sides to avoid surprises.
  • Not the same as the class attribute. The values live only in CSS and are not matched by .card selectors in the page.
  • Respect reduced motion. Long durations are easy to add with this property; wrap them in @media (prefers-reduced-motion: no-preference).

Browser support

This is a recent addition to view transitions, so check a compatibility table before relying on it; in browsers without it, the elements still transition, just without the class-based styling.