Course contentsShow
← Web Development
Lesson 510 of 2,9829. CSS Transitions and AnimationsFree lesson

Triggering Transitions with State Changes

Using pseudo-classes like :hover, :focus, and :active to initiate transitions.

Triggering Transitions with State Changes

You'll learn how to activate CSS transitions using interactive pseudo-classes that respond to user actions.

The Core Idea

CSS transitions don't happen automatically—they need a trigger. That trigger is a state change in your element. The most common way to create state changes is through pseudo-classes like :hover, :focus, and :active. These pseudo-classes detect user interactions and apply different styles, which—when combined with the transition property—create smooth animations between states.

Think of it like a light switch: the transition is the dimmer effect, but you still need someone to flip the switch. Pseudo-classes are your switches.

How It Works

When you declare a transition on an element's default state, the browser watches for style changes. When a pseudo-class applies different property values, the transition kicks in:

.button {
  background-color: blue;
  transition: background-color 0.3s ease;
}

.button:hover {
  background-color: green;
}

When you hover over the button, it smoothly transitions from blue to green. When you move your mouse away, it transitions back.

Common Trigger Pseudo-Classes

  • :hover - Triggers when the cursor moves over an element
  • :focus - Triggers when an element receives keyboard or click focus (useful for inputs and buttons)
  • :active - Triggers during the moment a user clicks/taps an element
.link {
  color: navy;
  transition: color 0.2s;
}

.link:hover {
  color: cyan;
}

.link:active {
  color: red;
}

Key Takeaway: Transitions require a state change to activate—use pseudo-classes like :hover, :focus, and :active to create interactive style changes that trigger your transitions smoothly.