Skip to content
back to cheatsheets

CSS Selectors Cheatsheet — Every Selector with Examples

· Reference

CSS selectors define which HTML elements a set of CSS rules applies to. Mastering selectors lets you target any element precisely without adding extra classes or IDs. This reference covers every selector type you can use in modern CSS.

Basic Selectors

SelectorExampleSelects
** { }All elements
elementp { }All <p> elements
.class.card { }Elements with class="card"
#id#header { }Element with id="header"
selector, selectorh1, h2 { }All <h1> and <h2> elements

Combinator Selectors

Combinators describe the relationship between two selectors.

CombinatorExampleSelects
A B (descendant)nav a { }All <a> inside <nav>, at any depth
A > B (child)ul > li { }<li> that are direct children of <ul>
A + B (adjacent sibling)h2 + p { }First <p> immediately after <h2>
A ~ B (general sibling)h2 ~ p { }All <p> siblings after <h2>

Attribute Selectors

Target elements based on their attributes and attribute values.

SelectorExampleSelects
[attr][disabled] { }Elements with the disabled attribute
[attr="value"][type="text"] { }Exact match
[attr~="value"][class~="card"] { }Attribute contains word in space-separated list
[attr|="value"][lang|="en"] { }Value is en or starts with en-
[attr^="value"][href^="https"] { }Attribute starts with value
[attr$="value"][src$=".png"] { }Attribute ends with value
[attr*="value"][class*="btn"] { }Attribute contains value anywhere
[attr="value" i][type="text" i] { }Case-insensitive match

Pseudo-Classes — State

These apply when an element is in a specific state.

SelectorSelects
:hoverElement being hovered by the pointer
:focusElement that has keyboard focus
:focus-visibleElement that has focus and the browser thinks a focus indicator should be shown
:focus-withinElement that contains a focused element
:activeElement being activated (clicked/tapped)
:visitedLinks that have been visited
:linkUnvisited links
:targetElement whose id matches the URL fragment
:checkedChecked checkboxes, radio buttons, or selected options
:indeterminateCheckboxes/radios in an indeterminate state
:disabledDisabled form elements
:enabledEnabled form elements
:requiredForm elements with the required attribute
:optionalForm elements without required
:validForm elements passing validation
:invalidForm elements failing validation
:in-rangeInput elements within their min/max range
:out-of-rangeInput elements outside their min/max range
:placeholder-shownInput elements currently showing placeholder text
:read-onlyElements that are not editable
:read-writeElements that are editable
:defaultThe default button or form element

Pseudo-Classes — Structural

These select elements based on their position in the document tree.

SelectorSelects
:first-childFirst child of its parent
:last-childLast child of its parent
:only-childElement with no siblings
:nth-child(n)nth child (1-indexed)
:nth-last-child(n)nth child from the end
:first-of-typeFirst element of its type among siblings
:last-of-typeLast element of its type among siblings
:only-of-typeElement with no siblings of the same type
:nth-of-type(n)nth element of its type
:nth-last-of-type(n)nth element of its type from the end
:rootThe document root element (<html>)
:emptyElements with no children (including text nodes)

nth-child Formulas

The n in :nth-child() accepts formulas:

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

Pseudo-Classes — Functional

SelectorSelects
:is(selector)Matches any of the listed selectors. Forgiving (ignores invalid selectors).
:where(selector)Same as :is() but with zero specificity.
:not(selector)Elements that do not match the selector.
:has(selector)Parent elements that contain a matching descendant. The “parent selector.”
/* Style cards that contain an image */
.card:has(img) {
  padding: 0;
}

/* Style any heading */
:is(h1, h2, h3, h4) {
  font-weight: bold;
}

/* Style links that are not in nav */
a:not(nav a) {
  text-decoration: underline;
}

Pseudo-Elements

Pseudo-elements target specific parts of an element rather than the element itself.

SelectorCreates/Selects
::beforeInserts content before the element’s content
::afterInserts content after the element’s content
::first-lineThe first line of a block element
::first-letterThe first letter of a block element
::placeholderPlaceholder text in form inputs
::selectionText selected/highlighted by the user
::markerThe bullet or number of a list item
::backdropThe backdrop behind a <dialog> or fullscreen element
/* Add a decorative arrow after links */
a.external::after {
  content: " \2192";
}

/* Style the first letter of a paragraph */
p::first-letter {
  font-size: 2em;
  color: var(--accent);
}

Specificity

When multiple selectors target the same element, specificity determines which rules win.

Selector TypeSpecificityExample
Inline styles1-0-0-0style="color: red"
ID selectors0-1-0-0#header
Class, attribute, pseudo-class0-0-1-0.card, [type], :hover
Element, pseudo-element0-0-0-1div, ::before
Universal (*), combinators0-0-0-0*, >, +, ~
:where()0-0-0-0Always zero specificity
:is(), :not(), :has()Uses highest specificity of its arguments

Explore selectors interactively with the CSS Selectors Reference tool.

#Learn More