ā¤ Like
šŸ”– Save
šŸ”— Share
@thedevspaceio
@thedevspaceio

CSS Selectors Cheatsheet

CSS Selectors Cheatsheet

Basic selectors

SelectorMeaningExample
*Universal selector* { ... }
EType selector`p { ... }
.classClass selector.btn { ... }
#idID selector#nav { ... }
[attr]Attribute presencea[target] { ... }

Combinators

SelectorRelationshipExample
A BDescendant. B inside Aarticle p { ... }
A > BDirect childul > li { ... }
A + BAdjacent sibling. B immediately after Ah2 + p { ... }
A ~ BGeneral sibling. B after Ah2 ~ p { ... }

Attribute selectors

SelectorMeaningExample
[attr]Has attributeimg[alt] { ... }
[attr=value]Exact matcha[rel="external"] { ... }
[attr~=value]Word contains[class~="lead"] { ... }
[attr^=value]Starts witha[href^="https"] { ... }
[attr$=value]Ends withimg[src$=".svg"] { ... }
[attr*=value]Contains substring[data-id*="post-"] { ... }

Pseudo-classes (state and structural)

SelectorMeaningExample
:hoverOn hoverbutton:hover { ... }
:focusElement has focusinput:focus { ... }
:activeActivated by usera:active { ... }
:first-childFirst child of parentli:first-child { ... }
:last-childLast child of parentli:last-child { ... }
:nth-child(n)Select by index (1-based)tr:nth-child(odd) { ... }
:nth-of-type(n)Index among typep:nth-of-type(2) { ... }
:not(selector)Negation:not(.muted) { ... }
:disabled, :checkedForm statesinput:checked + label { ... }

Pseudo-elements

SelectorMeaningExample
::beforeInsert content before element.btn::before { ... }
::afterInsert content after element.note::after { ... }
::first-letterFirst letter stylingp::first-letter { ... }
::first-lineFirst line stylingp::first-line { ... }
::selectionSelected text::selection { ... }

Grouping & shorthand

SelectorMeaning
A, BGroup selectors. Apply same rules to both
E:not(:first-child)Combine structural and negation

Selector functions

Common functional pseudo-classes and selector helpers.

FunctionMeaning / notes
:not(X)Select elements that do NOT match X.
:is(X)Matches if element matches any selector in list X.
:where(X)Like :is() but with zero specificity.
:has(X)Parent selector: matches an element if it contains a descendant matching X.
:matches()Legacy alias for :is(). Prefer :is().
:nth-child(an+b)Structural numeric selector. Supports formulas like 2n, odd, even, etc.
:nth-last-child(an+b)Like :nth-child() but counting from the end.
:nth-of-type(an+b)Index among siblings of the same element type.
:nth-last-of-type(an+b)Like :nth-of-type() counting from the end.
:lang(language)Matches elements with the given language (matches lang attribute).