The wasted potential of CSS attribute selectors by Elise Hein
May 24, 2024
In our preoccupation with classes, it’s easy to think of them as the default selector. Historically, when people have talked about targeting elements by their attributes, it’s been in the context of neat tricks or one liners. You make a mental note to use
a[href^=https://specific-domain.com]
the next time you want to use pink underlines for each link to one specific domain, and promptly forget about it forever. Or, to give a more practical example, attribute selectors have been recommended as a kind of linter or debugger for invalid HTML (img:not([alt]) { border: 1rem solid red; }
).
Source: The wasted potential of CSS attribute selectors by Elise Hein
More than 20 years ago we settled on the pattern on using classes in HTML for styling pages with CSS. From OOCSS to BEM and elsewhere we developed conventions for their use. But perhaps we’ve overlooked at times better ways of doing thing, like the attribute selector.