Evil Closet Monkey Evil Closet Monkey - 3 months ago 6
CSS Question

Determining if an attribute is absent via CSS Attribute Selector?

Can the CSS Attribute Selector, and/or combination of selectors, be used to determine if an attribute is absent?

An attribute selector can be used to test if the attribute is blank:

[aria-label='']


... but the attribute still must be present in order for that to pass. If the HTML tag does not have the attribute at all, the above selector does not pass.

Can a selector be written in a way that it would pass if the attribute is not there?

Answer

Something like this?

div:not([aria-label]) {
  color: red;  
}
<div aria-label="test">
  test
</div>

<div>
  test2
</div>

Comments