Demo

CSS Selectors Reference

Complete reference of all CSS selectors

About This Reference

This comprehensive reference lists all CSS selectors organized by category. Selectors are patterns used to select elements you want to style. Understanding selectors is fundamental to working with CSS effectively.

Each selector includes a description of its purpose and links to live demos (where available) and the official W3C specification. Click section headings below to view related tutorials.

Basic Selectors

Selector Description Links
* Universal selector - selects all elements
element Type selector - selects all elements of that type
.class Class selector - selects elements with that class
#id ID selector - selects element with that ID

Combinators

Selector Description Links
A B Descendant combinator - B is inside A (any level)
A > B Child combinator - B is direct child of A
A + B Adjacent sibling - B immediately follows A
A ~ B General sibling - B follows A (not necessarily immediately)
A || B Column combinator - B is in same grid/table column as A (experimental)

Attribute Selectors

Selector Description Links
[attr] Has attribute
[attr="value"] Attribute equals exactly
[attr~="value"] Attribute contains word
[attr^="value"] Attribute starts with
[attr$="value"] Attribute ends with
[attr*="value"] Attribute contains substring
[attr|="value"] Attribute equals or starts with value followed by hyphen
[attr="value" i] Case-insensitive attribute match (works with all attribute selectors)
[attr="value" s] Case-sensitive attribute match (explicit, default for most attributes)

Pseudo-classes: Child Position

Selector Description Links
:first-child First child of parent
:last-child Last child of parent
:nth-child(n) Nth child (1-indexed, supports formulas like 2n, 3n+1)
:nth-child(n of S) Nth child matching selector S (e.g., :nth-child(2 of .active) for 2nd element with .active class)
:nth-last-child(n) Nth child from end
:first-of-type First element of its type
:last-of-type Last element of its type
:nth-of-type(n) Nth element of its type
:only-child Only child of parent
:only-of-type Only element of its type

Pseudo-classes: Form States

Selector Description Links
:checked Checkbox or radio is checked
:disabled Form element is disabled
:enabled Form element is enabled
:required Form element is required
:optional Form element is optional
:valid Form element passes validation
:invalid Form element fails validation
:in-range Input value is in range
:out-of-range Input value is out of range
:placeholder-shown Input is showing placeholder

Modern Selectors

Selector Description Links
:is(A, B) Matches if element matches any selector (forgiving)
:where(A, B) Like :is() but with 0 specificity
:has(selector) Parent selector - element that contains selector
:not(selector) Negation - does not match selector

Pseudo-Elements

Selector Description Links
::before Insert content before element
::after Insert content after element
::first-letter First letter of element
::first-line First line of element
::selection Highlighted/selected text
::marker List item marker
::placeholder Input placeholder text

Other Useful Selectors

Selector Description Links
:root Document root element (usually <html>)
:empty Element with no children
:target Element matching URL fragment
:lang(language) Element with specified language

Understanding Specificity

When multiple selectors target the same element, specificity determines which styles apply. Specificity is calculated as a three-part value: (ID count, Class count, Type count).

Specificity Hierarchy

10,0,0 Inline Styles
style="color: red"

Inline styles have highest specificity (except !important)

0,1,0,0 ID Selectors
#header

Each ID adds 100 to specificity

0,0,1,0 Classes, Attributes, Pseudo-classes
.button, [type="text"], :hover

Each class, attribute, or pseudo-class adds 10 to specificity

0,0,0,1 Elements & Pseudo-elements
div, ::before

Each element or pseudo-element adds 1 to specificity

0,0,0,0 Universal Selector
*

The universal selector has no specificity

Calculation Examples

Selector Specificity Calculation
div 0,0,0,1 1 element
.button 0,0,1,0 1 class
#header 0,1,0,0 1 ID
div.button 0,0,1,1 1 class + 1 element
#header .nav a 0,1,1,1 1 ID + 1 class + 1 element
ul li:first-child 0,0,1,2 1 pseudo-class + 2 elements
:is(#id, .class) 0,1,0,0 Takes highest (ID)
:where(#id, .class) 0,0,0,0 Always 0 specificity

Key Rules

  • Combinators (>, +, ~, space) add no specificity
  • :is() and :not() take the specificity of their most specific argument
  • :where() always has 0 specificity (useful for easy overrides)
  • :has() follows the same rules as :is()
  • !important overrides all specificity (use sparingly)
  • When specificity is equal, the last rule in source order wins

Resources & Tools