Cheat Sheet

CSS Selectors Cheat Sheet — Complete Reference with Examples

Every CSS selector that matters: type, class, ID, attribute, pseudo-class, pseudo-element, combinators. Free, with specificity rules and real-world patterns.

Complete CSS selector reference, ordered by how often you'll actually use them. Format CSS with the CSS formatter; minify before deploy with CSS minifier.

Basic selectors

SelectorMatches
*Every element (the universal selector)
divEvery <div> element
.btnElements with class="btn"
#navElement with id="nav"
div.btn<div> with class="btn"
.btn.primaryElements with both classes "btn" AND "primary"
.btn, .linkAnything matching ".btn" OR ".link"

Combinators

SelectorMatches
div pAny <p> inside any <div> (descendant)
div > pAny <p> that's a direct child of <div>
h2 + p<p> immediately after a sibling <h2>
h2 ~ pEvery <p> following a sibling <h2>

Attribute selectors

SelectorMatches
[disabled]Elements with the disabled attribute (any value)
[type="email"]Exact attribute match
[href^="https"]href that starts with "https"
[href$=".pdf"]href that ends with ".pdf"
[href*="utilko"]href that contains "utilko"
[lang|="en"]lang exactly "en" OR starting with "en-"
[class~="btn"]class is exactly "btn" OR contains it as a separate word
[type="email" i]Case-insensitive match (modern browsers)

Pseudo-classes — state

SelectorMatches
:hoverElement under the cursor
:focusElement with keyboard/click focus
:focus-visibleFocus from keyboard navigation only (modern; better default than :focus)
:focus-withinElement OR any descendant has focus
:activeElement being pressed
:checkedChecked checkbox/radio
:disabled / :enabledForm control state
:required / :optionalForm field requirement
:valid / :invalidForm validation state
:placeholder-shownInput is empty (showing placeholder)
:visitedLink the user has visited (limited styling for privacy)
:targetElement matching URL fragment (#section)

Pseudo-classes — structural

SelectorMatches
:first-childFirst child of its parent
:last-childLast child
:only-childOnly child (no siblings)
:nth-child(2)2nd child
:nth-child(odd) / :nth-child(even)Alternating
:nth-child(3n+1)1st, 4th, 7th, … (formula)
:nth-last-child(1)Counting from the end
:nth-of-type(2)2nd of its element type (ignores other tags)
:first-of-type / :last-of-typeFirst/last of element type
:emptyElement with no children (including text)
:not(.btn)Anything NOT matching ".btn"
:is(h1, h2, h3)Matches any in the list (specificity = highest)
:where(h1, h2)Like :is() but specificity is 0
:has(img)Element that contains an <img> (modern; "parent selector")

Pseudo-elements (use ::)

SelectorTargets
::beforeGenerated content before element's content
::afterGenerated content after
::first-letterFirst letter (drop caps)
::first-lineFirst rendered line
::selectionHighlighted/selected text
::placeholderInput's placeholder text
::markerList item bullet/number
::backdropBackdrop behind <dialog> / fullscreen

Specificity (the silent ranking)

When multiple rules match, specificity decides which wins. Higher number → wins.

SelectorSpecificity (a, b, c)
Inline style="..."(1, 0, 0, 0) — beats anything in CSS
#id(0, 1, 0, 0)
.class / [attr] / :hover(0, 0, 1, 0)
tag / ::before(0, 0, 0, 1)
*(0, 0, 0, 0)
!importantWins anyway (use sparingly; debt)

Tie-breaker: later rule in source order wins.

Common patterns

/* Style the parent of an element with X */
.card:has(> .featured) { border: 2px solid gold; }

/* Every link except internal */
a:not([href^="/"]):not([href^="#"]) { color: var(--external); }

/* First paragraph after every h2 */
h2 + p { font-size: 1.1em; }

/* Zebra-stripe table rows */
tbody tr:nth-child(even) { background: #f9fafb; }

/* Hide element when input is focused */
.tooltip { display: block; }
input:focus + .tooltip { display: none; }

/* Style button only when valid form */
form:valid button[type="submit"] { background: #059669; }

/* Empty state for an empty container */
.list:empty::after { content: 'No results'; color: #888; }

Related tools

Format CSS: CSS formatter. Minify for production: CSS minifier. Pick brand-safe colors: color converter. Preview rendered HTML to test selectors: HTML preview.

Featured Tools

Try these free tools directly in your browser — no sign-up required.

css selectors css cheat sheet pseudo class attribute selector css specificity

Explore 300+ Free Tools

Utilko has tools for developers, writers, designers, students, and everyday users — all free, all browser-based.