Skip to main content

CSS Selectors Cheatsheet

Complete reference for all CSS selectors with examples and specificity values.
Click any selector to copy its example code.

Understanding Specificity

CSS specificity determines which styles are applied when multiple rules target the same element. Specificity is calculated as three numbers: ID - Class - Element

1-0-0
ID selectors
#header
0-1-0
Classes, attributes, pseudo-classes
.btn, [type], :hover
0-0-1
Elements, pseudo-elements
div, ::before
0-0-0
Universal, :where()
*, :where()

Basic Selectors

The fundamental building blocks of CSS selection

*0-0-0

Universal

Selects all elements on the page

* { margin: 0; padding: 0; }
Click to copy
element0-0-1

Type/Element

Selects all elements of the specified type

p { color: blue; }
Click to copy
.class0-1-0

Class

Selects elements with the specified class attribute

.button { background: red; }
Click to copy
#id1-0-0

ID

Selects the element with the specified id attribute

#header { height: 60px; }
Click to copy
[attr]0-1-0

Attribute

Selects elements with the specified attribute

[disabled] { opacity: 0.5; }
Click to copy

Attribute Selectors

Select elements based on their attributes and values

[attr]0-1-0

Has Attribute

Selects elements that have the specified attribute

input[required] { border: 2px solid red; }
Click to copy
[attr=value]0-1-0

Exact Match

Selects elements with attribute exactly equal to value

input[type="text"] { width: 200px; }
Click to copy
[attr~=value]0-1-0

Word Match

Selects elements with attribute containing value as a whole word (space-separated)

[class~="btn"] { cursor: pointer; }
Click to copy
[attr|=value]0-1-0

Prefix Match (Lang)

Selects elements with attribute equal to or starting with value followed by hyphen

[lang|="en"] { quotes: """ """; }
Click to copy
[attr^=value]0-1-0

Starts With

Selects elements with attribute value starting with specified value

a[href^="https"] { color: green; }
Click to copy
[attr$=value]0-1-0

Ends With

Selects elements with attribute value ending with specified value

a[href$=".pdf"] { background: url(pdf-icon.png); }
Click to copy
[attr*=value]0-1-0

Contains

Selects elements with attribute value containing specified value anywhere

[class*="card"] { border-radius: 8px; }
Click to copy

Combinator Selectors

Select elements based on their relationship to other elements

A BSum of both

Descendant

Selects all B elements that are descendants of A (any level deep)

nav a { text-decoration: none; }
Click to copy
A > BSum of both

Direct Child

Selects all B elements that are direct children of A

ul > li { list-style: disc; }
Click to copy
A + BSum of both

Adjacent Sibling

Selects the B element immediately after A (same parent)

h1 + p { font-size: 1.2em; }
Click to copy
A ~ BSum of both

General Sibling

Selects all B elements that follow A (same parent)

h1 ~ p { color: gray; }
Click to copy

Pseudo-classes (State)

Select elements based on their current state

:hover0-1-0

Hover

Selects element when mouse is over it

button:hover { background: blue; }
Click to copy
:active0-1-0

Active

Selects element while being activated (clicked)

button:active { transform: scale(0.98); }
Click to copy
:focus0-1-0

Focus

Selects element when it has keyboard focus

input:focus { outline: 2px solid blue; }
Click to copy
:focus-visible0-1-0

Focus Visible

Selects element when focused via keyboard (not mouse)

button:focus-visible { outline: 2px dashed blue; }
Click to copy
:focus-within0-1-0

Focus Within

Selects element when it or any descendant has focus

form:focus-within { border-color: blue; }
Click to copy
:visited0-1-0

Visited

Selects links that have been visited

a:visited { color: purple; }
Click to copy
:link0-1-0

Link

Selects unvisited links

a:link { color: blue; }
Click to copy
:checked0-1-0

Checked

Selects checked inputs (checkbox, radio, option)

input:checked { accent-color: green; }
Click to copy
:disabled0-1-0

Disabled

Selects disabled form elements

button:disabled { opacity: 0.5; cursor: not-allowed; }
Click to copy
:enabled0-1-0

Enabled

Selects enabled form elements

input:enabled { background: white; }
Click to copy
:required0-1-0

Required

Selects required form elements

input:required { border-left: 3px solid red; }
Click to copy
:optional0-1-0

Optional

Selects optional form elements (not required)

input:optional { border-left: 3px solid gray; }
Click to copy
:valid0-1-0

Valid

Selects form elements with valid values

input:valid { border-color: green; }
Click to copy
:invalid0-1-0

Invalid

Selects form elements with invalid values

input:invalid { border-color: red; }
Click to copy
:target0-1-0

Target

Selects element with id matching URL fragment

#section:target { background: yellow; }
Click to copy

Pseudo-classes (Structural)

Select elements based on their position in the DOM tree

:first-child0-1-0

First Child

Selects element that is first child of its parent

li:first-child { font-weight: bold; }
Click to copy
:last-child0-1-0

Last Child

Selects element that is last child of its parent

li:last-child { border-bottom: none; }
Click to copy
:only-child0-1-0

Only Child

Selects element that is the only child of its parent

p:only-child { font-size: 1.2em; }
Click to copy
:first-of-type0-1-0

First of Type

Selects first element of its type among siblings

p:first-of-type { margin-top: 0; }
Click to copy
:last-of-type0-1-0

Last of Type

Selects last element of its type among siblings

p:last-of-type { margin-bottom: 0; }
Click to copy
:only-of-type0-1-0

Only of Type

Selects element that is only one of its type among siblings

img:only-of-type { display: block; margin: auto; }
Click to copy
:nth-child(n)0-1-0

Nth Child

Selects element based on its position among siblings (1-indexed)

tr:nth-child(odd) { background: #f0f0f0; }
Click to copy
:nth-last-child(n)0-1-0

Nth Last Child

Selects element based on position counting from end

li:nth-last-child(2) { color: blue; }
Click to copy
:nth-of-type(n)0-1-0

Nth of Type

Selects element based on position among same-type siblings

p:nth-of-type(2) { font-style: italic; }
Click to copy
:nth-last-of-type(n)0-1-0

Nth Last of Type

Selects element based on position among same-type siblings from end

img:nth-last-of-type(1) { border: 2px solid blue; }
Click to copy
:empty0-1-0

Empty

Selects elements with no children (including text)

td:empty { background: #f5f5f5; }
Click to copy
:root0-1-0

Root

Selects the document root element (html)

:root { --primary-color: blue; }
Click to copy

Pseudo-classes (Functional)

Modern functional pseudo-classes for complex selections

:not(selector)0-1-0 + inner

Negation

Selects elements that do not match the selector

input:not([type='submit']) { width: 100%; }
Click to copy
:is(selector)Highest in list

Is (Matches-Any)

Matches any element that matches one of the selectors in the list

:is(h1, h2, h3) { font-family: serif; }
Click to copy

Browser: Chrome 88+, Firefox 78+, Safari 14+

:where(selector)0-0-0

Where

Same as :is() but with zero specificity

:where(h1, h2, h3) { margin-top: 0; }
Click to copy

Browser: Chrome 88+, Firefox 78+, Safari 14+

:has(selector)0-1-0 + inner

Has (Parent Selector)

Selects element if it contains elements matching the selector

article:has(img) { padding: 20px; }
Click to copy

Browser: Chrome 105+, Safari 15.4+, Firefox 121+

Pseudo-elements

Style specific parts of an element

::before0-0-1

Before

Inserts content before element's content

.quote::before { content: '"'; color: gray; }
Click to copy
::after0-0-1

After

Inserts content after element's content

.external-link::after { content: ' ↗'; }
Click to copy
::first-letter0-0-1

First Letter

Selects the first letter of block-level elements

p::first-letter { font-size: 2em; float: left; }
Click to copy
::first-line0-0-1

First Line

Selects the first line of block-level elements

p::first-line { font-weight: bold; }
Click to copy
::selection0-0-1

Selection

Styles text selected by user

::selection { background: yellow; color: black; }
Click to copy
::placeholder0-0-1

Placeholder

Styles placeholder text in form inputs

input::placeholder { color: #999; font-style: italic; }
Click to copy
::marker0-0-1

Marker

Styles list item markers (bullets, numbers)

li::marker { color: blue; font-weight: bold; }
Click to copy

Browser: Chrome 86+, Firefox 68+, Safari 11.1+

::backdrop0-0-1

Backdrop

Styles the backdrop behind dialog or fullscreen elements

dialog::backdrop { background: rgba(0,0,0,0.5); }
Click to copy

:nth-child() Formula Reference

The :nth-child() selector uses the formula an+b where a is the cycle size and b is the offset.

FormulaDescriptionExample
oddOdd elements (1st, 3rd, 5th...)tr:nth-child(odd)
evenEven elements (2nd, 4th, 6th...)tr:nth-child(even)
3The 3rd element onlyli:nth-child(3)
3nEvery 3rd element (3, 6, 9...)li:nth-child(3n)
3n+1Every 3rd starting from 1st (1, 4, 7...)li:nth-child(3n+1)
n+4All from 4th onwards (4, 5, 6...)li:nth-child(n+4)
-n+3First 3 elements only (1, 2, 3)li:nth-child(-n+3)
3n+1 of .activeEvery 3rd among .active elementsli:nth-child(3n+1 of .active)

Interactive Selector Demo

Hover over the list items to see different selectors in action:

:nth-child() examples

  • First item (yellow bg)
  • Second item (purple border)
  • Third item (green border)
  • Fourth item (purple border)
  • Fifth/Last item (blue bg)

:hover and state examples

Common Selector Patterns

Zebra-striped table

tr:nth-child(odd) {
  background: #f9fafb;
}
tr:nth-child(even) {
  background: #ffffff;
}

First/last child borders

li { border-bottom: 1px solid #e5e7eb; }
li:last-child { border-bottom: none; }
li:first-child { border-top-radius: 8px; }
li:last-child { border-bottom-radius: 8px; }

External links indicator

a[href^="http"]::after {
  content: " ↗";
  font-size: 0.8em;
}

Required field indicator

input:required {
  border-left: 3px solid #ef4444;
}
label:has(+ input:required)::after {
  content: " *";
  color: #ef4444;
}