TetSharing

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

SyntaxWhat it doesExample → result
elementEvery element of that typep
→ 2 matches
.classElements with that class.item
→ 3 matches
#idThe element with that id#app
→ 1 match
a.bBoth conditions on one elementli.active
→ 1 match
.a.bElements with both classes.item.active
→ 1 match
a, bEither selector (a group)h1, .intro
→ 2 matches
:rootThe document root (html):root
→ 1 match

Combinators

SyntaxWhat it doesExample → result
A BB anywhere inside Aul a
→ 3 matches
A > BB that is a direct child of Amain > p
→ 2 matches
A + BB immediately after Ah1 + p
→ 1 match
A ~ BEvery B after A (same parent)h1 ~ p
→ 2 matches

Attribute selectors

SyntaxWhat it doesExample → result
[attr]Has the attribute[target]
→ 1 match
[attr="v"]Attribute equals vinput[type="text"]
→ 1 match
[attr^="v"]Value starts with va[href^="https"]
→ 1 match
[attr$="v"]Value ends with va[href$=".pdf"]
→ 1 match
[attr*="v"]Value contains va[href*="o"]
→ 3 matches
[attr~="v"]Space-separated list contains the word v[class~="item"]
→ 3 matches

Structural pseudo-classes

SyntaxWhat it doesExample → result
:first-childFirst child of its parentli:first-child
→ 1 match
:last-childLast child of its parentli: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-typeFirst sibling of its element typep:first-of-type
→ 1 match
:last-of-typeLast sibling of its element typep:last-of-type
→ 1 match

Logical pseudo-classes

SyntaxWhat it doesExample → result
:not(x)Does not match xli: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 xli:has(a[target])
→ 1 match

Form states

SyntaxWhat it doesExample → result
:checkedChecked checkbox or radioinput:checked
→ 1 match
:disabledDisabled form controlbutton:disabled
→ 1 match
:requiredField with the required attribute:required
→ 1 match

Official docs

Compiled by Ethan Cole (pen name) with AI-assisted drafting; every example in the table is executed by our test suite. Related lessons: selectors, cascade specificity.