← All cheat sheets

CSS

CSS Selectors Cheat Sheet — Every Selector and Combinator

Selectors decide which elements a rule applies to. This reference covers the full set — from the basics to the modern functional pseudo-classes — with a plain-English description of each.

Put it into practice with the matching tool.

Try the CSS filter playground →

Advertisement

Basic selectors

*Every element
pEvery <p> element (type selector)
.cardEvery element with class="card"
#headerThe element with id="header"
.card.featuredElements with both classes
a.btnEvery <a> that also has class="btn"
h1, h2, h3Selector list — applies to all three

Combinators

div pDescendant — any <p> inside a <div>, at any depth
div > pChild — only direct children
h2 + pNext sibling — the <p> immediately after an <h2>
h2 ~ pSubsequent siblings — every <p> after an <h2>

Attribute selectors

[disabled]Has the attribute, whatever its value
[type="email"]Attribute exactly equals the value
[class~="btn"]Space-separated list contains the word
[href^="https"]Value starts with
[href$=".pdf"]Value ends with
[href*="blog"]Value contains the substring
[lang|="en"]Value is "en" or starts with "en-"
[href$=".pdf" i]Add i before ] to match case-insensitively

Pseudo-classes: state

:hoverPointer is over the element
:focusElement has keyboard focus
:focus-visibleFocused and a focus ring should show (keyboard, not mouse)
:focus-withinThe element or any descendant has focus
:activeBeing clicked or pressed
:visitedLink the user has already visited
:targetElement matching the current URL fragment
:checkedChecked checkbox, radio, or selected option
:disabled / :enabledForm control state
:required / :optionalWhether the field is required
:valid / :invalidPasses or fails validation
:placeholder-shownInput is empty and showing its placeholder

Pseudo-classes: position

:first-child / :last-childFirst or last child of its parent
:only-childThe sole child of its parent
:nth-child(3)The third child
:nth-child(odd)Odd-numbered children (also: even)
:nth-child(3n)Every third child
:nth-child(n+4)The fourth child onward
:nth-last-child(2)Second from the end
:first-of-typeFirst sibling of its element type
:nth-of-type(2)Second sibling of that type
:emptyNo children and no text at all
:rootThe <html> element — where custom properties go

Functional pseudo-classes

:not(.active)Elements that do not match the selector
:is(h1, h2, h3) aShorthand for a long selector list
:where(h1, h2) aLike :is() but adds zero specificity
.card:has(img)A card that contains an image — the "parent" selector
label:has(+ input:focus)Style based on a sibling's state

Pseudo-elements

::beforeInserts generated content before — needs content:
::afterInserts generated content after
::first-lineFirst rendered line of a block
::first-letterFirst letter — for drop caps
::selectionThe part the user has highlighted
::placeholderAn input's placeholder text
::markerThe bullet or number of a list item
::backdropThe layer behind a dialog or fullscreen element

Specificity (lowest to highest)

p, ::beforeType and pseudo-element — 0,0,1
.card, [href], :hoverClass, attribute, pseudo-class — 0,1,0
#headerID — 1,0,0
style="..."Inline style — beats all selectors
!importantOverrides everything — avoid where you can
:where(...)Always contributes 0 — useful for defaults

CSS Selectors in practice

How specificity is calculated

When rules conflict, the more specific selector wins, regardless of order. Specificity is compared as three numbers: IDs, then classes, attributes and pseudo-classes, then elements and pseudo-elements. #nav a (1,0,1) beats .menu .link (0,2,0), because one ID outranks any number of classes. Only when specificity ties does the later rule win. Inline styles beat selectors, and !important beats both — which is why overusing it leads to escalating !important wars.

Keeping specificity low

Low, flat specificity makes styles easy to override. Prefer single class selectors over long chains and IDs. :where() contributes zero specificity, so :where(.card) h2 is easy to override, while :is() takes the specificity of its most specific argument. Cascade layers, declared with @layer, let you order whole groups of styles — resets, base, components, utilities — so later layers win regardless of selector specificity.

Modern selectors worth using

:has() selects an element based on its contents: .card:has(img) styles cards that contain an image, and form:has(:invalid) styles a form with errors — the long-awaited "parent selector", now in all major browsers. :focus-visible shows focus rings for keyboard users without showing them on mouse clicks. :not() accepts complex selectors, and :nth-child(2 of .item) counts only matching siblings.

Performance and maintainability

Selector performance rarely matters on modern browsers; maintainability does. Avoid selectors tied to deep HTML structure, such as main > div > ul > li > a, which break when markup changes. Avoid styling by element type inside components, which leaks into nested content. Naming conventions such as BEM, or component-scoped styles, keep selectors predictable.

Debugging which rule wins

Browser developer tools show every rule applied to an element in order of precedence, with overridden declarations crossed out and the specificity of each selector on hover. When a style "does not work", check there: usually a more specific selector, a later rule, a layer or an inherited value is winning, or the property is invalid for that element.

Related guides

More cheat sheets