CSS Selectors Reference

Every selector type, specificity rules, and 2026 additions like :has() and @scope.

By Λ · Updated May 18, 2026

Specificity 101

Specificity decides which CSS rule wins when multiple rules target the same element. Three categories, scored as (a, b, c):

a
Inline + IDs
Inline style="" attribute, then #id selectors
b
Classes + Attrs + Pseudo-classes
.class, [attr], :hover
c
Type + Pseudo-elements
div, ::before

Higher tier always wins regardless of count. #id (1,0,0) beats .a.b.c.d (0,4,0). Inline style beats all selectors except !important. !important beats everything except later !important declarations.

Basic Selectors

SelectorMatchesSpecificity
*Universal (any element)(0,0,0)
elementBy tag name (e.g. div, h1)(0,0,1)
.classBy class name(0,1,0)
#idBy id attribute(1,0,0)
[attr]Has attribute(0,1,0)
[attr="value"]Attribute equals value(0,1,0)
[attr~="word"]Attribute contains word (space-separated)(0,1,0)
[attr|="lang"]Equals "lang" or starts with "lang-"(0,1,0)
[attr^="prefix"]Starts with prefix(0,1,0)
[attr$="suffix"]Ends with suffix(0,1,0)
[attr*="substring"]Contains substring(0,1,0)

Combinators

CombinatorMatchesExample
A BDescendant (any depth below A)article p
A > BDirect childul > li
A + BAdjacent sibling (immediately after)h2 + p
A ~ BGeneral sibling (any after, same parent)h2 ~ p

Pseudo-classes (state)

:hoverMouse over
:focusKeyboard focus
:focus-visibleFocus from keyboard (not mouse click)
:focus-withinSelf or any descendant has focus
:activeBeing clicked
:visitedAlready-visited link
:checkedChecked checkbox/radio
:disabled / :enabledForm control state
:required / :optionalRequired attribute state
:valid / :invalidForm validation state
:placeholder-shownInput is showing placeholder
:read-only / :read-writeEditable state

Pseudo-classes (structural)

:first-childFirst among siblings
:last-childLast among siblings
:only-childOnly child
:nth-child(n)nth child (1-based)
:nth-child(2n)Every even child
:nth-child(2n+1)Every odd child
:nth-of-type(n)nth among same tag siblings
:first-of-typeFirst sibling of its type
:emptyNo children (not even text)
:rootThe root element (usually <html>)
:not(selector)Does not match selector
:lang(en)Element with lang attribute

Modern selectors (2022-2026)

:is(a, b, c)Matches any selector in the list. Takes highest specificity of arguments.
:where(a, b, c)Like :is() but always specificity (0,0,0). Great for resets.
:has(selector)Parent selector. div:has(> img) matches divs with an img child.
:not(a, b)Modern :not() accepts multiple selectors.
:dir(ltr)Element with reading direction
:user-invalidInvalid input but only after user interaction
:popover-openOpen popover element (Popover API)

Pseudo-elements

::beforeInserted before content. Requires content: property.
::afterInserted after content.
::first-letterFirst letter of block.
::first-lineFirst rendered line.
::selectionSelected text.
::placeholderPlaceholder text in input.
::markerList bullet/number.
::backdropBackdrop of fullscreen / dialog element.

Common patterns

Last updated