Sub topic 4
Selecting by Attribute
This chapter assumes you know basic HTML attributes, can write a CSS rule with a selector and declarations, and have met type, class, and ID selectors. An attribute selector matches elements by the presence or value of an HTML attribute. It is written in square brackets.
The basic forms
The simplest form matches any element that has the attribute, whatever its value. The next form matches an exact value.
a[target] { text-decoration: underline; }
a[target="_blank"] { text-decoration: underline; }The first rule matches every a element with a target attribute. The second matches only those whose target is exactly _blank. Quotes around the value are optional for simple words, but required when the value contains spaces or special characters. Use them always and you avoid surprises.
Partial-match operators
Other operators match part of a value.
| Selector | Matches when the attribute value... | Column |
|---|---|---|
| `[attr=val]` | is exactly `val` | |
| `[attr~=val]` | is a space-separated list that contains the word `val` | |
| `[attr\ | =val]` | is `val`, or starts with `val-` |
| `[attr^=val]` | starts with `val` | |
| `[attr$=val]` | ends with `val` | |
| `[attr*=val]` | contains `val` anywhere |
Here they are in use:
a[href^="https://"] { color: green; }
a[href$=".pdf"] { color: crimson; }
[class~="note"] { border-left: 3px solid #999; }
[lang|="en"] { quotes: "“" "”"; }
img[src*="avatar"] { border-radius: 50%; }The ~= operator compares whole words. [class~="note"] matches class="box note" but not class="notebook". The *= operator has no such limit, so [class*="note"] would match notebook.
Case sensitivity
Value matching is case-sensitive for most attributes in HTML. Add the i flag before the closing bracket to ignore case.
a[href$=".pdf" i] { color: crimson; }This rule also matches REPORT.PDF. Some HTML attributes, such as type on inputs, are always compared case-insensitively. The flag matters for attributes like href and data-*.
Specificity
An attribute selector adds one to the middle column of the specificity triple, the same as a class. The attribute's operator and value make no difference.
.note { color: green; } /* (0,1,0) */
[class~="note"] { color: blue; } /* (0,1,0) */
p[class~="note"] { color: purple; } /* (0,1,1) */The first two rules tie at (0,1,0). Source order decides between them, so the second one wins when both match. The third has an extra type selector, so it beats both.
Common errors
- Forgetting quotes on values with special characters.
a[href=https://example.com]is invalid because:and/are not allowed in an unquoted value. The browser drops the whole rule. - Using
*=too loosely.[class*="col"]matchescolumn,collapse, andcolor-scheme. Use~=when you mean a whole class name. - Expecting
^=to match an empty string.[href^=""]matches nothing. The same goes for$=and*=. - Assuming attribute selectors are stronger than classes. They tie. A later class rule overrides an earlier attribute rule.
Exercise
A page has the rules .link { color: blue; } followed by a[href^="https"] { color: green; }. An element <a class="link" href="https://example.com"> is on the page. What color is it, and why?
Show answer
Green. Both selectors have specificity (0,1,0) or higher: .link is (0,1,0), and a[href^="https"] is (0,1,1) because it adds a type selector. The second selector has the higher specificity, so it wins. Even without the a, the tie would be settled by source order, and the later rule would still win.