CSS attr() gets an upgrade

You can now use `attr()` with any CSS property — not just content – and it can parse values into data types other than <string>.


This content originally appeared on Bram.us and was authored by Bramus!

~

A feature shipping in Chrome 133 – which goes stable on Feb 4 – is a more powerful attr(). From Chrome 133 onwards you use attr() with any CSS property — not just content – and it can parse values into data types other than <string>.

~

Here is an example that parses the data-clr atrribute into a <color>.

<style>
  div {
     color: attr(data-clr type(<color>), red);
  }
</style>
<div data-clr="blue">My text color is blue</div>
<div>My text color is red (the fallback value)</div>

~

Here is a more advanced example that parses an attribute into a <custom-ident>. Handy for View Transitions when you’ve already given the elements a unique id. in the markup: simply take that id and use that as the view-transition-name.

<style>
  .card[id] {
    view-transition-name: attr(id type(<custom-ident>), none); /* card-1, card-2, card-3, … */
    view-transition-class: card;
  }
</style>
<div class="cards">
  <div class="card" id="card-1"></div>
  <div class="card" id="card-2"></div>
  <div class="card" id="card-3"></div>
  <div class="card" id="card-4"></div>
</div>

Here’s a live demo. Each .card elements get a view-transition-name derived from the id attribute.

~

Supporting the (upcoming) release of this more powerful `attr()` function I published a blog post on the Chrome for Developers blog and also updated MDN:

~


This content originally appeared on Bram.us and was authored by Bramus!


Print Share Comment Cite Upload Translate Updates
APA

Bramus! | Sciencx (2025-01-20T18:53:21+00:00) CSS attr() gets an upgrade. Retrieved from https://www.scien.cx/2025/01/20/css-attr-gets-an-upgrade/

MLA
" » CSS attr() gets an upgrade." Bramus! | Sciencx - Monday January 20, 2025, https://www.scien.cx/2025/01/20/css-attr-gets-an-upgrade/
HARVARD
Bramus! | Sciencx Monday January 20, 2025 » CSS attr() gets an upgrade., viewed ,<https://www.scien.cx/2025/01/20/css-attr-gets-an-upgrade/>
VANCOUVER
Bramus! | Sciencx - » CSS attr() gets an upgrade. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/20/css-attr-gets-an-upgrade/
CHICAGO
" » CSS attr() gets an upgrade." Bramus! | Sciencx - Accessed . https://www.scien.cx/2025/01/20/css-attr-gets-an-upgrade/
IEEE
" » CSS attr() gets an upgrade." Bramus! | Sciencx [Online]. Available: https://www.scien.cx/2025/01/20/css-attr-gets-an-upgrade/. [Accessed: ]
rf:citation
» CSS attr() gets an upgrade | Bramus! | Sciencx | https://www.scien.cx/2025/01/20/css-attr-gets-an-upgrade/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.