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
#header.btn, [type], :hoverdiv, ::before*, :where()Basic Selectors
The fundamental building blocks of CSS selection
*0-0-0Universal
Selects all elements on the page
* { margin: 0; padding: 0; }Click to copyelement0-0-1Type/Element
Selects all elements of the specified type
p { color: blue; }Click to copy.class0-1-0Class
Selects elements with the specified class attribute
.button { background: red; }Click to copy#id1-0-0ID
Selects the element with the specified id attribute
#header { height: 60px; }Click to copy[attr]0-1-0Attribute
Selects elements with the specified attribute
[disabled] { opacity: 0.5; }Click to copyAttribute Selectors
Select elements based on their attributes and values
[attr]0-1-0Has Attribute
Selects elements that have the specified attribute
input[required] { border: 2px solid red; }Click to copy[attr=value]0-1-0Exact Match
Selects elements with attribute exactly equal to value
input[type="text"] { width: 200px; }Click to copy[attr~=value]0-1-0Word Match
Selects elements with attribute containing value as a whole word (space-separated)
[class~="btn"] { cursor: pointer; }Click to copy[attr|=value]0-1-0Prefix 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-0Starts With
Selects elements with attribute value starting with specified value
a[href^="https"] { color: green; }Click to copy[attr$=value]0-1-0Ends 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-0Contains
Selects elements with attribute value containing specified value anywhere
[class*="card"] { border-radius: 8px; }Click to copyCombinator Selectors
Select elements based on their relationship to other elements
A BSum of bothDescendant
Selects all B elements that are descendants of A (any level deep)
nav a { text-decoration: none; }Click to copyA > BSum of bothDirect Child
Selects all B elements that are direct children of A
ul > li { list-style: disc; }Click to copyA + BSum of bothAdjacent Sibling
Selects the B element immediately after A (same parent)
h1 + p { font-size: 1.2em; }Click to copyA ~ BSum of bothGeneral Sibling
Selects all B elements that follow A (same parent)
h1 ~ p { color: gray; }Click to copyPseudo-classes (State)
Select elements based on their current state
:hover0-1-0Hover
Selects element when mouse is over it
button:hover { background: blue; }Click to copy:active0-1-0Active
Selects element while being activated (clicked)
button:active { transform: scale(0.98); }Click to copy:focus0-1-0Focus
Selects element when it has keyboard focus
input:focus { outline: 2px solid blue; }Click to copy:focus-visible0-1-0Focus Visible
Selects element when focused via keyboard (not mouse)
button:focus-visible { outline: 2px dashed blue; }Click to copy:focus-within0-1-0Focus Within
Selects element when it or any descendant has focus
form:focus-within { border-color: blue; }Click to copy:visited0-1-0Visited
Selects links that have been visited
a:visited { color: purple; }Click to copy:link0-1-0Link
Selects unvisited links
a:link { color: blue; }Click to copy:checked0-1-0Checked
Selects checked inputs (checkbox, radio, option)
input:checked { accent-color: green; }Click to copy:disabled0-1-0Disabled
Selects disabled form elements
button:disabled { opacity: 0.5; cursor: not-allowed; }Click to copy:enabled0-1-0Enabled
Selects enabled form elements
input:enabled { background: white; }Click to copy:required0-1-0Required
Selects required form elements
input:required { border-left: 3px solid red; }Click to copy:optional0-1-0Optional
Selects optional form elements (not required)
input:optional { border-left: 3px solid gray; }Click to copy:valid0-1-0Valid
Selects form elements with valid values
input:valid { border-color: green; }Click to copy:invalid0-1-0Invalid
Selects form elements with invalid values
input:invalid { border-color: red; }Click to copy:target0-1-0Target
Selects element with id matching URL fragment
#section:target { background: yellow; }Click to copyPseudo-classes (Structural)
Select elements based on their position in the DOM tree
:first-child0-1-0First Child
Selects element that is first child of its parent
li:first-child { font-weight: bold; }Click to copy:last-child0-1-0Last Child
Selects element that is last child of its parent
li:last-child { border-bottom: none; }Click to copy:only-child0-1-0Only 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-0First of Type
Selects first element of its type among siblings
p:first-of-type { margin-top: 0; }Click to copy:last-of-type0-1-0Last of Type
Selects last element of its type among siblings
p:last-of-type { margin-bottom: 0; }Click to copy:only-of-type0-1-0Only 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-0Nth 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-0Nth 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-0Nth 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-0Nth 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-0Empty
Selects elements with no children (including text)
td:empty { background: #f5f5f5; }Click to copy:root0-1-0Root
Selects the document root element (html)
:root { --primary-color: blue; }Click to copyPseudo-classes (Functional)
Modern functional pseudo-classes for complex selections
:not(selector)0-1-0 + innerNegation
Selects elements that do not match the selector
input:not([type='submit']) { width: 100%; }Click to copy:is(selector)Highest in listIs (Matches-Any)
Matches any element that matches one of the selectors in the list
:is(h1, h2, h3) { font-family: serif; }Click to copyBrowser: Chrome 88+, Firefox 78+, Safari 14+
:where(selector)0-0-0Where
Same as :is() but with zero specificity
:where(h1, h2, h3) { margin-top: 0; }Click to copyBrowser: Chrome 88+, Firefox 78+, Safari 14+
:has(selector)0-1-0 + innerHas (Parent Selector)
Selects element if it contains elements matching the selector
article:has(img) { padding: 20px; }Click to copyBrowser: Chrome 105+, Safari 15.4+, Firefox 121+
Pseudo-elements
Style specific parts of an element
::before0-0-1Before
Inserts content before element's content
.quote::before { content: '"'; color: gray; }Click to copy::after0-0-1After
Inserts content after element's content
.external-link::after { content: ' ↗'; }Click to copy::first-letter0-0-1First Letter
Selects the first letter of block-level elements
p::first-letter { font-size: 2em; float: left; }Click to copy::first-line0-0-1First Line
Selects the first line of block-level elements
p::first-line { font-weight: bold; }Click to copy::selection0-0-1Selection
Styles text selected by user
::selection { background: yellow; color: black; }Click to copy::placeholder0-0-1Placeholder
Styles placeholder text in form inputs
input::placeholder { color: #999; font-style: italic; }Click to copy::marker0-0-1Marker
Styles list item markers (bullets, numbers)
li::marker { color: blue; font-weight: bold; }Click to copyBrowser: Chrome 86+, Firefox 68+, Safari 11.1+
::backdrop0-0-1Backdrop
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.
| Formula | Description | Example |
|---|---|---|
odd | Odd elements (1st, 3rd, 5th...) | tr:nth-child(odd) |
even | Even elements (2nd, 4th, 6th...) | tr:nth-child(even) |
3 | The 3rd element only | li:nth-child(3) |
3n | Every 3rd element (3, 6, 9...) | li:nth-child(3n) |
3n+1 | Every 3rd starting from 1st (1, 4, 7...) | li:nth-child(3n+1) |
n+4 | All from 4th onwards (4, 5, 6...) | li:nth-child(n+4) |
-n+3 | First 3 elements only (1, 2, 3) | li:nth-child(-n+3) |
3n+1 of .active | Every 3rd among .active elements | li: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;
}Explore more CSS tools
CSS Grid Generator
Create grid layouts visually
CSS Flexbox Generator
Create flex layouts visually
CSS Gradient Generator
Create custom gradients
CSS Text Gradient
Gradient text effects
CSS Gradients Collection
275+ ready-to-use gradients
CSS Box Shadow Generator
Design multi-layer shadows
CSS Selection Generator
Style text highlight colors
CSS Scrollbar Generator
Style custom scrollbars
CSS Blend Mode Generator
Mix colors with blend modes
CSS Cursor Generator
Preview all cursor styles
CSS Loader Generator
Create loading animations
CSS Button Generator
Design buttons with hover effects
CSS Glow Generator
Create neon glow effects
Fluid Typography
Scale text across screen sizes
