Data-Driven CSS Layouts with Typed attr()
Data-driven layouts without JavaScript charts
Small dashboards often pull in a charting library just to draw a few bars. Typed attr() lets CSS read numbers from data-* attributes and size blocks directly so you can skip the JavaScript chart layer.
The old hack vs the modern fix
View demo.bar {
block-size: 1.5rem;
inline-size: calc(1% * var(--pct));
background: var(--accent);
}
/* --pct set in JS per element */
bars.forEach(bar => {
bar.style.setProperty('--pct', parseFloat(bar.dataset.pct));
});.bar {
block-size: 1.5rem;
background: var(--accent);
inline-size: attr(data-pct type(<percentage>));
}/* No Tailwind utility yet. Use arbitrary values or inline style bindings for experimental typed attr() demos. */Attr browser support
Note: The attr() function can be used with any CSS property, but support for using attr() with typed values on properties other than content is experimental. The pattern on this page should be treated as progressive enhancement.
Live demo
Why the modern way wins
Less glue JavaScript
Instead of copying numbers from data-* attributes into custom properties, CSS reads them directly. You keep the data in one place and let layout respond to it.
Inline dashboards
Typed attr() works well for tiny in-context charts: value bars in cards, capacity meters, or progress indicators. No charting library, no canvas, no SVG boilerplate.
Progressive enhancement
Typed attr() works in Chromium 133+ and Firefox 155+, not in Safari yet. You can gate the behavior behind @supports and keep a custom property based fallback for other browsers.
- Key Change
- Data in attributes
- CSS reads numbers directly
- Old Approach
- JS sets custom properties
- DOM writes for every bar
- Modern Approach
- attr() typed values
- Data → layout with no JS
How it works
The classic bar chart pattern stores a number in a data-* attribute and then copies that value into a --pct custom property with JavaScript. Layout runs off the custom property, not the data itself, so you end up with two sources of truth.
Typed attr() extends the long-standing attr() function so it can return non-string values. In this pattern a bar reads attr(data-pct type(
Note: attr() is widely supported when used with content, but using attr() on other properties with typed values is still experimental. Support for this pattern is Chromium 133+ and Firefox 155+, not Safari yet, so treat it as progressive enhancement only and keep a custom property or fixed-size fallback for everyone else.