--- title: Automatically Assigned View Transition Names description: Explore current methods for automatically generating view transition names. head: - tag: meta attrs: property: "og:image" content: "/auto-og.png" --- import Gallery from "@/components/Gallery.astro" Often, you do not really care about the specific value you assign to `view-transition-name` because you know how to use `view-transition-class` to easily assign styles to a whole… class of pseudo-elements. But you still have to come up with different view transition names for all elements of that class because the view transition names must be unique. For a set of images like the following, you would need to define 36 view transition names. ## Why Care About Dynamic Names? If you're writing HTML by hand, adding view transition names takes some effort, but not much compared to everything else. If you're generating HTML from data, you can also generate view transition names with minimal extra work. So beyond tech demos, what's the real-world use case for automatically generating view transition names? There are some scenarios where automatically generating view transition names comes in handy: * Separation of concerns: You could weave the view transition names assignments into your code, but you prefer to to keep it separate for better clarity or customizability. * Lack of control: You do not have full control over the code, e.g. because your HTML is generated from Markdown or comes from a CMS. * Structural mismatch: You want to target elements across different components globally. Typically, you achieve this separation by defining view transition names in a global stylesheet rather than as inline style attributes or local CSS in components. With CSS selectors, you have a global view on your page and can select individual HTML elements as needed. However, assigning static names to the `view-transition-name` property is limited to situations where you already know your elements or at least an upper bound for their number. CSS processors can't do much to help with that either. In reality, you don't want to select each element individually. Instead, you want to assign unique names to a group of elements using a single selector. ## How to Assign Names Automatically? Here are your options right now: * [`view-transition-name: match-element`](/basics/pseudos/#auto-generated-names-match-element), which only addresses same-document view transitions. * [`view-transition-name: auto`](/basics/pseudos/#auto-generated-names-auto), which is only supported in Safari. * [Copy an existing unique attribute](/tips/auto-names/#using-attr-for-view-transition-names) like `id` using `attr(id type())`, which currently is only works in Chromium browsers. * In JavaScript, do something like \ `querySelectorAll("selector").forEach(e => e.style.viewTransitionName = ...)` * Use a script like the [`declarative-names`](/tools/utensil-drawer/#declarative-names) script \ `