Sub topic 12
Designing for touch, hover, and pointer differences
This chapter assumes you know media queries and mobile-first CSS from earlier chapters. It covers a problem that width cannot solve. A 1024px-wide tablet and a 1024px-wide laptop get the same layout from width queries, but one is used with a finger and the other with a mouse.
Why width is not enough
Screen width tells you how much room you have. It does not tell you how the person points at things or whether they can hover. A large touch screen has a wide viewport and no hover. A small laptop window has a narrow viewport and a precise mouse.
CSS gives you media features that describe the input device directly:
| Feature | Question it answers | Common values |
|---|---|---|
| `hover` | Can the primary input hover over elements? | `hover`, `none` |
| `pointer` | How precise is the primary input? | `fine` (mouse, stylus), `coarse` (finger), `none` |
| `any-hover` | Can any available input hover? | `hover`, `none` |
| `any-pointer` | Is any available input fine or coarse? | `fine`, `coarse`, `none` |
The hover and pointer features look only at the primary input. The any- versions look at all inputs. A laptop with a touch screen reports a fine, hovering primary pointer but any-pointer: coarse as well.
Hover styles that only apply where hover exists
On a touch screen, a tap often triggers the :hover state and leaves it stuck until the user taps elsewhere. A menu item can look highlighted long after it was touched. Wrap hover styling in a query so touch devices never see it:
@media (hover: hover) and (pointer: fine) {
.menu-item:hover {
background: #eef;
}
}Keep the base style and the :focus-visible style outside the query. Keyboard users need a visible focus state whatever the device.
Bigger targets for coarse pointers
A fingertip is much less precise than a mouse cursor. Common guidance is a target of at least 44 by 44 CSS pixels. WCAG 2.2 sets a minimum of 24 by 24 CSS pixels (success criterion 2.5.8) and treats 44 as the more comfortable size. Enlarge targets only when the pointer is coarse, so dense desktop layouts stay compact:
.menu-item {
padding: 0.5rem 0.75rem;
}
@media (pointer: coarse) {
.menu-item {
padding: 0.75rem 1rem;
min-height: 2.75rem; /* 44px at a 16px root size */
}
}Spacing matters as well as size. Two small buttons packed together cause mistaken taps even if each one meets the minimum.
Do not hide essential actions behind hover
Hover-only controls fail on touch screens, because there is no way to trigger them. Examples are a tooltip that holds the only explanation of an icon, or a delete button that appears when the cursor is over a row. Treat hover as a bonus layer:
- Make every action reachable by tap and by keyboard.
- Show important controls by default, or reveal them on
:focus-withinas well as:hover. - Use hover to add polish, such as a color change, not to carry meaning.
Common errors
- Using width as a proxy for touch. Assuming "narrow means phone" breaks on large tablets and on touch laptops.
- Using
any-pointerto decide target size. On a touch laptop,any-pointer: coarseis true even when the user is holding a mouse. Preferpointerfor sizing, and keep targets reasonably large for everyone. - Removing focus outlines to make hover and focus look the same.
- Testing only in a desktop browser with a resized window. The window still reports a mouse. Use device emulation in browser tools, then check on a real touch device.
In your design tool, annotate each frame with the input assumption. Mark which hover states exist on the desktop frame and what replaces them on the mobile frame.
Exercise
A page has a row of small icon buttons that show a tooltip on :hover. Users on a touch tablet with a 1100px-wide screen cannot tell what the icons mean. Why does a width query not fix this, and what two changes would you make?
Show answer
A width query only describes available space, and a touch tablet can be as wide as a laptop, so the tooltip layout still applies and still needs hover. Change 1: give the icons visible text labels or an accessible alternative that does not need hover, and keep the tooltip as an extra inside @media (hover: hover) and (pointer: fine). Change 2: enlarge the buttons for coarse pointers with @media (pointer: coarse), for example min-height: 2.75rem and more padding.