Overview

Pseudo-classes select elements by state or position and score (0,1,0); pseudo-elements select parts of an element and score (0,0,1). Basic, combinator, and logical selectors are in css-selectors.

State pseudo-classes

Use :focus-visible for focus rings: it shows for keyboard focus and hides on mouse clicks.

Pseudo-classMatches
:hover, :activePointer over / being pressed.
:focus, :focus-visible, :focus-withinFocused / focus the browser shows / contains a focused element.
:checked, :disabled, :requiredForm control state.
:valid, :invalidValidity, evaluated from page load.
:user-invalidInvalid after the user interacted, so empty required fields do not flash red.
:emptyNo children, whitespace text included.
:targetElement matching the URL fragment.
:rootThe document root (html).
:link, :visitedUnvisited / visited links (:visited styling is restricted for privacy).

Structural pseudo-classes

Use :nth-of-type when siblings have different tags and :nth-child when position alone matters.

Pseudo-classMatches
:first-child, :last-child, :only-childPosition among all siblings.
:first-of-type, :last-of-typePosition among siblings of the same tag.
:nth-child(2), :nth-child(odd), :nth-child(3n)Second / odd / every third child (1-based).
:nth-last-child(1)Counted from the end.
:nth-of-type(2)Second of its tag.

:nth-child counts siblings of any tag; p:nth-child(2) matches only a p that is also the second child.

Pseudo-elements

Use the double-colon form; single-colon :before and :after are legacy spellings.

Pseudo-elementTargets
::before, ::afterGenerated content inside the element.
::first-line, ::first-letterFirst rendered line / letter.
::markerList bullet or number.
::placeholderInput placeholder text.
::selectionSelected text.
::backdropBackdrop of a modal <dialog> or fullscreen element.