Sub topic 13
Adapting to hover and pointer capabilities
This chapter assumes you can write basic @media rules, know the and operator, and understand the cascade. Width queries tell you how much room a page has. They do not tell you how the user will interact with it. A tablet and a small laptop can have the same width, but one is used with a finger and the other with a mouse. Input capability queries cover that difference.
The two features
Two media features describe the primary input device:
hoverreports whether the primary input can hover over elements. Values arehoverandnone.pointerreports how precise the primary input is. Values arefine(mouse, trackpad, stylus),coarse(finger), andnone.
"Primary" means the input the browser considers the main one. Devices can have several inputs, so CSS also offers any-hover and any-pointer. These are true if any available input matches.
| Feature | Matches when | Typical device |
|---|---|---|
| `hover: hover` | Primary input can hover | Desktop with a mouse |
| `hover: none` | Primary input cannot hover | Phone or tablet |
| `pointer: fine` | Primary input is precise | Mouse, trackpad |
| `pointer: coarse` | Primary input is imprecise | Touch screen |
| `any-pointer: coarse` | At least one input is imprecise | Laptop with a touch screen |
Avoiding sticky hover on touch screens
On many touch browsers, a tap triggers :hover and the style stays until the user taps elsewhere. A button can look stuck in its hover state. Wrapping hover styles in a capability query avoids this:
.button {
background: #0d6efd;
color: #fff;
}
@media (hover: hover) and (pointer: fine) {
.button:hover {
background: #0a58ca;
}
}Devices that cannot hover never receive the :hover rule. The base style still works everywhere. Keep anything essential, such as a menu that opens only on hover, out of this pattern, because touch users could not reach it.
Larger targets for coarse pointers
A finger is less precise than a mouse. A common guideline is a touch target of at least 44 by 44 CSS pixels. Use pointer: coarse to enlarge targets only where needed:
.nav a {
display: inline-block;
padding: 0.25rem 0.5rem;
}
@media (pointer: coarse) {
.nav a {
padding: 0.75rem 1rem;
min-height: 44px;
}
}Common errors
- Using width to guess touch. A narrow window on a desktop is still a mouse. A wide tablet is still touch. Test the input, not the screen size.
- Trusting the primary input alone. A hybrid laptop reports a fine, hovering primary input but also has a touch screen. Add
any-pointer: coarserules if touch users on such devices need larger targets. - Hiding functionality behind hover. Hover-only content is unreachable without a hover-capable input. Keyboard users also need
:focus-visiblestyles. - Writing hover rules without a fallback. Put the default look outside the query, and let the query add enhancements.
Exercise
A laptop with a touch screen reports pointer: fine as its primary input. Which query would let you enlarge buttons for users who might also touch the screen, and why is pointer: coarse not enough?
Show answer
Use @media (any-pointer: coarse). The pointer feature checks only the primary input, which is the trackpad on this laptop, so pointer: coarse does not match. any-pointer matches if any available input is coarse, including the touch screen.