CSS Selectors Cheatsheet
cssAll CSS selector types, pseudo-classes, and pseudo-elements.
Basic Selectors
elementSelects all elements of the specified type
p { color: blue; }.classnameSelects all elements with the specified class attribute
.btn { padding: 10px; }#idnameSelects the element with the specified id attribute
#header { height: 60px; }*Selects all elements in the document
* { box-sizing: border-box; }[attribute]Selects elements that have the specified attribute
[disabled] { opacity: 0.5; }[attribute="value"]Selects elements with attribute exactly equal to value
[type="text"] { border: 1px solid; }[attribute~="value"]Selects elements with attribute containing value as a whole word
[class~="btn"] { cursor: pointer; }[attribute^="value"]Selects elements with attribute value starting with specified value
[href^="https"] { color: green; }[attribute$="value"]Selects elements with attribute value ending with specified value
[src$=".png"] { border: none; }[attribute*="value"]Selects elements with attribute containing specified substring
[class*="col-"] { float: left; }Combinators
ancestor descendantSelects all descendants of the ancestor element
nav a { text-decoration: none; }parent > childSelects only direct children of the parent element
ul > li { list-style: square; }element + siblingSelects the immediate next sibling element
h1 + p { font-size: 1.2em; }element ~ siblingsSelects all following siblings of the element
h2 ~ p { margin-left: 20px; }selector1, selector2Groups multiple selectors to apply same styles
h1, h2, h3 { font-family: serif; }Structural Pseudo-classes
:first-childSelects elements that are the first child of their parent
li:first-child { font-weight: bold; }:last-childSelects elements that are the last child of their parent
li:last-child { border-bottom: none; }:nth-child(an+b)Selects elements based on their position using a formula
tr:nth-child(odd) { background: #f5f5f5; }:nth-last-child(an+b)Selects elements counting from the last child
li:nth-last-child(2) { color: red; }:only-childSelects elements that are the only child of their parent
p:only-child { margin: 0; }:first-of-typeSelects first element of its type among siblings
p:first-of-type { text-indent: 2em; }:last-of-typeSelects last element of its type among siblings
span:last-of-type { margin-right: 0; }:nth-of-type(an+b)Selects elements of type based on position formula
p:nth-of-type(2n) { background: #eee; }:only-of-typeSelects elements that are the only one of their type
article p:only-of-type { font-size: 1.1em; }:emptySelects elements that have no children or text
td:empty { background: #ccc; }:rootSelects the document root element (html in HTML)
:root { --main-color: #333; }Form State Pseudo-classes
:checkedSelects checked checkboxes and radio buttons
input:checked + label { color: green; }:disabledSelects disabled form elements
input:disabled { background: #ddd; }:enabledSelects enabled form elements
input:enabled { border-color: blue; }:requiredSelects form elements with required attribute
input:required { border-left: 3px solid red; }:optionalSelects form elements without required attribute
input:optional { border-color: gray; }:validSelects form elements with valid input
input:valid { border-color: green; }:invalidSelects form elements with invalid input
input:invalid { border-color: red; }:focusSelects the currently focused element
input:focus { outline: 2px solid blue; }:focus-withinSelects element containing a focused descendant
form:focus-within { box-shadow: 0 0 5px blue; }:placeholder-shownSelects input elements showing placeholder text
input:placeholder-shown { font-style: italic; }:read-onlySelects elements that are not editable by user
input:read-only { background: #f0f0f0; }:read-writeSelects elements that are editable by user
input:read-write { background: white; }Link and Interaction Pseudo-classes
:linkSelects unvisited links
a:link { color: blue; }:visitedSelects visited links
a:visited { color: purple; }:hoverSelects elements when mouse hovers over them
button:hover { background: #eee; }:activeSelects elements being activated (clicked)
a:active { color: red; }:targetSelects element matching the URL fragment identifier
#section:target { background: yellow; }Functional Pseudo-classes
:not(selector)Selects elements that do not match the selector
p:not(.intro) { font-size: 14px; }:is(selector-list)Matches any element in the selector list with forgiving parsing
:is(h1, h2, h3) { color: navy; }:where(selector-list)Like :is() but with zero specificity
:where(article, section) p { line-height: 1.6; }:has(selector)Selects parent elements containing specified descendants
article:has(img) { padding: 20px; }:nth-child(n of selector)Selects nth child matching the selector among siblings
li:nth-child(2 of .active) { color: red; }Pseudo-elements
::beforeCreates a pseudo-element as first child of selected element
.icon::before {
content: '★';
margin-right: 5px;
}::afterCreates a pseudo-element as last child of selected element
.link::after {
content: ' →';
}::first-lineSelects the first line of a block-level element
p::first-line {
font-variant: small-caps;
}::first-letterSelects the first letter of a block-level element
p::first-letter {
font-size: 2em;
float: left;
}::placeholderSelects the placeholder text in form inputs
input::placeholder {
color: #999;
font-style: italic;
}::selectionSelects the portion of text highlighted by user
::selection {
background: yellow;
color: black;
}::markerSelects the marker box of list items
li::marker {
color: red;
font-weight: bold;
}::backdropSelects the backdrop behind fullscreen or dialog elements
dialog::backdrop {
background: rgba(0,0,0,0.5);
}