Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

view-transition-name — naming an element in a view transition

The view-transition-name property marks an element for independent capture during a view transition. Matching names pair its old and new visual states; the declaration alone does not start a transition.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

view-transition-name: none | <custom-ident> | auto | match-element;

view-transition-name lets a captured element animate independently of the rest of the page. Its initial value is none; it is not inherited. none disables independent capture, but does not remove the element from a captured ancestor's image.

A custom identifier such as card pairs old and new visual states, even when they belong to different DOM nodes. The name must be unique among elements captured simultaneously within the transition; duplicates abort the animation, not the DOM update itself. Customize the group with ::view-transition-group(card).

The Level 2 draft also describes match-element, which generates a name from element identity, and auto, which can use the element's id and otherwise generates a name. These keywords are not custom identifiers. A specification draft does not guarantee browser support.

The example starts a transition with document.startViewTransition() and changes the position of card. Without support, it performs an ordinary update. The prefers-reduced-motion: reduce media query sets the animation duration to 0s only for the card group; the default transition for the root group remains enabled.

Examples

<!doctype html>
<meta charset="utf-8">
<title>view-transition-name</title>
<style>
  #card {
    view-transition-name: card;
    width: 120px;
    padding: 16px;
    background: navy;
    color: white;
  }
  #card.moved { margin-left: 160px; }
  ::view-transition-group(card) { animation-duration: 600ms; }
  @media (prefers-reduced-motion: reduce) {
    ::view-transition-group(card) { animation-duration: 0s; }
  }
</style>
<button id="move" type="button">Move</button>
<div id="card">A</div>
<script>
  const button = document.querySelector('#move');
  const card = document.querySelector('#card');
  button.addEventListener('click', async () => {
    button.disabled = true;
    const update = () => card.classList.toggle('moved');
    try {
      if (document.startViewTransition &&
          CSS.supports('view-transition-name', 'card')) {
        const transition = document.startViewTransition(update);
        await transition.finished;
      } else {
        update();
      }
    } finally {
      button.disabled = false;
    }
  });
</script>

Result

After the first click: card.classList.contains('moved') === true. After the second completed click: false. With supported view transitions, the card changes position through its named transition group; otherwise it moves immediately.

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources