CSS

Lesson 07

State selectors and focus-visible

Style hover, active, selected, and keyboard focus states as separate reviewable behavior.

Good Code

tabs.css
.tab-button {
  /* Hover, focus, and selected state each get a distinct visual signal. */
  color: #334155;
  border-bottom: 2px solid transparent;
}

.tab-button:hover {
  color: #0f172a;
}

.tab-button:focus-visible {
  outline: 3px solid #f59e0b;
  outline-offset: 3px;
}

.tab-button[aria-selected="true"] {
  color: #1d4ed8;
  border-bottom-color: currentColor;
}

Bad Code

tabs.css
.tab-button {
  /* Removing outline without a replacement hides keyboard position. */
  outline: none;
}

.tab-button:hover,
.tab-button:focus {
  color: #1d4ed8;
}

.tab-button.active {
  border-bottom: 2px solid #1d4ed8;
}

Review Notes

What to review

Good Code

The good version keeps mouse hover, keyboard focus, and selected state distinct, so each user interaction has a visible and meaningful style.

Bad Code

The bad version removes the native outline and blends focus with hover, making keyboard navigation harder to detect and tying selected state to a visual-only class.

Takeaways

  • Use :focus-visible for keyboard focus and avoid removing outlines without a visible replacement.