Course contentsShow
← Web Development
Lesson 125 of 2,9823. CSS FundamentalsFree lesson

Pseudo-Classes: :hover, :focus, :active

Style interactive states of elements using the most common user action pseudo-classes.

Pseudo-Classes: :hover, :focus, :active

You'll learn how to style elements differently when users interact with them, making your site feel more responsive and alive.

What Are Interactive Pseudo-Classes?

Pseudo-classes are special selectors that target elements in specific states. While the combinators you just learned select elements based on their location, these pseudo-classes select based on user interaction.

Think of them like mood rings for HTML elements — they change appearance based on what's happening to them.

The Three Most Common Interactive States

:hover — Triggers when the user's mouse cursor is over the element (hovering). Perfect for showing that something is clickable.

button:hover {
  background-color: lightblue;
}

:focus — Activates when the element receives keyboard focus (usually by pressing Tab) or is clicked. Essential for accessibility and form inputs.

input:focus {
  border: 2px solid blue;
}

:active — Applies the moment the user presses down on the element, before releasing. It's the "I'm clicking right now!" state.

a:active {
  color: red;
}

Practical Example

.button {
  background-color: gray;
  color: white;
}

.button:hover {
  background-color: darkgray; /* Mouse over */
}

.button:focus {
  outline: 3px solid orange; /* Keyboard navigation */
}

.button:active {
  background-color: black; /* While clicking */
}

These states can overlap! An element can be :focus and :hover simultaneously.

Key Takeaway: Use :hover, :focus, and :active pseudo-classes to provide visual feedback during user interactions, making interfaces feel responsive and improving accessibility for both mouse and keyboard users.