DevToolsForYou

CSS Selectors Cheatsheet

A quick reference for CSS selector syntax — type, class, ID, attribute, pseudo-class, pseudo-element, and combinator selectors.

Updated Apr 11, 2026

Sections

  1. Basic Selectors
  2. Attribute Selectors
  3. Combinators
  4. Pseudo-Classes
  5. Pseudo-Elements
  6. Specificity

Basic Selectors

SelectorExampleMatches
TypepAll <p> elements
Class.cardElements with class="card"
ID#headerElement with id="header"
Universal*Every element
Multipleh1, h2All <h1> and <h2> elements

Attribute Selectors

SelectorMatches
[attr]Elements with the attribute present
[attr=val]Attribute equals exactly val
[attr~=val]Attribute contains val as a whitespace-separated word
[attr|=val]Attribute starts with val or val-
[attr^=val]Attribute starts with val
[attr$=val]Attribute ends with val
[attr*=val]Attribute contains val anywhere
[attr=val i]Case-insensitive match

Combinators

SelectorExampleMatches
Descendant (space)div p<p> anywhere inside a <div>
Child (>)ul > li<li> that is a direct child of <ul>
Adjacent sibling (+)h2 + p<p> immediately after an <h2>
General sibling (~)h2 ~ pAll <p> siblings after an <h2>

Pseudo-Classes

SelectorMatches
:hoverElement being hovered
:focusElement with keyboard/pointer focus
:activeElement being activated (e.g. clicked)
:visited<a> that has been visited
:link<a> not yet visited
:checkedChecked checkbox or radio input
:disabledDisabled form element
:enabledEnabled form element
:requiredInput with required attribute
:optionalInput without required attribute
:valid / :invalidInput that passes / fails constraint validation
:first-childElement that is the first child of its parent
:last-childElement that is the last child of its parent
:nth-child(n)Element that is the nth child (1-based, e.g. 2n+1 = odd)
:nth-of-type(n)nth element of its type among siblings
:first-of-typeFirst element of its type in its parent
:last-of-typeLast element of its type in its parent
:only-childElement with no siblings
:only-of-typeOnly element of its type in its parent
:emptyElement with no children (including text)
:not(selector)Element that does NOT match selector
:is(selector-list)Matches any of the listed selectors (forgiving)
:has(selector)Element that contains a matching descendant
:rootRoot element of the document — usually <html>
:targetElement whose id matches the URL fragment

Pseudo-Elements

SelectorCreates / Matches
::beforeInserts generated content before the element's content
::afterInserts generated content after the element's content
::first-lineThe first line of a block element
::first-letterThe first letter of a block element
::placeholderPlaceholder text of an input or textarea
::selectionPortion of the element selected by the user
::markerMarker box of a list item (bullet or number)

Specificity

Specificity is calculated as (ID, Class/Attribute/Pseudo-class, Type/Pseudo-element). Higher specificity wins.

Selector TypeSpecificity
Inline style1,0,0,0 — highest
ID selector (#id)0,1,0,0
Class, attribute, pseudo-class0,0,1,0
Type, pseudo-element0,0,0,1
Universal (*), combinators0,0,0,0
!importantOverrides all — avoid except as last resort