Sub topic 12
Designing for touch, hover, and pointer differences
This chapter assumes you can write media queries, use mobile-first min-width CSS, and read the breakpoint frames from earlier chapters. Width tells you how much room a layout has. It does not tell you how people interact with the page. A 1024px-wide tablet is touched with a finger, and a 1024px-wide laptop is used with a mouse. A breakpoint on width alone treats them the same, so you need a second set of checks for input.
Why width is the wrong signal for input
It is tempting to treat "mobile" as "touch" and "desktop" as "mouse". That assumption fails in several common cases:
- Tablets are wide but use touch.
- Laptops with touchscreens support both touch and a trackpad.
- Phones can be connected to a mouse or keyboard.
- Desktop browsers can be resized to a narrow window, and that does not add touch.
So design two questions separately. Width decides how many columns you have. Input capability decides how big the controls are and whether hover can carry meaning.
The input media features
CSS exposes the input hardware through four media features. They are part of Media Queries Level 4 and are supported in current browsers.
| Feature | Values | Describes |
|---|---|---|
| `pointer` | `none`, `coarse`, `fine` | Accuracy of the primary input |
| `hover` | `none`, `hover` | Whether the primary input can hover |
| `any-pointer` | `none`, `coarse`, `fine` | Accuracy of all available inputs |
| `any-hover` | `none`, `hover` | Whether any available input can hover |
A finger is coarse. A mouse, trackpad, or stylus is fine. A touchscreen alone gives pointer: coarse and hover: none. A mouse gives pointer: fine and hover: hover.
The plain pointer and hover features describe only the primary input, which the browser chooses. On a hybrid device the primary input may be the touchscreen even though a trackpad is attached. The any- versions are true if at least one input matches.
Hover as an enhancement
Touch screens have no hover. Browsers emulate it: after a tap, the element may stay in its :hover state until the user taps somewhere else. This is called sticky hover. A button can look permanently highlighted after one tap.
The fix is to wrap hover styles in a query that only matches devices that can hover:
@media (hover: hover) and (pointer: fine) {
.menu-item:hover {
background: #eef;
}
}On a touch-only phone this rule never applies, so no sticky highlight appears. The :hover selector inside the query still works for mouse users.
Follow one rule: hover can add polish, but it must never be the only way to reach something. Information that appears only on hover is invisible to touch users, and a dropdown that opens only on hover cannot be opened by tap. Keyboard users have the same problem. Pair every :hover rule with :focus-visible so keyboard users see the same state.
.menu-item:focus-visible {
outline: 3px solid #1a56db;
outline-offset: 2px;
}
@media (hover: hover) and (pointer: fine) {
.menu-item:hover {
background: #eef;
}
}Use :active for press feedback. It works for touch, mouse, and keyboard activation, and it does not need a media query.
Target size for touch
A fingertip covers roughly 10mm, far more than a mouse cursor. Small targets cause missed taps and accidental taps on neighbours. Published guidance gives these minimums:
| Source | Minimum target size |
|---|---|
| WCAG 2.2 criterion 2.5.8 (Level AA) | 24 by 24 CSS pixels, with spacing exceptions |
| WCAG 2.1 criterion 2.5.5 (Level AAA) | 44 by 44 CSS pixels |
| Apple Human Interface Guidelines | 44 by 44 points |
| Material Design | 48 by 48 dp |
Treat 44 to 48 CSS pixels as the working target for touch, and use 24 as the absolute floor. Remember that CSS pixels are not device pixels, as the viewport chapter explained, so a 44px button is a comfortable size on any phone.
You do not need to make everything large for mouse users. You can enlarge targets only when the primary pointer is coarse:
.icon-button {
min-width: 2rem;
min-height: 2rem;
}
@media (pointer: coarse) {
.icon-button {
min-width: 2.75rem;
min-height: 2.75rem;
}
}With a 16px root size, 2.75rem is 44px. Using rem also means the target grows when users raise their default font size.
If a larger visible button does not fit the design, enlarge only the hit area. A pseudo-element extends the clickable region without changing the visual size:
.icon-button {
position: relative;
}
.icon-button::after {
content: "";
position: absolute;
inset: -0.5rem;
}Check that enlarged hit areas of neighbouring controls do not overlap. Overlap makes taps ambiguous and defeats the purpose.
Spacing and placement
Size is only part of the problem. Consider these points when you design the touch frames:
- Leave at least 8px between adjacent targets, so a slightly off tap hits the gap and not the wrong control.
- Put frequent actions within thumb reach. On large phones, the top corners are the hardest to reach one-handed. A bottom navigation bar or a floating action button is easier to use than a control at the top.
- Do not place destructive actions right next to common ones.
- Keep inline text links in paragraphs readable. WCAG exempts links inside sentences from the size minimum, but lists of links should have generous line spacing.
Hover alternatives
Common hover patterns need a touch-friendly counterpart.
| Hover pattern | Problem on touch | Alternative |
|---|---|---|
| Tooltip with essential information | Never appears | Show the text inline, or use a tap-to-open disclosure |
| Dropdown menu opened on hover | Cannot be opened | Open on click or tap, and close on outside tap or Escape |
| Actions revealed on row hover | Hidden permanently | Always show the actions, or use a visible "more" button |
| Image zoom on hover | Not available | Allow pinch zoom, or open a larger view on tap |
For the third pattern, you can keep the hover reveal for mouse users and show the actions by default everywhere else:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Row actions</title>
<style>
.row { display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 1rem; }
.row-actions button { min-width: 2.75rem; min-height: 2.75rem; }
@media (hover: hover) and (pointer: fine) {
.row-actions { opacity: 0; }
.row:hover .row-actions,
.row:focus-within .row-actions { opacity: 1; }
.row-actions button { min-width: 2rem; min-height: 2rem; }
}
</style>
</head>
<body>
<div class="row">
<span>Quarterly report.pdf</span>
<span class="row-actions">
<button type="button">Edit</button>
<button type="button">Delete</button>
</span>
</div>
</body>
</html>The default styles are the touch-safe version, with visible, large buttons. The hover query turns on the reveal behaviour only for a fine pointer that can hover. The :focus-within rule keeps the buttons reachable for keyboard users, because opacity: 0 does not remove them from the tab order.
Related gotchas
- Hybrid devices. If you need to show a touch-friendly control whenever a touch input exists, use
any-pointer: coarse. If you usepointer: fineas the only check, a touch laptop user may be treated as mouse-only. - Queries combine with width. Input and width queries are independent, so you can combine them:
@media (min-width: 48em) and (pointer: coarse)targets tablets with touch. Use this sparingly, because every combination adds a state to test. - Do not detect devices. Reading the user agent string to find "mobile" breaks on tablets, desktop-mode browsers, and new devices. Capability queries are more reliable.
- Gestures need alternatives. Swipe-to-delete, drag-and-drop, and pinch gestures are hard for some users and impossible with a keyboard. Always provide a single-pointer, visible alternative such as a button.
- Text selection and zoom. Do not disable pinch zoom with
user-scalable=noormaximum-scale=1in the viewport tag. It blocks users who need to zoom and fails WCAG 1.4.4. - Form fields. On iOS Safari, an input with a font size under 16px triggers an automatic zoom when focused. Set form control text to at least
1rem.
Summary of the approach
- Write the base styles for touch: large targets, no hover dependency, visible actions.
- Add
:focus-visiblestates for keyboard users. - Wrap hover effects in
@media (hover: hover) and (pointer: fine). - Use
pointer: coarseorany-pointer: coarseto enlarge targets where the base size is smaller. - Check the result on a real touch device and with a mouse, at widths between your breakpoints.
Exercise
A touch laptop reports a trackpad as its primary input, so hover: hover and pointer: fine match. The user also taps the screen. Your CSS makes .icon-button 44px only inside @media (pointer: coarse). What goes wrong, and how do you fix it?
Show answer
Because the primary pointer is fine, the coarse rule never applies, so the buttons stay at their small size and the user's finger taps miss. Fix it by using any-pointer: coarse for the enlarged size, because it matches when any available input is coarse. Alternatively, make the 44px size the base style and shrink only inside the pointer: fine query.
Exercise
A product list shows a "Quick add" button only when a row is hovered. Describe a version that works for mouse, touch, and keyboard users.
Show answer
Make the button visible and at least 44px by default, which serves touch users. Inside @media (hover: hover) and (pointer: fine), hide it with opacity: 0 and show it on .row:hover and .row:focus-within. Because opacity keeps the button in the tab order, keyboard users can reach it, and :focus-within makes it appear when they do.