Code & computing / QUICK REFERENCE

CSS selectors, pseudo-classes and specificity

Choose elements by structure, attribute, state and relationship, and understand selector specificity.

61 entries

Basic and compound selectors

Every paragraph
p

Type selector matches all p elements. [1][5]

Unique ID
#menu

Matches element with the ID value menu. [1][5]

Class name
.card

Finds all elements with class token card. [1][5]

Multiple classes
.card.selected

Matches elements carrying both class tokens. [1][5]

Element with class
button.primary

Matches only button elements also having primary class. [1][5]

Any element
*

The universal selector matches every element in scope. [1][5]

Two kinds of elements
h1, h2

Comma-separated selector list matches either heading type. [1][5]

A class within an ID
#menu .item

A descendant with class item under the menu element. [1][5]

Child of parent
nav > a

Selects direct a children of nav, not deeper nested links. [1][5]

Any descendant
article a

Selects a links at any descendant depth inside article. [1][5]

Adjacent sibling
h2 + p

A paragraph immediately after an h2 sharing the same parent. [1][5]

Following siblings
h2 ~ p

Any subsequent p sibling after an h2 in the same parent. [1][5]

Direct list item
ul > li

Matches direct items of unordered lists only. [1][5]

Nested link target
.card > a

The anchor must be a direct child of card. [1][5]

Selector list inside :is
:is(h1, h2)

Matches either heading using the matches-any pseudo-class. [1][5]

Attribute and relational patterns

Present attribute
a[download]

Matches links bearing a download attribute, regardless of value. [2][1]

Exact attribute
input[type="email"]

Matches inputs with that exact type value. [2][1]

Attribute begins with
a[href^="https:"]

Selects links whose href starts with HTTPS scheme text. [2][1]

Attribute ends with
a[href$=".pdf"]

Selects URLs ending in lowercase .pdf, including query-string caveats. [2][1]

Substring in attribute
a[href*="/docs/"]

Finds links whose href contains the literal substring. [2][1]

Whitespace token
[class~="active"]

Matches a whitespace-separated token in attribute value. [2][1]

Language prefix
[lang|="en"]

Matches en or an en- prefixed language tag. [2][1]

Case-insensitive value
[data-state="OPEN" i]

Matches ASCII-case-insensitive attribute values. [2][1]

Not disabled
button:not(:disabled)

Excludes buttons disabled by the HTML disabled state. [2][1]

Either input state
input:is(:checked,:focus)

Matches checked inputs or focused input. [2][1]

Low-specificity wrapper
:where(.toolbar) button

Matches descendants but :where() contributes zero specificity. [2][1]

Card containing image
.card:has(img)

Selects card elements containing a descendant img. [2][1]

Form with invalid field
form:has(input:invalid)

Matches forms containing invalid input elements. [2][1]

Section with direct heading
section:has(> h2)

Matches sections with a direct h2 child. [2][1]

Exclude nested cards
.card:not(.nested)

Filters to cards without nested class. [2][1]

State and positional pseudo-classes

First child
li:first-child

List item must be the first element child in its parent. [1][3]

Last child
li:last-child

List item must be the last element child. [1][3]

Second child
li:nth-child(2)

Matches an li that is exactly second among all element siblings. [1][3]

Odd child
li:nth-child(odd)

Selects li at odd sibling positions. [1][3]

Even child
li:nth-child(even)

Selects li at even sibling positions. [1][3]

First of its type
p:first-of-type

First p among p siblings even if not first child. [1][3]

Last of its type
p:last-of-type

Last p among elements of type p in that parent. [1][3]

Only child
.notice:only-child

Matches a notice when it has no element siblings. [1][3]

Empty element
div:empty

No element or text nodes; whitespace text counts as content. [1][3]

Focused input
input:focus

Match the focused input element. [1][3]

Visible keyboard focus
:focus-visible

Focus indication for input modality and browser heuristics. [1][3]

Hovered interactive
a:hover

Matches a link while pointer is over it. [1][3]

Checked selection
input:checked

Checked checkbox or radio input. [1][3]

Required form value
input:required

Form input marked as required. [1][3]

Invalid value
input:invalid

Built-in constraint validation says current input is invalid. [1][3]

Disabled button
button:disabled

Matches buttons disabled through HTML semantics. [1][3]

Root of DOM
:root

Matches the document root element in a normal HTML document. [1][3]

Specificity and cascade decisions

Type selector weight
p → (0,0,1)

One type selector contributes one element unit. [3][4]

Class selector weight
.card → (0,1,0)

Each class contributes to the middle specificity column. [3][4]

ID selector weight
#main → (1,0,0)

A single ID outweighs any number of class selectors. [3][4]

Compound weight
div.card:hover → (0,2,1)

One type and two class/pseudo-class units. [3][4]

Descendant weight
#app .card a → (1,1,1)

Combinators themselves do not add specificity. [3][4]

:where contributes zero
:where(#app, .card) → (0,0,0)

Its argument weight is deliberately ignored. [3][4]

:is takes strongest argument
:is(#app, .card) → (1,0,0)

It uses maximum specificity among listed selectors. [3][4]

:not parameter counts
:not(.disabled) → (0,1,0)

The not pseudo-class itself adds none, its argument does. [3][4]

:has parameter counts
.card:has(img) → (0,1,1)

Include card class plus the selected argument's type. [3][4]

Later equal-weight rule wins
Same origin, layer, scope proximity and specificity → later declaration

Source order is the final tie-break after earlier cascade stages. [3][4]

Origin and importance first
!important can win against more specific normal declaration

Cascade origin, importance and layer are evaluated before specificity. [3][4]

Normal unlayered priority
Normal author unlayered rules outrank normal authored layers

A later layer may beat an earlier layer, but unlayered normal wins over all normal layers. [3][4]

Important layers reverse
Earlier important author layer outranks later important layer

Importance reverses layer precedence compared with normal declarations. [3][4]

Avoid specificity escalation
Prefer class-scoped selectors and cascade layers

Repeating fake IDs to force priority creates hard-to-maintain styles. [3][4]

Reserved placement, separate from reference controls. No ad network is active.

Updated CSS Selectors 4 · modern browsers

Sources

  1. CSS selector reference ↗
  2. Attribute selectors ↗
  3. CSS specificity ↗
  4. Introduction to CSS cascade ↗
  5. Selector lists ↗