View Transitions: Use the new attr() or match-element for the view-transition-name?

At CSS Day, “YouTube guy” Kevin Powell showed a lot of demos that relied on the advanced attr() function. In one of the examples he used attr() to set view-transition-name values — a technique I covered in my article on advanced attr() function.

But then during the Q&A, Cyd Stumpel wondered if he couldn’t just use match-element there.

The short answer to that question is yes. The longer answer is … It Depends™

~

view-transition-name: attr(…);

To recap, the technique Kevin showed is this:

<style>
  .card {
    view-transition-name: attr(data-id type(<custom-ident>), none);
    view-transition-class: card;
  }
</style>

It reads the data-id attribute’s value and casts it to a <custom-ident> for use as the view-transition-name value.

You can see Kevin apply it live here. When applied, all cards will get a view-transition-name and get snapshotted — and therefore also move — individually as part of the View Transition.

Kevin live coding it at CSS Day. Note he starts off typing data() which is later corrected to attr() with some help from Jake and myself.

~

view-transition-name: match-element;

The thing Cyd mentioned is match-element, which automatically gives an element a view-transition-name value based on the element’s identity. In Kevin’s demo this would totally work, and the demo would work just as with the attr() code.

<style>
  .card {
    view-transition-name: match-element;
    view-transition-class: card;
  }
</style>

So, what’s the fuss about then?

~

match-element works … until it doesn’t

The thing with match-element is that it uses element identity to assign automagic values. This element identity is not to be confused with the id attribute — they are two different things.

Think of this identity as an internal number that the browser assigns to created elements. Every time an element is created, the internal counter increments. So if you were to destroy an element and then recreate it including its id, you’d end up with an element that that has a different identity, even though it has the same id.

The side-effect of match-element using element identity, is that are two scenarios when you should not be using match-element at all:

  1. When you build an MPA (aka: a website), match-element doesn’t work at all because nodes in different documents are never the same; they all have a different identity, so match-element can never match them.
  2. If you need to style a specific element from the whole set, then you need the unique name to use in your ::view-transition-*() selectors, which match-element doesn’t give you.

So, there you have it 🙂

~

TL;DR match-element is limited to Same-Document View Transitions and can’t be used to uniquely target any of the View Transition pseudos.

~

🔥 Like what you see? Want to stay in the loop? Here's how:

I can also be found on 𝕏 Twitter and 🐘 Mastodon but only post there sporadically.

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.