CSS Pseudo-classes
Target elements based on their state, position, or relationship. Pseudo-classes enable dynamic styling without JavaScript.
💻 Pseudo-class Basics
/* Basic syntax */
selector:pseudo-class {
property: value;
}
/* Examples */
a:hover {
color: red;
}
input:focus {
border-color: blue;
}
li:first-child {
font-weight: bold;
}
🖱️ User Action Pseudo-classes
:hover
/* Mouse over element */
.button:hover {
background: #0056b3;
}
a:hover {
text-decoration: underline;
}
/* Nested hover */
.card:hover .card-title {
color: #007bff;
}
/* Only on hover-capable devices */
@media (hover: hover) {
.button:hover {
background: #0056b3;
}
}
:active
/* Element being clicked */
.button:active {
transform: scale(0.95);
}
a:active {
color: red;
}
/* Touch devices benefit from :active */
:focus
/* Element has focus (keyboard/mouse) */
input:focus {
border-color: #007bff;
outline: none;
box-shadow: 0 0 0 3px rgba(0,123,255,0.25);
}
button:focus {
outline: 2px solid #007bff;
outline-offset: 2px;
}
/* Textarea focus */
textarea:focus {
border-color: #007bff;
}
:focus-visible
/* Focus from keyboard only */
button:focus {
outline: none; /* Remove for all focus */
}
button:focus-visible {
outline: 2px solid #007bff; /* Only keyboard focus */
outline-offset: 2px;
}
/* Better accessibility */
.button:focus-visible {
box-shadow: 0 0 0 3px rgba(0,123,255,0.5);
}
:focus-within
/* Container with focused child */
form:focus-within {
box-shadow: 0 0 10px rgba(0,123,255,0.3);
}
.search-container:focus-within {
border-color: #007bff;
}
/* Highlight parent when input focused */
.form-group:focus-within label {
color: #007bff;
font-weight: 600;
}
🔗 Link Pseudo-classes
/* Unvisited link */
a:link {
color: #007bff;
}
/* Visited link */
a:visited {
color: #6610f2;
}
/* Order matters: LVHA (Link-Visited-Hover-Active) */
a:link { color: #007bff; }
a:visited { color: #6610f2; }
a:hover { color: #0056b3; }
a:active { color: #004085; }
/* Apply to all link states */
a:any-link {
text-decoration: none;
}
📝 Form Pseudo-classes
:enabled & :disabled
/* Enabled input (default) */
input:enabled {
background: white;
}
/* Disabled input */
input:disabled {
background: #e9ecef;
color: #6c757d;
cursor: not-allowed;
}
button:disabled {
opacity: 0.5;
pointer-events: none;
}
:checked
/* Checked checkbox/radio */
input[type="checkbox"]:checked {
background: #007bff;
}
/* Style label when checkbox checked */
input:checked + label {
color: #007bff;
font-weight: bold;
}
/* Toggle switch */
.switch input:checked ~ .slider {
background: #4caf50;
}
/* Radio button checked */
input[type="radio"]:checked {
border-color: #007bff;
}
:indeterminate
/* Checkbox in indeterminate state */
input[type="checkbox"]:indeterminate {
background: #6c757d;
}
/* Progress bar without value */
progress:indeterminate {
animation: progress-indeterminate 1.5s infinite;
}
:valid & :invalid
/* Valid input */
input:valid {
border-color: #28a745;
}
/* Invalid input */
input:invalid {
border-color: #dc3545;
}
/* Only show validation after interaction */
input:not(:placeholder-shown):valid {
border-color: #28a745;
}
input:not(:placeholder-shown):invalid {
border-color: #dc3545;
}
/* Email validation */
input[type="email"]:invalid {
border-color: #dc3545;
}
input[type="email"]:valid {
border-color: #28a745;
}
:required & :optional
/* Required field */
input:required {
border-left: 3px solid #dc3545;
}
/* Optional field */
input:optional {
border-left: 3px solid #6c757d;
}
/* Label for required field */
input:required + label::after {
content: " *";
color: red;
}
:read-only & :read-write
/* Read-only input */
input:read-only {
background: #f8f9fa;
cursor: default;
}
/* Editable input */
input:read-write {
background: white;
}
/* contenteditable elements */
[contenteditable]:read-write {
border: 1px solid #ced4da;
}
:placeholder-shown
/* When placeholder is visible */
input:placeholder-shown {
border-color: #ced4da;
}
/* Hide label when placeholder shown */
input:placeholder-shown + label {
opacity: 0;
}
/* Show label when typing */
input:not(:placeholder-shown) + label {
opacity: 1;
transform: translateY(-20px);
}
:in-range & :out-of-range
/* Number input within range */
input[type="number"]:in-range {
border-color: #28a745;
}
/* Number input outside range */
input[type="number"]:out-of-range {
border-color: #dc3545;
}
🔢 Structural Pseudo-classes
:first-child & :last-child
/* First child */
li:first-child {
font-weight: bold;
}
p:first-child {
margin-top: 0;
}
/* Last child */
li:last-child {
border-bottom: none;
}
p:last-child {
margin-bottom: 0;
}
:first-of-type & :last-of-type
/* First paragraph */
p:first-of-type {
font-size: 1.2em;
}
/* Last image */
img:last-of-type {
margin-bottom: 0;
}
/* Different from :first-child */
/* :first-of-type finds first of element type */
:nth-child()
/* Specific child */
li:nth-child(3) {
color: red; /* 3rd li */
}
/* Even children */
tr:nth-child(even) {
background: #f8f9fa;
}
/* Odd children */
tr:nth-child(odd) {
background: white;
}
/* Every 3rd element */
li:nth-child(3n) {
color: blue;
}
/* Every 3rd starting from 2nd */
li:nth-child(3n+2) {
color: green;
}
/* First 3 elements */
li:nth-child(-n+3) {
font-weight: bold;
}
/* All except first 3 */
li:nth-child(n+4) {
opacity: 0.7;
}
:nth-last-child()
/* Count from end */
li:nth-last-child(2) {
color: red; /* 2nd from end */
}
/* Last 3 items */
li:nth-last-child(-n+3) {
font-weight: bold;
}
:nth-of-type()
/* Every 2nd paragraph */
p:nth-of-type(2n) {
background: #f8f9fa;
}
/* First 3 images */
img:nth-of-type(-n+3) {
border: 2px solid #007bff;
}
:only-child
/* Only child of parent */
li:only-child {
list-style: none;
}
p:only-child {
margin: 0;
}
:only-of-type
/* Only paragraph in container */
p:only-of-type {
text-align: center;
}
/* Only image */
img:only-of-type {
display: block;
margin: 0 auto;
}
:empty
/* Element with no children */
div:empty {
display: none;
}
p:empty::before {
content: "No content available";
color: #6c757d;
}
/* Hide empty table cells */
td:empty {
background: #f8f9fa;
}
🎯 Negation & Logical Pseudo-classes
:not()
/* All except specific selector */
li:not(.active) {
opacity: 0.7;
}
/* Not first child */
p:not(:first-child) {
margin-top: 1em;
}
/* Not disabled */
button:not(:disabled) {
cursor: pointer;
}
/* Multiple not */
input:not([type="submit"]):not([type="button"]) {
width: 100%;
}
/* Complex not */
:not(p):not(h1):not(h2) {
color: #6c757d;
}
:is()
/* Match any of the selectors */
:is(h1, h2, h3) {
line-height: 1.2;
}
/* Same as: */
h1, h2, h3 {
line-height: 1.2;
}
/* More complex */
:is(.card, .panel) :is(h1, h2) {
margin-top: 0;
}
/* Forgiving selector list */
:is(h1, :invalid-selector, h2) {
/* Still works, ignores invalid */
}
:where()
/* Like :is() but with 0 specificity */
:where(h1, h2, h3) {
margin: 0;
}
/* Easy to override */
h2 {
margin-top: 1em; /* This wins */
}
/* Useful for resets */
:where(ul, ol) {
padding-left: 0;
}
:has()
/* Parent selector (relatively new) */
/* Card with image */
.card:has(img) {
display: flex;
}
/* Form with error */
form:has(input:invalid) {
border-color: #dc3545;
}
/* Li with nested ul */
li:has(> ul) {
font-weight: bold;
}
/* Article with h2 */
article:has(h2) {
padding-top: 2em;
}
🎯 Practical Examples
/* Striped table */
tr:nth-child(even) {
background: #f8f9fa;
}
/* Highlight row on hover */
tr:hover {
background: #e9ecef;
}
/* Form validation feedback */
input:focus {
border-color: #007bff;
outline: none;
}
input:valid {
border-color: #28a745;
}
input:invalid:not(:placeholder-shown) {
border-color: #dc3545;
}
/* Navigation active state */
nav a {
color: #6c757d;
transition: color 0.3s;
}
nav a:hover {
color: #007bff;
}
nav a.active {
color: #007bff;
font-weight: bold;
}
/* Card interactions */
.card {
transition: transform 0.3s, box-shadow 0.3s;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
.card:active {
transform: translateY(-2px);
}
/* Custom checkbox */
input[type="checkbox"] {
display: none;
}
input[type="checkbox"] + label {
position: relative;
padding-left: 30px;
cursor: pointer;
}
input[type="checkbox"] + label::before {
content: "";
position: absolute;
left: 0;
width: 20px;
height: 20px;
border: 2px solid #007bff;
border-radius: 3px;
}
input[type="checkbox"]:checked + label::after {
content: "✓";
position: absolute;
left: 4px;
top: 0;
color: #007bff;
}
/* Accordion */
.accordion-item {
border-bottom: 1px solid #dee2e6;
}
.accordion-header {
cursor: pointer;
padding: 1rem;
}
.accordion-header:hover {
background: #f8f9fa;
}
.accordion-content {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s;
}
.accordion-item.active .accordion-content {
max-height: 500px;
}
/* Tabs */
.tab-button {
padding: 0.5rem 1rem;
border: none;
background: transparent;
cursor: pointer;
}
.tab-button:hover {
background: #f8f9fa;
}
.tab-button.active {
border-bottom: 2px solid #007bff;
color: #007bff;
}
/* Dropdown menu */
.dropdown {
position: relative;
}
.dropdown-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
}
.dropdown:hover .dropdown-menu {
display: block;
}
/* Focus styles for accessibility */
button:focus-visible {
outline: 2px solid #007bff;
outline-offset: 2px;
}
a:focus-visible {
outline: 2px dashed #007bff;
outline-offset: 3px;
}
/* Empty state */
.list:empty::before {
content: "No items to display";
display: block;
padding: 2rem;
text-align: center;
color: #6c757d;
}
/* First and last spacing */
.section > *:first-child {
margin-top: 0;
}
.section > *:last-child {
margin-bottom: 0;
}
/* Alternate colors */
.tag:nth-child(4n+1) { background: #007bff; }
.tag:nth-child(4n+2) { background: #28a745; }
.tag:nth-child(4n+3) { background: #ffc107; }
.tag:nth-child(4n+4) { background: #dc3545; }
🎯 Key Takeaways
- :hover/:active/:focus: User interaction states
- :focus-visible: Only keyboard focus (better UX)
- :nth-child(): Select elements by position (even, odd, formulas)
- :not(): Exclude elements from selection
- :checked/:disabled: Form element states
- :valid/:invalid: Form validation styling
- :has(): Parent selector (check if contains element)
- Specificity: Pseudo-classes count as class selectors