CSS Pseudo-Classes
Master CSS pseudo-classes to style elements based on their state, position, and user interaction for dynamic styling.
Pseudo-classes select elements based on their state or position. They're essential for creating interactive, dynamic interfaces.
Common Pseudo-Classes
Link States
a:link {
color: #007bff; /* Unvisited */
}
a:visited {
color: #6610f2; /* Visited */
}
a:hover {
color: #0056b3; /* Mouse over */
}
a:active {
color: #ff3300; /* Being clicked */
}Focus
input:focus {
outline: 2px solid #007bff;
border-color: #007bff;
}
button:focus-visible {
outline: 3px solid #007bff;
outline-offset: 2px;
}
.element:focus-within {
/* When any child has focus */
border-color: #007bff;
}Structural Pseudo-Classes
First and Last
li:first-child {
font-weight: bold;
}
li:last-child {
border-bottom: none;
}
p:first-of-type {
font-size: 1.2em;
}
p:last-of-type {
margin-bottom: 0;
}Nth Child
/* Even rows */
tr:nth-child(even) {
background-color: #f8f9fa;
}
/* Odd rows */
tr:nth-child(odd) {
background-color: white;
}
/* Every third item */
li:nth-child(3n) {
color: #007bff;
}
/* First 3 items */
li:nth-child(-n+3) {
font-weight: bold;
}
/* All but first 3 */
li:nth-child(n+4) {
opacity: 0.7;
}Nth of Type
p:nth-of-type(2) {
color: #007bff;
}
div:nth-of-type(odd) {
background-color: #f8f9fa;
}Only Child
li:only-child {
list-style: none;
}
p:only-of-type {
text-align: center;
}User Action Pseudo-Classes
Hover
.card:hover {
transform: translateY(-4px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}
button:hover {
background-color: #0056b3;
}Active
button:active {
transform: scale(0.95);
}
.link:active {
color: #ff3300;
}Focus
input:focus {
border-color: #007bff;
box-shadow: 0 0 8px rgba(0, 123, 255, 0.3);
}Input Pseudo-Classes
Enabled and Disabled
input:enabled {
background-color: white;
}
input:disabled {
background-color: #e9ecef;
cursor: not-allowed;
opacity: 0.6;
}Checked
input[type="checkbox"]:checked {
background-color: #007bff;
}
input[type="radio"]:checked + label {
font-weight: bold;
color: #007bff;
}Valid and Invalid
input:valid {
border-color: #28a745;
}
input:invalid {
border-color: #dc3545;
}
input:required {
border-left: 3px solid #007bff;
}
input:optional {
border-left: 3px solid #6c757d;
}In Range and Out of Range
input:in-range {
border-color: #28a745;
}
input:out-of-range {
border-color: #dc3545;
}Read Only and Read Write
input:read-only {
background-color: #f8f9fa;
}
input:read-write {
background-color: white;
}Placeholder Shown
input:placeholder-shown {
border-color: #dee2e6;
}
input:not(:placeholder-shown) {
border-color: #007bff;
}Negation Pseudo-Class
/* All paragraphs except first */
p:not(:first-child) {
margin-top: 1em;
}
/* All inputs except checkboxes */
input:not([type="checkbox"]) {
padding: 8px;
}
/* Multiple negations */
a:not(.active):not(.disabled) {
color: #007bff;
}Target Pseudo-Class
/* Highlighted section from URL hash */
section:target {
background-color: #fff3cd;
border-left: 4px solid #ffc107;
}Empty Pseudo-Class
.message:empty {
display: none;
}
div:empty::before {
content: "No content available";
color: #6c757d;
}Root Pseudo-Class
:root {
--primary-color: #007bff;
--secondary-color: #6c757d;
font-size: 16px;
}Language Pseudo-Class
:lang(en) {
quotes: '"' '"';
}
:lang(fr) {
quotes: '« ' ' »';
}Practical Examples
Striped Table
tr:nth-child(even) {
background-color: #f8f9fa;
}
tr:hover {
background-color: #e9ecef;
}Form Validation Styles
input:valid {
border-color: #28a745;
background-image: url('check.svg');
background-repeat: no-repeat;
background-position: right 10px center;
}
input:invalid:not(:focus) {
border-color: #dc3545;
}
input:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 8px rgba(0, 123, 255, 0.3);
}Navigation Active State
.nav-link:hover {
color: #007bff;
}
.nav-link.active,
.nav-link:target {
color: #007bff;
border-bottom: 2px solid #007bff;
}Card Hover Effect
.card {
transition: all 0.3s ease;
}
.card:hover {
transform: translateY(-8px);
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
}
.card:hover .card-title {
color: #007bff;
}Accordion
.accordion-item:target .accordion-content {
max-height: 500px;
}
.accordion-item:not(:target) .accordion-content {
max-height: 0;
overflow: hidden;
}Disabled Button
button:disabled {
background-color: #6c757d;
cursor: not-allowed;
opacity: 0.6;
}
button:not(:disabled):hover {
background-color: #0056b3;
}Combining Pseudo-Classes
/* Hover on enabled buttons only */
button:enabled:hover {
background-color: #0056b3;
}
/* First child that's also hovered */
li:first-child:hover {
font-weight: bold;
}
/* Valid input that has focus */
input:valid:focus {
border-color: #28a745;
}Best Practices
Pseudo-Class Best Practices
- Use
:hoverand:focustogether for accessibility - Prefer
:focus-visibleover:focusfor keyboard users - Use
:not()to avoid repetitive overrides - Style
:disabledto show unavailable actions - Use
:validand:invalidfor form feedback - Remember LVHA order for links (Link, Visited, Hover, Active)
- Test pseudo-classes with keyboard navigation
- Use
:nth-child()for patterns and stripes - Combine pseudo-classes for specific selections
- Consider touch devices (no hover)
Accessibility
/* Ensure focus is visible */
a:focus-visible,
button:focus-visible {
outline: 3px solid #007bff;
outline-offset: 2px;
}
/* Don't remove focus without alternative */
*:focus:not(:focus-visible) {
outline: none;
}
/* Indicate disabled state clearly */
button:disabled {
opacity: 0.6;
cursor: not-allowed;
}Browser Support
Most pseudo-classes have excellent browser support. :focus-visible requires modern browsers. Always test in target browsers.
Master pseudo-classes to create dynamic, interactive, accessible interfaces!
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on
CSS Combinators
Master CSS combinators to create powerful, specific selectors for targeting elements based on their relationships.
CSS !important
Learn when and how to use the !important rule in CSS, its implications, and best practices for maintaining clean code.
© 2026CoderrShyamAll Rights Reserved.