Back to notes

Typed CSS attr(): HTML attributes that style anything

23 November 2026·
cssattrhtmldata-attributes
Live demoOpen full screen

width: attr(data-size px) is now real CSS. Since Firefox 155 joined Chrome, attr() can read an HTML attribute, parse it as a number, a length, a colour or an angle, and use it in any property, not just in content. Writing an attribute is enough to style the element, with no JavaScript in between.

For years, attr() was the function you met once in a tooltip tutorial: content: attr(data-label) and nothing else, because it only worked in content and always returned a string. If you wanted a bar chart whose bar heights came from the HTML, you wrote inline style="--value: 64" or a script that copied each attribute into a custom property. The CSS Values and Units Level 5 draft removes that wall, and both Chromium and Gecko now ship it.

What changed

The new syntax, as MDN documents it, is attr(<attr-name> <attr-type>?, <fallback-value>?). The middle part is what matters. It can be:

  • type(<syntax>): parse the attribute as a CSS type, such as type(<number>), type(<color>), type(<length>) or type(<custom-ident>).
  • A unit such as px, %, deg, s or ms: the attribute holds a bare number, and CSS attaches the unit.
  • raw-string, or nothing at all: the old behaviour, a string.

The fallback is used when the attribute is missing or doesn't parse as the type you asked for.

The pattern from the demo

Every bar in the demo above is a <button> with three attributes and no inline styles:

<button class="bar" data-value="64" data-hue="200" data-delay="90"></button>

And this is the CSS that turns those attributes into height, colour and timing:

.bar {
  --v:     attr(data-value type(<number>), 0);
  --hue:   attr(data-hue type(<number>), 30);
  --delay: attr(data-delay ms, 0ms);

  height: calc(var(--v) * 1%);
  background: linear-gradient(oklch(0.86 0.15 var(--hue)), oklch(0.58 0.2 var(--hue)));
  transition-delay: var(--delay);
}

/* The old attr() still works for the label */
.bar::after { content: attr(data-value) / ""; }

The JavaScript in the demo only does one thing: it changes attributes. "Wave" writes new data-values and a staggered data-delay (0, 45, 90… ms) on each bar; the sliders and the arrow keys edit the selected bar's attributes. Height, colour and the cascade of delays are computed by CSS from those attributes. When an attribute changes, the browser restyles the element and the normal transition kicks in.

Two details are worth pointing out:

  • Each bar reads its own attributes. attr() resolves against the element the declaration applies to, so the same rule gives twelve different results. Before, you'd need twelve inline styles or twelve :nth-child() rules.
  • Storing the result in a custom property is a convenience, not a requirement. height: attr(data-value %, 0%) would also work on its own. I used custom properties so the colour can be reused in two gradients and the shadow, and so the fallback (below) has one place to write to.

Chrome's announcement shows another nice use: view-transition-name: attr(id type(<custom-ident>), none) gives every card a view-transition name taken from its id.

Browser support and gotchas

Chrome shipped the new attr() in version 133. Firefox's release notes for version 155 (1 September 2026) say it "can now be used in any CSS property" with types, units, fallbacks and namespaces, and also inside container style queries. Safari Technology Preview 249 (29 July 2026) added it, and advanced attr() is one of the Interop 2026 focus areas, but the stable Safari compat data still lists it as unsupported. In my tests, the WebKit 26.5 build that ships with Playwright 1.62 already runs the demo natively.

So for now it's progressive enhancement. The demo checks support with CSS.supports('width: attr(x type(<number>))') and, without it, follows the pattern from Chrome's Modern Web Guidance: a MutationObserver watches the data-* attributes and copies them into the same custom properties with style.setProperty(). The result looks the same, and the badge at the top tells you which path you're on. I tested both paths: native in Chromium 151 and WebKit 26.5, and the fallback in Firefox 153, which predates support.

The things that tripped me up while testing:

  • A unit means "the attribute is a bare number". attr(data-w px, 7px) with data-w="50" gives 50px, but with data-w="50px" it falls back to 7px. If the attribute already includes the unit, use type(<length>) instead, which in turn rejects a bare 50.
  • No type, no number. width: attr(data-w) returns a string, so the declaration ends up invalid and the element falls back to its default width. That is the most common "it doesn't work" moment.
  • No URLs, on purpose. MDN is explicit: values from attr() are "attr()-tainted", and using them inside url() or image-set() makes the declaration invalid at computed-value time. background-image: url(attr(data-icon)) gave none in both engines I tried. This blocks attributes from being turned into requests.
  • Don't use it for meaningful text. content: attr(...) is fine for a decorative label, but generated content shouldn't be the only place a value lives. The demo also keeps an aria-label on each bar with its value.

What to keep an eye on

  • Stable Safari. It's in Technology Preview and in Interop 2026; once it lands in a release, typed attr() stops needing a fallback in current browsers.
  • Design systems built on attributes. A data-columns="3" or data-tone="warning" that styles itself without utility classes or inline styles is suddenly very cheap to build.
  • Attributes in container style queries. Firefox 155 also accepts attr() inside container style queries. It's the next piece to watch as the other engines catch up.

Sources