CSS Pseudo-Classes Cheatsheet
Complete reference for all CSS pseudo-classes.
Click any example to copy the code to your clipboard.
Interactive Demos
:hover Demo
button:hover {
background: primary/80;
transform: scale(1.05);
box-shadow: 0 10px 25px rgba(0,0,0,0.2);
}:focus vs :focus-visible Demo
Tip: :focus-visible only shows outline when using keyboard navigation (Tab key), not mouse clicks.
:checked Demo
input:checked + span {
font-weight: bold;
color: var(--primary);
}:valid / :invalid Demo
Type an email to see validation styles
input:valid { border-color: green; }
input:invalid { border-color: red; }:focus-within Demo
This container uses :focus-within
.container:focus-within {
border-color: primary;
background: primary/5;
}:nth-child() Interactive Demo
Odd elements (1st, 3rd, 5th...)
li:nth-child(odd) {
background: var(--primary);
color: white;
}:has() - The Parent Selector (Game-Changer!)
Finally! Select parent elements based on their children. This was impossible in CSS for decades.

Card with image (styled with :has)
Card without image (no :has styles)
/* Style parent based on child */
.card:has(img) {
background: primary/10;
border-color: primary/30;
}
/* Style form if any input is invalid */
form:has(input:invalid) {
border-color: red;
}
/* Show sibling when checkbox is checked */
input:has(+ .dropdown:focus) {
border-color: blue;
}User Action
Pseudo-classes that respond to user interactions
:hoverAll browsersApplies when mouse is over the element
:activeAll browsersApplies while element is being clicked/pressed
:focusAll browsersApplies when element has focus (clicked or tabbed to)
:focus-visible95%+ supportApplies when element has focus AND user is using keyboard navigation
:focus-within95%+ supportApplies when element OR any of its descendants has focus
Link States
Pseudo-classes for styling anchor elements
:linkAll browsersUnvisited links (anchor with href)
:visitedAll browsersLinks the user has already visited
:any-link95%+ supportMatches any anchor with href (visited or not)
Form States
Pseudo-classes for form element states and validation
:checkedAll browsersApplies to checked checkboxes, radios, or selected options
:indeterminateAll browsersCheckbox in indeterminate state (neither checked nor unchecked)
:disabledAll browsersDisabled form elements
:enabledAll browsersEnabled form elements (default state)
:requiredAll browsersForm elements with required attribute
:optionalAll browsersForm elements without required attribute
:validAll browsersForm elements with valid input
:invalidAll browsersForm elements with invalid input
:in-rangeAll browsersInput with value within min/max range
:out-of-rangeAll browsersInput with value outside min/max range
:placeholder-shown95%+ supportInput currently showing placeholder text
:defaultAll browsersDefault form element in a group (default button, initially selected option)
:read-onlyAll browsersElements with readonly attribute
:read-writeAll browsersEditable elements (inputs without readonly)
Structural (Tree)
Pseudo-classes based on element position in the DOM tree
:rootAll browsersThe document root element (usually <html>)
:emptyAll browsersElements with no children (including text nodes)
:first-childAll browsersFirst child of its parent
:last-childAll browsersLast child of its parent
:only-childAll browsersElement that is the only child of its parent
:first-of-typeAll browsersFirst element of its type among siblings
:last-of-typeAll browsersLast element of its type among siblings
:only-of-typeAll browsersOnly element of its type among siblings
:nth-child(n)All browsersElement based on position among siblings (1-indexed)
:nth-last-child(n)All browsersLike :nth-child but counting from the end
:nth-of-type(n)All browsersElement based on position among same-type siblings
:nth-last-of-type(n)All browsersLike :nth-of-type but counting from the end
Logical
Pseudo-classes for complex selector logic
:not()All browsersMatches elements that do NOT match the selector
:is()95%+ supportMatches any of the given selectors (forgiving - ignores invalid selectors)
:where()95%+ supportLike :is() but with zero specificity
:has()Modern browsersParent selector - matches elements containing the specified selector (game-changer!)
Other
Additional useful pseudo-classes
:targetAll browsersElement whose ID matches the URL fragment (e.g., #section1)
:scope95%+ supportReference element for selectors (usually :root in stylesheets)
:lang()All browsersElements with specific language attribute
:dir()Modern browsersElements with specific text direction (ltr or rtl)
:nth-child() Formula Reference
The :nth-child() formula is An+B where:
- A = cycle size (how often to repeat)
- n = counter starting from 0
- B = offset (where to start)
| Formula | Description | Matches |
|---|---|---|
:nth-child(odd) | Odd elements (1st, 3rd, 5th...) | 1, 3, 5, 7, 9 |
:nth-child(even) | Even elements (2nd, 4th, 6th...) | 2, 4, 6, 8, 10 |
:nth-child(2n) | Every 2nd element (same as even) | 2, 4, 6, 8, 10 |
:nth-child(2n+1) | Every 2nd, starting from 1st (same as odd) | 1, 3, 5, 7, 9 |
:nth-child(3n) | Every 3rd element | 3, 6, 9 |
:nth-child(3n+1) | Every 3rd, starting from 1st | 1, 4, 7, 10 |
:nth-child(n+4) | 4th element and beyond | 4, 5, 6, 7, 8, 9, 10 |
:nth-child(-n+3) | First 3 elements only | 1, 2, 3 |
:nth-child(5) | Only the 5th element | 5 |
Common Patterns & Use Cases
Zebra Striping
tr:nth-child(even) {
background: #f5f5f5;
}Remove Last Border
li:not(:last-child) {
border-bottom: 1px solid #ddd;
}Form Validation Styles
input:valid { border-color: green; }
input:invalid { border-color: red; }
input:focus:invalid { outline: red; }Accessible Focus Styles
button:focus-visible {
outline: 2px solid blue;
outline-offset: 2px;
}Style All Headings
:is(h1, h2, h3, h4) {
color: navy;
font-weight: bold;
}Conditional Parent Styling
/* Style card if it has an image */
.card:has(img) {
display: grid;
grid-template-columns: 200px 1fr;
}Browser Support Legend
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