Course contentsShow
← Web Development
Lesson 2584 of 2,98246. Animation LibrariesFree lesson

Simple State Changes with Transitions

Implement hover, focus, and toggle animations using only CSS transitions for optimal performance.

Simple State Changes with Transitions

What you'll learn: How to implement performant hover, focus, and toggle animations using only CSS transitions for common UI state changes.

The Core Idea

CSS transitions are your first tool for animating state changes in web applications. Before reaching for JavaScript animation libraries, transitions handle most interactive UI needs—hover effects, focus indicators, button clicks, dropdown reveals—with minimal code and maximum browser optimization.

Think of transitions like automatic smooth camera movements. You tell the browser "I'm changing this element's color from blue to red," and the transition property says "film that change over half a second with an ease curve." The browser handles all the frame-by-frame rendering natively.

When Transitions Shine

Hover states are perfect transition candidates:

.button {
  background: #007bff;
  transition: background 0.3s ease;
}

.button:hover {
  background: #0056b3;
}

Focus indicators provide accessible feedback:

.input {
  border: 2px solid #ddd;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.input:focus {
  border-color: #007bff;
  box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
}

Toggle states like dropdowns or accordions:

.dropdown-content {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.4s ease-out;
}

.dropdown.open .dropdown-content {
  max-height: 500px;
}

Why Choose Transitions

Browsers optimize transitions on the GPU when animating transform and opacity. They're declarative—no JavaScript event listeners eating memory. For simple A-to-B state changes triggered by user interaction, transitions are faster to write, easier to maintain, and more performant than any library.

Key Takeaway: Use CSS transitions for hover, focus, and toggle animations—they're performant, declarative, and handle 80% of UI state changes without JavaScript overhead.