CSS selectors cheat sheet
Every example below was run with document.querySelectorAll() against the small page shown under "Sample markup", and the result column is the number of elements it matched. Pseudo-elements such as ::before style parts of an element and cannot be selected this way, so they are not listed. New to selectors? Read the selectors lesson and cascade and specificity.
Sample markup used by the examples
<main id="app">
<h1 class="title">Menu</h1>
<p class="intro">Pick a page.</p>
<ul class="menu">
<li class="item active"><a href="/home">Home</a></li>
<li class="item"><a href="https://example.com" target="_blank">Partner</a></li>
<li class="item"><a href="/docs.pdf">Docs</a></li>
</ul>
<form><input type="text" required><input type="checkbox" checked><button disabled>Send</button></form>
<p>Last paragraph.</p>
</main>Basic selectorsCombinatorsAttribute selectorsStructural pseudo-classesLogical pseudo-classesForm states
Basic selectors
| Syntax | What it does | Example → result |
|---|---|---|
element | Every element of that type | p→ 2 matches |
.class | Elements with that class | .item→ 3 matches |
#id | The element with that id | #app→ 1 match |
a.b | Both conditions on one element | li.active→ 1 match |
.a.b | Elements with both classes | .item.active→ 1 match |
a, b | Either selector (a group) | h1, .intro→ 2 matches |
:root | The document root (html) | :root→ 1 match |
Combinators
| Syntax | What it does | Example → result |
|---|---|---|
A B | B anywhere inside A | ul a→ 3 matches |
A > B | B that is a direct child of A | main > p→ 2 matches |
A + B | B immediately after A | h1 + p→ 1 match |
A ~ B | Every B after A (same parent) | h1 ~ p→ 2 matches |
Attribute selectors
| Syntax | What it does | Example → result |
|---|---|---|
[attr] | Has the attribute | [target]→ 1 match |
[attr="v"] | Attribute equals v | input[type="text"]→ 1 match |
[attr^="v"] | Value starts with v | a[href^="https"]→ 1 match |
[attr$="v"] | Value ends with v | a[href$=".pdf"]→ 1 match |
[attr*="v"] | Value contains v | a[href*="o"]→ 3 matches |
[attr~="v"] | Space-separated list contains the word v | [class~="item"]→ 3 matches |
Structural pseudo-classes
| Syntax | What it does | Example → result |
|---|---|---|
:first-child | First child of its parent | li:first-child→ 1 match |
:last-child | Last child of its parent | li:last-child→ 1 match |
:nth-child(n) | The nth child (1-based) | li:nth-child(2)→ 1 match |
:nth-child(odd) | Children 1, 3, 5, ... | li:nth-child(odd)→ 2 matches |
:first-of-type | First sibling of its element type | p:first-of-type→ 1 match |
:last-of-type | Last sibling of its element type | p:last-of-type→ 1 match |
Logical pseudo-classes
| Syntax | What it does | Example → result |
|---|---|---|
:not(x) | Does not match x | li:not(.active)→ 2 matches |
:is(a, b) | Matches any in the list | :is(h1, p)→ 3 matches |
:where(a, b) | Like :is() but adds zero specificity | :where(.menu) a→ 3 matches |
:has(x) | Contains something matching x | li:has(a[target])→ 1 match |
Form states
| Syntax | What it does | Example → result |
|---|---|---|
:checked | Checked checkbox or radio | input:checked→ 1 match |
:disabled | Disabled form control | button:disabled→ 1 match |
:required | Field with the required attribute | :required→ 1 match |