You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/basics/pseudos.mdx
+22-9Lines changed: 22 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -263,6 +263,9 @@ It is not important to have much imagination when choosing names. They can be th
263
263
264
264
### Auto-Generated Names: match-element
265
265
266
+
_For same-document view transitions, only_
267
+
268
+
266
269
The opposite problem to view transition names not being unique is that elements need to have _any_`view-transition-name` assigned in order to trigger a group or morph animation. Often, the actual value doesn't matter. What's important is simply having a value defined as it acts as a marker for elements for which the API should generate view transition groups. For large numbers of elements, doing this manually can be cumbersome. You can automate it with JavaScript, as with the headings on this site or in [this demo](/tips/auto-names/).
267
270
268
271
However, it is also possible to do this with only CSS by using the `match-element` value, no JavaScript needed.
@@ -274,7 +277,7 @@ However, it is also possible to do this with only CSS by using the `match-elemen
274
277
```
275
278
276
279
Using `match-element` assigns a generated name that is bound to the DOM element.
277
-
Those names typically look like `-ua-....` with some random suffix.
280
+
Those names typically look like `-ua-....`[with some random suffix](/tips/auto-names/#auto-generated-names).
278
281
Two different elements are always assigned different values.
279
282
280
283
How could you style your pseudo-elements, if they have an unknown random name? The general approach here is to also assign a view transition class.
@@ -284,24 +287,34 @@ How could you style your pseudo-elements, if they have an unknown random name? T
284
287
view-transition-name: match-element;
285
288
view-transition-class: card;
286
289
}
290
+
291
+
::view-transition-group(.card) {
292
+
/* styles for all pseudo-elements of cards */
293
+
}
287
294
```
295
+
Unlike the view transition name, the view transition class name does not need to be unique across elements. Then [use the view transition class name](/basics/styling/#with-classes) in your style rules.
296
+
297
+
In this example, you have multiple `.card` elements but you do not care to give them all unique view transition names. You just want to opt them all in for view transitions. The generated pseudo-elements for the card are all styled the same, independent of the individual card. That is what the view-transition-class accomplishes.
288
298
289
-
The class name does not need to be unique across elements. Then [use the view transition class name](/basics/styling/#with-classes) in your style rules.
299
+
Note that `.card` in line 1 is a CSS class, while `.card` in line 6 is a view transition class. If you find that irritating, you could as well use different identifiers for the two.
290
300
291
-
This works fine for **same-document** view transitions on supporting browsers.
301
+
302
+
303
+
The special identifier `match-element` works fine for **same-document** view transitions on all browsers.
292
304
For **cross-document** view transitions, generated names for the two document should never be the same. Therefore groups created from `match-element` in cross-document view transitions will always have image-pairs with a single old or new image, only.
293
305
294
306
:::caution
295
-
Safari seems to handle `match-element` like `none` in cross-document view transitions, i.e. it does not generate any names when view transitions are enabled via `@view-transition{navigate-auto}`. It does generate names when the transition ist started by calling `startViewTransition()`.
307
+
Safari seems to handle `match-element` like `none` in cross-document view transitions, i.e. it does not generate any names when view transitions are enabled via `@view-transition{navigation: auto}`. It does generate names when the transition ist started by calling `startViewTransition()`.
296
308
:::
297
309
298
310
Until it gets broader, convergent support in browsers, **I would not recommend `match-element`** for cross-document view transitions. There are [alternatives](/tips/auto-names/#how-to-assign-names-automatically) that work cross-browser and also for cross-document navigation.
299
311
300
312
### Auto-Generated Names: auto
301
313
302
-
_`view-transition-name: auto` is a bit of a mess right now. Use with caution._
314
+
_Not available across browsers, yet._
315
+
316
+
_`view-transition-name: auto` is a bit of a mess right now. It is part of the spec, and it was at some point also supported by Chromium browsers, but currently it is only supported in Safari. Do not use it (yet).
303
317
304
-
Browser also defined `auto` as a special value for `view-transition-name`, but its semantics are not consistent across browsers and its specification is work in progress. Do not use it (yet).
305
318
306
319
```css
307
320
selector {
@@ -312,12 +325,12 @@ selector {
312
325
This is similar to `match-element` but it also takes the `id` property of an HTML element into account: If the `id` property is set, `auto` generates the same view transition name for all elements with that `id`, independent of the document that element belongs to.
313
326
Different ids generate different view transition names.
314
327
315
-
So the main use case here is to match elements with the same id in cross-document view transitions. But it also works with `startViewTransition`. Just make sure that your `id` values are unique as it should be even if not enforced by browsers. Otherwise you might get duplicate view transition names and the view transition gets skipped with an error.
328
+
So the main use case here is to match elements with the same id in cross-document view transitions. But it also works with `startViewTransition`. Just make sure that your `id` values are unique as they should be, even if not enforced by browsers. Otherwise you might get duplicate view transition names and the view transition gets skipped with an error.
316
329
317
-
Different browsers and browser versions show divergent behavior. Safari uses the `id` value as view transition name. On some Webkit versions you might also see `-ua-id-<id>`, where `<id>` is the element's `id` value. Chrome uses random values similar to those generated for `match-element`.
330
+
Safari uses `-ua-id-<id>` as the view transition name.
318
331
319
332
:::caution
320
-
As with `match-element`, if `id` is not set, Safari does not seem to generate random names for cross-document view transitions. It does so for same-document view transitions.
333
+
As with `match-element`, if `id` is not set, Safari does not generate random names for cross-document view transitions. It does so if `id` is set and for same-document view transitions.
Copy file name to clipboardExpand all lines: src/content/docs/tips/auto-names.mdx
+15-16Lines changed: 15 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -41,54 +41,53 @@ In reality, you don't want to select each element individually. Instead, you wan
41
41
## How to Assign Names Automatically?
42
42
Here are your options right now:
43
43
44
-
* Useful for same-document view transitions: generate dynamic identifiers with [match-element](/basics/pseudos/#auto-generated-names-match-element)
44
+
*[`view-transition-name: match-element`](/basics/pseudos/#auto-generated-names-match-element), which only addresses same-document view transitions.
45
+
*[`view-transition-name: auto`](/basics/pseudos/#auto-generated-names-auto), which is only supported in Safari.
45
46
46
-
* Cross-document: generate dynamic identifiers with [auto](/basics/pseudos/#auto-generated-names-auto) for elements with `id` properties.
47
-
48
-
*[Copy an existing unique attribute](/basics/pseudos/#auto-generated-names-match-element) like `id` using `attr(id type(<custom-ident>))`
47
+
*[Copy an existing unique attribute](/tips/auto-names/#using-attr-for-view-transition-names) like `id` using `attr(id type(<custom-ident>))`, which currently is only works in Chromium browsers.
`<script src="/declarative-names.js" data-vtbag-decl="selector"/>`, which works cross-browser for same-document and cross-document view transitions.
53
52
54
53
55
-
I would **not recommend the first three** approaches just yet, as of today, they don't work reliably across all browsers. But I'm curious and looking forward to what the future has in store.
54
+
I would **not recommend the first three** approaches just yet. As of today, they don't work reliably across all browsers. But I'm curious and looking forward to what the future has in store.
56
55
57
56
### Auto Generated Names
58
57
58
+
The only reliable mechanism for automatically generated view transition names across browsers is `match-element` (_only for same-document view transitions_)
59
+
60
+
It generates unique view transition names for each element that has this value set. The generated names are stable as long as the same elements are matched. Because different documents have different elements the names will change on cross-document navigation and even on page reload. Therefore you can't use it for shared element animations across documents.
61
+
62
+
The view transition names generated this way depend on the browser and are not meant to be used in pseudo-element selectors like `::view-transition-group(...)`. The spec says, they have to start with `-ua-`.
59
63
60
-
Assigning `match-element` or `auto` as a view transition name dynamically generates unique names.
61
-
Where it is supported, it works well for same-document view transitions.
64
+
A typical pattern is to [combine `view-transition-name: match-element` with the `view-transition-class` property](/basics/pseudos/#auto-generated-names-match-element).
62
65
63
-
The view transition names generated this way depend on the browser. While Safari's `auto` value seems to encourage the use of the id attribute as view transition name for cross-document view transitions, this does not work for the other browsers.
64
66
65
67
For same-document view-transitions, Safari generates rather short names, but it wont generate those for cross-document view transition.
66
68
```cs
67
69
-ua-auto-2235
68
70
-ua-auto-2236
69
71
-ua-auto-2237
70
72
```
71
-
What works in Safari for cross-document view transitions: Use `auto` on elements that have their `id` set. In this case, Safari/Webkit just copies the id as an view transition name, directly or with some prefix.
73
+
What works in Safari for cross-document view transitions: Use `auto` on elements that have their `id` set. In this case, Safari/Webkit just copies the id as an view transition name with some prefix.
72
74
```cs
73
75
myid
74
76
-ua-id-myid
75
77
```
76
78
But that is not really auto generating view transition names, right?
77
79
78
80
79
-
Chrome's dynamically generated names use what looks like a cryptographic hash to distinguish the owner documents of elements and a counter to distinguish the elements within the document. That way it assigns different ids during cross-document transitions.
81
+
Chrome's dynamically generated names use something that looks like a random string or hash to distinguish the owner documents of elements, and an integer to distinguish the elements within the document. That way it also assigns different ids during cross-document transitions.
80
82
81
83
```cs
82
84
-ua-auto-F5E19860A59EF9D0C4AAFAA1378C6862-63044
83
85
-ua-auto-F5E19860A59EF9D0C4AAFAA1378C6862-63051
84
86
-ua-auto-F5E19860A59EF9D0C4AAFAA1378C6862-63058
85
87
```
86
-
Auto-generation in Chrome works well for same-document and cross-document view transitions. For cross-document view transition, when using `auto` on elements that have their `id` set, Chrome also generates such cryptic names, where the hash only depends on the id, omitting the counter.
87
-
```cs
88
-
-ua-auto-A266076C3FD7C0005E7B8A3E59C387EE
89
-
```
88
+
Generation in Chrome works well for same-document and cross-document view transitions.
90
89
91
-
Some Chrome versions conceal the random value by mapping it back to `match-element` on some interfaces. But that might soon change again.
90
+
Browsers typicallyconceal the random values by mapping them back to `match-element`or `auto`on some interfaces.
0 commit comments