Sub topic 3
Reacting to the user: :hover, :active, :focus, :focus-visible, :focus-within
This chapter covers the pseudo-classes that respond to what a user is doing with the pointer and keyboard. It assumes you can write CSS rules with type, class, and attribute selectors and combine them with descendant and child combinators. It also assumes you know the one-colon syntax for pseudo-classes from the previous chapter.
The five states at a glance
A user action pseudo-class matches an element only while a certain interaction is happening. When the interaction ends, the rule stops matching and the element returns to its other styles. No JavaScript is needed.
| Pseudo-class | Matches while | Typical use |
|---|---|---|
| `:hover` | A pointer rests over the element | Highlight buttons and links |
| `:active` | The element is being pressed | Pressed-button feedback |
| `:focus` | The element has keyboard focus, by any means | Basic focus ring |
| `:focus-visible` | The element has focus and the browser decides a ring is helpful | Keyboard-only focus ring |
| `:focus-within` | The element or any descendant has focus | Highlight a form group |
:hover
:hover matches when a pointing device is over the element. It also matches when the pointer is over a descendant, because the descendant is part of the element's box tree.
.menu-item:hover {
background: #eef3fb;
}Two gotchas matter in practice.
- Touch screens have no true hover. Many mobile browsers apply
:hoverafter a tap and leave it "stuck" until the user taps elsewhere. - Keyboard users never trigger
:hover. Any information or control that appears only on hover is unreachable for them.
To limit hover effects to devices that really hover, wrap them in a media query:
@media (hover: hover) {
.menu-item:hover {
background: #eef3fb;
}
}:active
:active matches while the user is pressing the element. For a mouse, that is from button-down to button-up. For a keyboard, it applies while Space or Enter is held on a button or link.
button:active {
transform: translateY(1px);
}Because it is brief, use :active only for small feedback such as a slight shift or a darker background.
:focus
An element has focus when it is the target of keyboard input. Links, buttons, form controls, and elements with a tabindex attribute can receive focus. Pressing Tab moves focus to the next focusable element.
input:focus {
border-color: #1a73e8;
}:focus matches whenever the element has focus, regardless of how it got there. That includes a mouse click. This is why many designers disliked the default ring. Clicking a button showed an outline that seemed unnecessary to mouse users. The tempting fix is the following rule, and it is one of the most harmful lines of CSS you can write:
/* Do not do this */
button:focus {
outline: none;
}It removes the only visual cue that keyboard users have for where they are on the page. :focus-visible solves the underlying problem properly.
:focus-visible
:focus-visible matches a focused element only when the browser's heuristics say a focus indicator is needed. The heuristics are roughly these:
- After keyboard navigation, such as Tab, the element matches.
- After a mouse click on a button or link, it usually does not match.
- A text input matches even after a click, because the user needs to see where they are typing.
- If a script moves focus right after a keyboard interaction, it matches. After a pointer interaction, it typically does not.
Browsers decide this, so details vary slightly. The element still has focus in every case. Only the rule's match changes.
The topic's example styles a clear ring for those cases:
button:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
}
.form-group:focus-within {
background: #f0f6ff;
}A reliable pattern is to leave the browser default alone, or to replace it only inside :focus-visible. If you must support an old browser that does not know :focus-visible, an unknown pseudo-class invalidates the whole rule. Do not group the old and new selectors in a single comma-separated list. Write them as separate rules.
/* Fallback for old browsers */
button:focus {
outline: 3px solid #1a73e8;
}
/* Modern browsers: remove the ring for mouse clicks only */
button:focus:not(:focus-visible) {
outline: none;
}In an old browser, the third rule is dropped because the selector is invalid, so the ring stays. In a modern one, the ring is hidden only when the browser decides it is not needed.
:focus-within
:focus-within matches an element when the element itself or any of its descendants has focus. It lets a parent react to a child, which CSS could not do before without JavaScript.
<div class="form-group">
<label for="email">Email</label>
<input id="email" type="email">
</div>.form-group:focus-within {
background: #f0f6ff;
}
.form-group:focus-within label {
color: #1a73e8;
}When the input receives focus, the whole group and its label change. This works for any depth of nesting. A common use is a dropdown menu that stays open while any link inside it has focus:
.dropdown .menu {
display: none;
}
.dropdown:hover .menu,
.dropdown:focus-within .menu {
display: block;
}Here the menu opens for pointer users through :hover and for keyboard users through :focus-within. Both selectors share a rule body, so the two input methods get the same behavior.
How the states interact
An element can match several of these pseudo-classes at once. A button under the mouse that is being clicked matches :hover, :active, and often :focus. When rules conflict, the usual cascade applies: higher specificity wins, and for equal specificity the later rule wins. Each pseudo-class adds the same specificity as a class selector, so button:hover and button:focus tie. Source order then decides.
The diagram is simplified. After a mouse click, a button is focused but :focus-visible usually does not match. After a Tab press, it matches :focus-visible and never passes through the hover or pressed states.
Common errors
- Removing outlines without a replacement.
outline: noneon:focusleaves keyboard users lost. Always provide a visible alternative. - Using
:focusfor a ring on every click. Switch to:focus-visibleso mouse users do not see rings on buttons. - Hover-only interfaces. Menus, tooltips, and actions that only appear on
:hoverfail on touch and keyboard. - Low-contrast rings. A focus indicator should contrast with both the element and the background. A light gray ring on white is easy to miss.
- Hiding the ring with
overflow. A parent withoverflow: hiddencan clip an outline that extends outside the child. Use a negativeoutline-offsetor add padding. - Assuming
:focus-withinmatches siblings. It matches only the element and its ancestors-of-the-focused-element, not neighbors.
A complete example
This stylesheet gives a form consistent feedback for pointer and keyboard users:
.form-group {
padding: 0.75rem;
border-radius: 6px;
}
.form-group:focus-within {
background: #f0f6ff;
}
button {
background: #1a73e8;
color: #fff;
border: 0;
padding: 0.5rem 1rem;
border-radius: 4px;
}
@media (hover: hover) {
button:hover {
background: #1558b0;
}
}
button:active {
background: #0f4488;
}
button:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
}Notice the order of the button rules: hover, then active. Because they have equal specificity, the pressed color wins when both match. This is the same idea behind the link-state ordering in the next chapter.
Exercise
A designer asks you to remove the focus ring that appears when a mouse user clicks a button, but keyboard users must still see a ring. Which selector do you use for the ring, and why is button:focus { outline: none; } the wrong approach?
Show answer
Apply the ring in button:focus-visible { outline: 3px solid #1a73e8; outline-offset: 2px; }. The browser matches :focus-visible after keyboard navigation but usually not after a mouse click, so mouse users see no ring and keyboard users do. button:focus { outline: none; } removes the ring for every kind of focus, so keyboard users can no longer tell which element is active.
Exercise
You have <div class="search"><input type="search"></div>. Write a rule that adds a 2px blue border to the .search container whenever the input inside it has focus. Why can't a plain .search:focus do this?
Show answer
Use .search:focus-within { border: 2px solid #1a73e8; }. The div itself never receives focus, because it is not focusable, so .search:focus never matches. :focus-within matches an element when it or any descendant has focus, so it matches the div while the input is focused.