Sub topic 3
Reacting to the user: :hover, :active, :focus, :focus-visible, :focus-within
This chapter assumes you know CSS rule syntax, type, class, and attribute selectors, and the descendant and child combinators. It also assumes you know that a pseudo-class is written with one colon and selects elements by state.
The working model
Browsers track what the user is doing and attach temporary states to elements. A pseudo-class matches an element only while its state is true. When the state ends, the rule stops applying and the element returns to its normal style.
| Pseudo-class | Matches while |
|---|---|
| `:hover` | A pointer is over the element |
| `:active` | The element is being pressed, such as a mouse button held down |
| `:focus` | The element has keyboard focus |
| `:focus-visible` | The element has focus and the browser decides a focus indicator is needed |
| `:focus-within` | The element or any descendant has focus |
Hover and active
:hover gives feedback that an element is interactive. :active gives feedback for the moment of the press.
.btn {
background: #1a73e8;
color: #fff;
transition: background 0.15s;
}
.btn:hover {
background: #1557b0;
}
.btn:active {
background: #0f3d7a;
}Touch screens have no true hover. Many mobile browsers apply :hover after a tap and keep it until the user taps elsewhere. Do not hide essential information behind hover only.
Focus, and why :focus-visible exists
An element has focus when it will receive keyboard input. Links, buttons, and form controls get focus from the Tab key or a click.
Browsers draw a default outline on focused elements. Developers often removed it with outline: none because it appeared after mouse clicks on buttons. That broke keyboard navigation. :focus-visible solves the problem. The browser shows the indicator for keyboard use, and usually not for a mouse click on a button. Text inputs show it for any focus, because the user needs to know where typing will go.
button:focus-visible {
outline: 3px solid #1a73e8;
outline-offset: 2px;
}
.form-group:focus-within {
background: #f0f6ff;
}:focus-within matches a container when any descendant has focus. Here the whole .form-group is highlighted while the user types in an input inside it. It is a way to style a parent from a child's state.
Common errors
- Removing the outline with no replacement.
a:focus { outline: none; }leaves keyboard users unable to see where they are. If you change the outline, provide a clear alternative with enough contrast. - Styling only
:hover. Keyboard users never trigger hover. Pair hover rules with a focus rule, for example.btn:hover, .btn:focus-visible { ... }. - Putting
:activebefore:hover. With equal specificity, the later rule wins. The press color would be hidden while the pointer is also over the element. The next chapter covers the full ordering for links. - Using
:focuson a container..form-group:focusnever matches, because adivis not focusable by default. Use:focus-within. - Relying on hover for touch users. Menus that open only on hover can be unreachable on phones.
Exercise
A card contains a link. You want the whole card to get a blue border when a keyboard user tabs to the link inside it. Which pseudo-class do you put on .card, and why does .card:focus not work?
Show answer
Use .card:focus-within. .card:focus matches only when the card itself has focus, and a div is not focusable by default. :focus-within matches the card whenever it or any descendant, such as the link, has focus.
Exercise
A button has button:focus { outline: none; } and a button:hover background change. What problem does a keyboard user face, and what rule fixes it?
Show answer
Keyboard users get no visible indicator of which button has focus, and they never trigger hover. Replace the outline removal with button:focus-visible { outline: 3px solid #1a73e8; outline-offset: 2px; }.