Back to all features
attr()
The attr() CSS function, in the context of any property, returns the value of an attribute of an HTML element, with the option to return it as a specific type or unit. You can set a default value for missing or invalid attributes.
Community use cases (5)
Community signal summary
Developers are requesting native support for
attr()across arbitrary CSS properties to streamline dynamic styling, enable Content Management System (CMS) configurations [1], and derive layout properties likeobject-positiondirectly from HTML data attributes [4]. Key use cases include binding dynamicanchor-nameorposition-anchoridentifiers across reusable component instances in CSS Anchor Positioning [5].In the meantime, developers must rely on inline
styleattributes [4][5], runtime JavaScript polyfills [4], or generating unique per-element<style>blocks with nonces [4]. These workarounds introduce significant maintenance overhead, bloat markup, and trigger security violations under strict Content Security Policies (CSP) [4][5].Generated by AI from community comments and may contain inaccuracies. Read the comments below for full developer context.
This summary covers all 5 comments, is up to date, and was last updated on September 5, 2026.
I want a simple way to customise a component from within a CMS (content management system).
Injecting styles from a CMS is not simple.
Needing to rangle with template logic inside a HTML attribute (e.g. class, style) is not simple.
Needing utility classes is wasteful and removing unused classes is complex and brittle.
I should be able make an attribute, and put a value provided by the CMS into the attribute, and have CSS that works with every value of that attribute instead of needing pre-made styles or complex template conditions.
@spartanatreyu can you give an example?
I work with CMS all the time, how would this feature work better than inputing that information into a custom property (
--foo) and using that value?PS: i too recall wanting this feature, but can't find my own use example in the hundreds of documents created ever since, not to mention since it didn't work i probably reworked it to be done in some other fashion.
CMS use case I’d have liked to support with
attr(): Supportobject-fitin image tags.What I want to do with this feature
Style images based on their focal point for better UX when images resize to fit different viewport widths. The CMS that calculates focal points outputs data- attributes, then site themes can decide if they want to write the needed styles in their stylesheets. CSS could look like this, defined once for all images site-wide:
What I'm having to do in the meantime
You need a JS polyfill or equivalent bespoke implementation, or write inline style attributes (which are incompatible with a strict CSP). Or you need to write inline style tags like this for every
<img>tag:This is very inelegant / verbose to write, and the CSP consideration adds a layer of complexity, that simply wouldn’t be there with
attr()for arbitrary properties.What I want to do with this feature
We're building a set of UI components that users can assemble into web pages. We want to dynamically assign an anchor name via prop, so that any element can be anchored to another.
What I'm having to do in the meantime
The only way to do that now is by using the
styleattribute, like:but using the
styleattribute is not compatible with CSP, so we're stuck at the moment with hard-coded values that are very limiting as you cannot have multiple instances of the same component on the page.