CSS: attribute selector ([x])
Match Attribute Name
tag[attr]-
Match element of name tag that has attribute attr.
tag here is compound selector, or none, which means the universal selector (* any tag).
π’ TIP: the tag name or attribute names need not be valid HTML names. Useful for XML such as SVG and HTML Custom Data Attribute.
a[data-accessed] { border: solid 1px grey; } matches
<a href="https://example.com/7161" data-accessed="2026-05-09" target="_blank" rel="noreferrer">https://example.com/7161</a>
Match Attribute Name and Value
tag[attr="val"]-
Match tag, has attribute attr, value val.
button[type="button"] { margin: 1ex; } matches
<button id="x5685" type="button">Random</button>
Match Parts of Attribute Value
Match Beginning of Attribute Value
tag[attr^="val"]-
Match tag, has attribute attr, value begins with
val./* color span tag with class name starting with x- */ span[class^="x-"] { color: red; }
Match End of Attribute Value
tag[attr$="val"]-
Match tag, has attribute attr, value ends with
val.
Match Substring of Attribute Value
tag[attr*="val"]-
Match tag, has attribute attr, value contains
val./* Color all links to Wikipedia red. */ a[href*="wikipedia.org/"] { color: red; }
Match Attribue Value that Has Space or Hyphen
tag[attr~="val"]-
Match tag, has attribute attr, value is a list of space-separated values, one of which is exactly equal to val.
Usually used for HTML Class Attribute.
tag[attr|="val"]-
Match tag, has attribute attr, value is val, or begin with val and is immediately followed by a hyphen.
Often used for lang attributes such as
lang="zh-Hans"for simplified chinese andlang="zh-Hant"for traditional Chinese./* any p tag, have attribute zz, and value is aa or start with aa- */ p[zz|="aa"] { color: red; } <p zz="aa">match</p> <p zz="aa-">match</p> <p zz="aa-3">match</p> <p zz="aa-4">match</p>
Case-Insensitive Match on Attribute Value
in any of the attribute selector, you can add a i before the right square bracket, to indicate case-insensitive match for the value part.
i can also be capital I
e.g.
e[a="v" i]e[a="v" I]
/* make mp3 audio links with a music symbol */ a[href$=".mp3" i]:before { content: "π΅Β "; }
<a href="xyz.MP3">xyz.mp3</a>
Match Multiple Attributes
The attribute selector can be combined. For example:
[aaa="1"][bbb="2"] { color: red; }
matches
<p aaa="1" bbb="2">something</p>
Match Attribute that Does Not Exist
/* match a pre tag that does not have class attribute */ pre:not([class]) { color: red; }
CSS. Selectors
Selector types
- CSS: types of selectors
- CSS: simple selectors
- CSS: compound selectors
- CSS: complex selector (combinator)
Simple selectors
- CSS: type selector (tag name)
- CSS: universal selector (* any tag)
- CSS: class selector (.x)
- CSS: ID selector (#)
- CSS: attribute selector ([x])
Combinators
- CSS: descendant selector (space)
- CSS: child selector (>)
- CSS: adjacent sibling selector (+)
- CSS: subsequent sibling selector (~)
Selector list
Special selector
- CSS: :root selector
- CSS: no child selector
- CSS: first child, sibling rank selector
- CSS: nth-child selector
- CSS: pseudo-class selector (:)
- CSS: pseudo-element selector (::)
- CSS: negation selector (:not)
- CSS: βis any ofβ selector (:is, :where)
- CSS: :has descendant selector