Codectionary / Developer documentation / CSS

Interactive Pseudo-classes

These pseudo-classes style elements based on user interaction state. :hover applies while the pointer is over an element, :focus while it has keyboard focus, :active during the moment of being clicked/pressed, and :visited for links the user has already navigated to.

Syntax

:hover, :focus, :active, :visited

Examples

The Interaction State Order

A button styled across its different interactive states.

.button {
  background: #3b82f6;
  transition: background 0.15s ease;
}

.button:hover {
  background: #2563eb;
}

.button:focus {
  outline: 2px solid #93c5fd;
  outline-offset: 2px;
}

.button:active {
  background: #1d4ed8;
}

Styling Visited Links

A subtle way to indicate previously-visited pages.

a:visited {
  color: #6b21a8; /* a slightly muted purple, distinct from the unvisited link color */
}

Parameters and values

  • :hover (pseudo-class): Applies while the pointer is over the element
  • :focus (pseudo-class): Applies while the element has keyboard/programmatic focus
  • :active (pseudo-class): Applies during the moment of being clicked or pressed
  • :visited (pseudo-class): Applies to links the user has already navigated to

Best practices

  • Never remove focus outlines (outline: none) without providing a clearly visible custom alternative - it is essential for keyboard navigation accessibility
  • Style hover states as a nice-to-have enhancement, not a requirement - hover has no equivalent on touchscreens
  • Follow the LVHA order (:link, :visited, :hover, :active) when declaring these together, since later rules override earlier ones at equal specificity
  • Use :focus-visible instead of :focus alone in modern projects, so focus rings only show for keyboard users, not every mouse click

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation