Day 125 - How attr() unlocks a new way of dynamic templating

Day 125 - How attr() unlocks a new way of dynamic templating

🎉 An Awesome CSS Link a Day – Day 125 🎉

How attr() unlocks a new way of dynamic templating

#AnAwesomeCSSLinkADay #YouDontMessWithCSS #CSS

Hello there 👋 Ever dreamed of building an entire guitar chord system using only HTML and CSS? Or better, ever dreamed of building something so dynamic that can be parametrized by using just HTML attributes?

Thanks to the new powers of the attr() function, it’s now possible to use type aware attribute values directly inside CSS properties, bringing semantic meaning to visual representations!

📐 Theory: The New Power of attr() in CSS

With modern browser support evolving, attr() is no longer limited to content. Today, it can be used across many CSS properties, with type support like attr(attr-name-1 type<angle>), attr(data-level type<integer>), and even colors! This evolution makes attr() a real game-changer for styling dynamic UIs without JavaScript.

  1. attr() can now be used for properties like transform, opacity, width, color, and more.
  2. Attribute values can be typed (<length>, <angle>, <number>, etc.) to ensure consistent rendering.
  3. Developers can encode meaningful data directly into HTML, keeping styling semantic and modular.
  4. Paired with CSS Grid and pseudo-elements, this enables rich visual layouts, like guitar chord diagrams, in pure CSS!

🚀 Today’s Link

Explore this incredible example by Mads Stoumann, where every guitar chord is rendered in CSS using attr() and smart layout techniques. A must-see for music lovers and CSS nerds alike!

💫 Giveaways

  • Real-world use of typed attr() in production CSS.
  • A complete guitar chord renderer built without JS.
  • Inspiration for data-driven visuals using only HTML + CSS.
  • Semantic markup meets expressive CSS design.

❓ Why This Rocks

  • Eliminates the need for JS in many data-driven visual layouts.
  • Helps build fully accessible, styleable components based on attributes.
  • Modular and declarative, separating content, data, and presentation.
  • Boosts creativity in generative design using web standards.

🗣️ Join the Conversation

  • Have you used attr() beyond content before?
  • What are some use cases where attr() could simplify your CSS?
  • Could you build data visualizations with CSS alone?
  • Do you think attr() will reduce the reliance on JavaScript?

Let’s celebrate the return of attr() as a modern CSS superpower! 🎸

To view or add a comment, sign in

More articles by Emiliano Pisu

Explore topics