{ ILoveJS }

CSS Selectors Cheatsheet

css

All CSS selector types, pseudo-classes, and pseudo-elements.

7 sections · 56 items

Basic Selectors

Element Selector
element

Selects all elements of the specified type

css
p { color: blue; }
Class Selector
.classname

Selects all elements with the specified class attribute

css
.btn { padding: 10px; }
ID Selector
#idname

Selects the element with the specified id attribute

css
#header { height: 60px; }
Universal Selector
*

Selects all elements in the document

css
* { box-sizing: border-box; }
Attribute Selector
[attribute]

Selects elements that have the specified attribute

css
[disabled] { opacity: 0.5; }
Attribute Equals
[attribute="value"]

Selects elements with attribute exactly equal to value

css
[type="text"] { border: 1px solid; }
Attribute Contains Word
[attribute~="value"]

Selects elements with attribute containing value as a whole word

css
[class~="btn"] { cursor: pointer; }
Attribute Starts With
[attribute^="value"]

Selects elements with attribute value starting with specified value

css
[href^="https"] { color: green; }
Attribute Ends With
[attribute$="value"]

Selects elements with attribute value ending with specified value

css
[src$=".png"] { border: none; }
Attribute Contains
[attribute*="value"]

Selects elements with attribute containing specified substring

css
[class*="col-"] { float: left; }

Combinators

Descendant Combinator
ancestor descendant

Selects all descendants of the ancestor element

css
nav a { text-decoration: none; }
Child Combinator
parent > child

Selects only direct children of the parent element

css
ul > li { list-style: square; }
Adjacent Sibling Combinator
element + sibling

Selects the immediate next sibling element

css
h1 + p { font-size: 1.2em; }
General Sibling Combinator
element ~ siblings

Selects all following siblings of the element

css
h2 ~ p { margin-left: 20px; }
Selector List
selector1, selector2

Groups multiple selectors to apply same styles

css
h1, h2, h3 { font-family: serif; }

Structural Pseudo-classes

First Child
:first-child

Selects elements that are the first child of their parent

css
li:first-child { font-weight: bold; }
Last Child
:last-child

Selects elements that are the last child of their parent

css
li:last-child { border-bottom: none; }
Nth Child
:nth-child(an+b)

Selects elements based on their position using a formula

css
tr:nth-child(odd) { background: #f5f5f5; }
Nth Last Child
:nth-last-child(an+b)

Selects elements counting from the last child

css
li:nth-last-child(2) { color: red; }
Only Child
:only-child

Selects elements that are the only child of their parent

css
p:only-child { margin: 0; }
First of Type
:first-of-type

Selects first element of its type among siblings

css
p:first-of-type { text-indent: 2em; }
Last of Type
:last-of-type

Selects last element of its type among siblings

css
span:last-of-type { margin-right: 0; }
Nth of Type
:nth-of-type(an+b)

Selects elements of type based on position formula

css
p:nth-of-type(2n) { background: #eee; }
Only of Type
:only-of-type

Selects elements that are the only one of their type

css
article p:only-of-type { font-size: 1.1em; }
Empty
:empty

Selects elements that have no children or text

css
td:empty { background: #ccc; }
Root
:root

Selects the document root element (html in HTML)

css
:root { --main-color: #333; }

Form State Pseudo-classes

Checked
:checked

Selects checked checkboxes and radio buttons

css
input:checked + label { color: green; }
Disabled
:disabled

Selects disabled form elements

css
input:disabled { background: #ddd; }
Enabled
:enabled

Selects enabled form elements

css
input:enabled { border-color: blue; }
Required
:required

Selects form elements with required attribute

css
input:required { border-left: 3px solid red; }
Optional
:optional

Selects form elements without required attribute

css
input:optional { border-color: gray; }
Valid
:valid

Selects form elements with valid input

css
input:valid { border-color: green; }
Invalid
:invalid

Selects form elements with invalid input

css
input:invalid { border-color: red; }
Focus
:focus

Selects the currently focused element

css
input:focus { outline: 2px solid blue; }
Focus Within
:focus-within

Selects element containing a focused descendant

css
form:focus-within { box-shadow: 0 0 5px blue; }
Placeholder Shown
:placeholder-shown

Selects input elements showing placeholder text

css
input:placeholder-shown { font-style: italic; }
Read Only
:read-only

Selects elements that are not editable by user

css
input:read-only { background: #f0f0f0; }
Read Write
:read-write

Selects elements that are editable by user

css
input:read-write { background: white; }

Functional Pseudo-classes

Not
:not(selector)

Selects elements that do not match the selector

css
p:not(.intro) { font-size: 14px; }
Is
:is(selector-list)

Matches any element in the selector list with forgiving parsing

css
:is(h1, h2, h3) { color: navy; }
Where
:where(selector-list)

Like :is() but with zero specificity

css
:where(article, section) p { line-height: 1.6; }
Has
:has(selector)

Selects parent elements containing specified descendants

css
article:has(img) { padding: 20px; }
Nth Child Of
:nth-child(n of selector)

Selects nth child matching the selector among siblings

css
li:nth-child(2 of .active) { color: red; }

Pseudo-elements

Before
::before

Creates a pseudo-element as first child of selected element

css
.icon::before {
  content: '★';
  margin-right: 5px;
}
After
::after

Creates a pseudo-element as last child of selected element

css
.link::after {
  content: ' →';
}
First Line
::first-line

Selects the first line of a block-level element

css
p::first-line {
  font-variant: small-caps;
}
First Letter
::first-letter

Selects the first letter of a block-level element

css
p::first-letter {
  font-size: 2em;
  float: left;
}
Placeholder
::placeholder

Selects the placeholder text in form inputs

css
input::placeholder {
  color: #999;
  font-style: italic;
}
Selection
::selection

Selects the portion of text highlighted by user

css
::selection {
  background: yellow;
  color: black;
}
Marker
::marker

Selects the marker box of list items

css
li::marker {
  color: red;
  font-weight: bold;
}
Backdrop
::backdrop

Selects the backdrop behind fullscreen or dialog elements

css
dialog::backdrop {
  background: rgba(0,0,0,0.5);
}

Related Content