CSS Advanced Features

CSS Pseudo-classes

CSS Pseudo-classes are keywords added to selectors that specify a special state of selected elements, such as :hover, :focus, :nth-child(), enabling styling based on user interaction or document structure.

What are pseudo-classes in CSS?

A pseudo-class selects an element in a particular state or position, written with a single colon. Examples are :hover, :focus-visible, :checked, :disabled, :first-child, :nth-child(2n) and :not(.active). They let you style interaction and structure without adding classes in JavaScript.

a:hover            { text-decoration: underline; }
button:focus-visible { outline: 2px solid blue; }
li:nth-child(odd)  { background: #f5f5f5; }
input:invalid      { border-color: red; }
.card:not(.featured) { opacity: .8; }

Interactive Pseudo-classes Demo

Code Editor

<ul>
  <li>First</li><li>Second</li><li>Third</li><li>Fourth</li>
</ul>
<input placeholder="Required field" required>
<button>Hover or Tab to me</button>
<p><a href="#">Link</a></p>

Preview

Interactive Pseudo-classes

User Interaction States

/* Hover state - when mouse is over element */
.button:hover {
  background-color: #2563eb;
  transform: translateY(-2px);
}

/* Focus state - when element receives keyboard focus */
.button:focus {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

/* Active state - when element is being clicked */
.button:active {
  transform: translateY(0);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.1);
}

These pseudo-classes respond to user interactions without JavaScript.

Link States

/* Link states in order */
a:link {
  color: #3b82f6; /* Unvisited */
}

a:visited {
  color: #7c3aed; /* Visited */
}

a:hover {
  color: #1d4ed8; /* Hover */
  text-decoration: underline;
}

a:active {
  color: #1e40af; /* Active */
}

Remember LVHA order for proper cascading

Focus Management

/* Focus visible (keyboard navigation) */
.button:focus-visible {
  outline: 2px solid #3b82f6;
  outline-offset: 2px;
}

/* Focus within (child has focus) */
.form-group:focus-within {
  box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}

/* Hide outline for mouse users */
.button:focus:not(:focus-visible) {
  outline: none;
}

Enhanced focus handling for accessibility

Structural Pseudo-classes

Child-based Selection

/* First and last children */
li:first-child {
  border-top: 3px solid #3b82f6;
}

li:last-child {
  border-bottom: 3px solid #ef4444;
}

/* Nth child patterns */
tr:nth-child(even) {
  background-color: #f9fafb; /* Zebra stripes */
}

.card:nth-child(3n+1) {
  margin-left: 0; /* Every 3rd item */
}

p:nth-last-child(2) {
  font-weight: bold; /* 2nd from end */
}

Target elements based on their position among siblings.

Type-specific Selection

/* First/last of element type */
h2:first-of-type {
  margin-top: 0;
}

p:last-of-type {
  margin-bottom: 0;
}

/* Nth of type */
img:nth-of-type(odd) {
  float: left;
}

img:nth-of-type(even) {
  float: right;
}

Select based on element type, not just position

Only Child Selection

/* Single child */
.section p:only-child {
  text-align: center;
  font-size: 1.2em;
}

/* Only of its type */
.article img:only-of-type {
  display: block;
  margin: 2rem auto;
  max-width: 100%;
}

/* Empty elements */
.placeholder:empty {
  display: none;
}

Handle single or empty elements differently

Form State Pseudo-classes

Input Validation States

/* Valid input styling */
input:valid {
  border-color: #10b981;
  background-image: url('checkmark-icon.svg');
}

/* Invalid input styling */
input:invalid {
  border-color: #ef4444;
}

/* Only show invalid state after user interaction */
input:invalid:not(:placeholder-shown) {
  border-color: #ef4444;
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
}

/* Required field indicator */
input:required {
  border-left: 3px solid #f59e0b;
}

Provide instant visual feedback for form validation.

Input States

/* Disabled inputs */
input:disabled {
  background-color: #f3f4f6;
  color: #9ca3af;
  cursor: not-allowed;
}

/* Read-only inputs */
input:read-only {
  background-color: #f9fafb;
  border-style: dashed;
}

/* Placeholder shown */
input:placeholder-shown {
  font-style: italic;
}

Style different input states appropriately

Checkbox & Radio States

/* Checked state */
input[type="checkbox"]:checked + label {
  background-color: #dbeafe;
  border-color: #3b82f6;
}

/* Indeterminate state */
input:indeterminate + label {
  background: linear-gradient(
    45deg, #f3f4f6 25%, #3b82f6 25%
  );
}

/* In range/out of range */
input:out-of-range {
  border-color: #ef4444;
}

Handle various form control states

Advanced Pseudo-classes

Logical Pseudo-classes

/* NOT selector - exclude elements */
.button:not(.disabled):not(.loading) {
  cursor: pointer;
}

/* IS selector - match any selector in list */
.element:is(.active, .highlighted, .selected) {
  background-color: #dbeafe;
}

/* WHERE selector - same as :is but lower specificity */
.card :where(h1, h2, h3) {
  margin-top: 0;
}

/* HAS selector - parent has specific child */
.article:has(img) {
  display: grid;
  grid-template-columns: 1fr 200px;
}

Powerful logical selectors for complex styling conditions.

Target and Root

/* Target pseudo-class for anchor links */
.section:target {
  background-color: #fef3c7;
  border: 2px solid #f59e0b;
  animation: highlight 0.5s ease;
}

/* Root pseudo-class */
:root {
  --primary-color: #3b82f6;
  --font-size-base: 16px;
}

/* Any-link matches all links */
:any-link {
  text-decoration: none;
  border-bottom: 1px solid currentColor;
}

Special pseudo-classes for document structure and navigation.

Best Practices & Common Patterns

Best Practices

  • • Follow LVHA order for link states
  • • Use :focus-visible for better accessibility
  • • Combine pseudo-classes for specific targeting
  • • Use :not() to reduce CSS repetition
  • • Test pseudo-classes across different browsers
  • • Consider performance with complex selectors

Creative Uses

  • • Create CSS-only modals with :target
  • • Build toggles with :checked
  • • Style form validation without JavaScript
  • • Create zebra stripes with :nth-child
  • • Use :has() for parent selection
  • • Build responsive components with :is()

CSS Pseudo-classes — step by step

1 / 4
1

Interaction Pseudo-classes

Use :hover, :focus, :active, and :visited to style elements based on user interaction — the foundation of CSS-driven interactivity.

1 of 4
🔍:where() Has Zero Specificity
Unlike :is() which takes the highest specificity of its argument list, :where() always has 0 specificity — making it ideal for utility resets that you want to be easy to override.

🎯 Quick Check

Which pseudo-class selects the last element among siblings of its type?

CSS Pseudo-classes: summary

Key points

  • Style links in LVHA order: :link, :visited, :hover, :active, so the right state wins.
  • Use :focus-visible instead of :focus to show focus rings for keyboard users without showing them on mouse clicks.
  • :nth-child(3n+1) matches the 1st, 4th, 7th… children. :nth-of-type counts only elements of the same tag.
  • :is(), :where(), :not() and :has() take selector lists as arguments.

Common mistakes

  • Removing focus outlines (outline: none) without a replacement, which breaks keyboard navigation.
  • Using :first-child when the element isn't actually the first child; :first-of-type may be what you need.

Frequently asked questions

›What is the difference between a pseudo-class and a pseudo-element?

A pseudo-class (single colon, :hover) selects an existing element in a certain state. A pseudo-element (double colon, ::before) styles a part of an element or inserts generated content.

›What is the difference between nth-child and nth-of-type?

:nth-child(2) matches the element only if it's the 2nd child of any type. :nth-of-type(2) matches the 2nd element of its own tag type among its siblings.