CSS

CSS Selectors Cheatsheet

Quick reference for CSS selectors including element, class, ID, attribute, pseudo-class, pseudo-element, and combinator selectors.

Basic Selectors

SelectorExampleDescription
**Universal: all elements
elementpType: all <p> elements
.class.cardClass: elements with class “card”
#id#headerID: element with id “header”
selector, selectorh1, h2Grouping: matches either

Combinator Selectors

SelectorExampleDescription
A Bdiv pDescendant: p inside div (any depth)
A > Bdiv > pChild: direct p children of div
A + Bh2 + pAdjacent sibling: p immediately after h2
A ~ Bh2 ~ pGeneral sibling: all p after h2 at same level

Attribute Selectors

SelectorDescription
[attr]Has attribute
[attr="val"]Attribute equals “val”
[attr~="val"]Attribute contains word “val” (space-separated)
[attr|="val"]Attribute equals “val” or starts with “val-”
[attr^="val"]Attribute starts with “val”
[attr$="val"]Attribute ends with “val”
[attr*="val"]Attribute contains “val”
[attr="val" i]Case-insensitive match

Pseudo-Classes: State

SelectorDescription
:hoverMouse over element
:activeElement being activated (clicked)
:focusElement has focus
:focus-visibleFocus via keyboard navigation
:focus-withinElement or descendant has focus
:visitedVisited link
:linkUnvisited link
:targetElement targeted by URL fragment
:checkedChecked input/option
:disabledDisabled form element
:enabledEnabled form element
:requiredRequired form field
:optionalOptional form field
:validValid form input
:invalidInvalid form input
:placeholder-shownInput showing placeholder

Pseudo-Classes: Structural

SelectorDescription
:first-childFirst child of parent
:last-childLast child of parent
:nth-child(n)Nth child (1-indexed)
:nth-last-child(n)Nth child from end
:only-childElement with no siblings
:first-of-typeFirst of its type in parent
:last-of-typeLast of its type in parent
:nth-of-type(n)Nth of its type
:nth-last-of-type(n)Nth of type from end
:only-of-typeOnly element of its type
:emptyElement with no children
:rootDocument root (<html>)

nth-child Formulas

PatternMatches
:nth-child(3)3rd child
:nth-child(odd)1st, 3rd, 5th…
:nth-child(even)2nd, 4th, 6th…
:nth-child(2n)Even children
:nth-child(2n+1)Odd children
:nth-child(3n)Every 3rd child
:nth-child(n+4)4th and beyond
:nth-child(-n+3)First 3 children
:nth-child(n+2):nth-child(-n+5)Children 2 through 5

Pseudo-Classes: Other

SelectorDescription
:not(selector)Negation
:is(selector)Matches any in list (forgiving)
:where(selector)Same as :is() but zero specificity
:has(selector)Parent selector (contains match)

Pseudo-Elements

SelectorDescription
::beforeInsert content before element
::afterInsert content after element
::first-lineFirst line of text
::first-letterFirst letter of text
::placeholderPlaceholder text in inputs
::selectionUser-selected text
::markerList item marker

Specificity (low to high)

LevelExampleSpecificity
Universal*0-0-0
Element/pseudo-elementp, ::before0-0-1
Class/attribute/pseudo-class.card, [type], :hover0-1-0
ID#header1-0-0
Inline stylestyle="..."1-0-0-0
!importantOverrides all