Sub topic 10
Designing for touch and pointer capability
This chapter assumes you can write base styles and min-width media queries, as covered in the earlier chapters. It also assumes you know the :hover and :focus pseudo-classes and can open your browser's developer tools. The chapter covers how to size and space interactive elements for fingers, and how to ask the browser what kind of input the device has.
Screen width does not tell you the input type
Earlier chapters used min-width queries to adapt layout to available space. Width describes space. It says nothing about how a person will operate the page.
- A phone has a narrow screen and a touchscreen.
- A tablet has a wide screen and a touchscreen.
- A laptop with a touchscreen has a wide screen, a trackpad and a touchscreen.
- A small desktop browser window is narrow, but the user still has a mouse.
If you enlarge buttons only below 48em, a tablet user gets small targets. If you add hover effects only above 48em, a hybrid laptop user may tap a menu that never opens. Treat layout and input as two separate questions and use a different tool for each.
Making touch targets large enough
A touch target is the area that responds when someone taps. A fingertip covers roughly 8 to 10 mm of screen, and it is imprecise. Small targets cause missed taps and accidental taps on neighbors.
Published guidance gives these minimums:
| Source | Minimum size | Notes |
|---|---|---|
| WCAG 2.2, criterion 2.5.8 (level AA) | 24 by 24 CSS pixels | Smaller targets are allowed if enough spacing surrounds them |
| WCAG 2.2, criterion 2.5.5 (level AAA) | 44 by 44 CSS pixels | The stricter, more comfortable target |
| Apple Human Interface Guidelines | 44 by 44 points | Points are close to CSS pixels in practice |
| Material Design | 48 by 48 dp | Density-independent pixels |
A practical default is 44 CSS pixels, which is 2.75rem when the root font size is 16px. Using rem also lets the target grow when the user raises their default font size.
.button {
min-block-size: 2.75rem;
padding-inline: 1rem;
}min-block-size is the logical version of min-height. It follows the writing direction, so the rule still works in vertical writing modes. Use min- rather than a fixed size so a label that wraps to two lines can make the button taller.
Size is only part of the problem
Two 44px buttons placed edge to edge still invite mis-taps near the border between them. Leave space between adjacent targets, for example with gap in a flex or grid container.
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}Inline links in text
Links inside a paragraph cannot be padded to 44px without disturbing the line height. WCAG 2.5.8 exempts inline links for that reason. Where a list of links is the main content, such as a navigation menu or a footer, you can make each link a block with padding.
.nav a {
display: block;
padding: 0.75rem 1rem;
}Growing the hit area without growing the look
Sometimes a design needs a small icon. You can enlarge the tappable area with a pseudo-element that extends past the visible box.
.icon-button {
position: relative;
inline-size: 1.5rem;
block-size: 1.5rem;
}
.icon-button::after {
content: "";
position: absolute;
inset: -0.625rem;
}The ::after box extends 0.625rem (10px) beyond each side, which produces a 2.75rem hit area around a 1.5rem icon. Check that the extended areas of neighboring icons do not overlap, because overlapping areas defeat the point.
Asking the browser about input capability
CSS has media features that report the input hardware. They let you target what the device can do, not what its screen size suggests.
| Feature | Values | Meaning |
|---|---|---|
| `pointer` | `fine`, `coarse`, `none` | Accuracy of the primary pointing device. Mouse and stylus are `fine`. A finger is `coarse`. |
| `hover` | `hover`, `none` | Whether the primary input can hover. |
| `any-pointer` | `fine`, `coarse`, `none` | Whether any available input has that accuracy. |
| `any-hover` | `hover`, `none` | Whether any available input can hover. |
The any- versions matter on hybrid devices. A touchscreen laptop usually reports its primary input as fine with hover, because the trackpad is primary. But any-pointer: coarse is also true, because a finger is available. This difference drives most decisions in this chapter.
The guideline: use the primary features (hover, pointer) to add refinements for precise input, and use the any- features to make sure every available input is served. If a touch user might exist, do not hide the touch accommodations behind a primary-input check.
Hover styling only where hover exists
On a touchscreen there is no true hover. Browsers emulate it, so a tap can leave :hover styling "stuck" on an element until the user taps elsewhere. Wrapping hover rules in a capability query avoids this.
.button {
min-block-size: 2.75rem;
padding-inline: 1rem;
}
@media (hover: hover) and (pointer: fine) {
.button:hover {
background: #e8eefc;
}
}The base .button rule carries everything needed for touch. The query then adds an enhancement for devices whose primary input can hover precisely. This is the mobile-first pattern applied to input: start with the minimal case and add on.
Enlarging controls for coarse input
You can combine the same idea with spacing. Dense desktop controls can stay compact for a mouse, while coarse input gets room.
.menu a {
padding: 0.5rem 0.75rem;
}
@media (any-pointer: coarse) {
.menu a {
padding: 0.875rem 1rem;
}
}Here the base style is compact and the query enlarges it. That reverses the usual direction of "add, don't undo", but it is acceptable because the condition is "any touch is possible", and you want touch to win on hybrid devices. If you prefer to keep the base touch-friendly, start with the large padding and shrink it inside @media (hover: hover) and (pointer: fine). Choose one direction and stay consistent within a project.
Never put essential content behind hover
Hover is not available to touch users, keyboard users or many assistive technology users. Rules that follow from this:
- Do not reveal important content, such as a submenu or the only label of an icon, with
:hoveralone. - Give every hover effect a matching
:focus-visibleeffect so keyboard users see the same feedback. - Treat hover as a hint. The page must work when the hover never happens.
.card a:hover,
.card a:focus-visible {
text-decoration: underline;
}Selector lists like this one apply the same style for both states. Note that the hover media query is optional here, because underlining on a stuck hover is harmless.
Common errors
- Using
max-width: 768pxas a "touch device" test. A narrow desktop window matches it, and a large tablet does not. Usepointerandhoverinstead. - Trusting
hover: noneto detect phones. Some devices misreport capability, and a connected mouse can change the answer. Design so that either result still gives a usable page. - Hiding touch accommodations behind primary-input checks. On a hybrid laptop,
pointer: coarseis false while a finger is still in use. Useany-pointer: coarse. - Fixing
heighton buttons. A setheight: 2.75remclips wrapped text. Usemin-block-size. - Shrinking the hit area with
overflow: hiddenon a parent. An extended pseudo-element hit area is clipped when an ancestor hides overflow. - Zooming disabled. Adding
user-scalable=noormaximum-scale=1to the viewport tag blocks pinch zoom, which harms users who need it.
Practical checklist
- Give buttons and form controls a
min-block-sizeof about 2.75rem. - Put
gapbetween neighboring targets. - Put hover polish inside
@media (hover: hover) and (pointer: fine). - Use
any-pointer: coarsefor anything touch users need. - Mirror every
:hoverstyle with:focus-visible. - Test with emulation, then on at least one real phone or tablet.
Exercise
A touchscreen laptop uses its trackpad as the primary input. A developer wraps larger button padding in @media (pointer: coarse). A user on this laptop taps the buttons with a finger and finds them cramped. What is wrong, and what is the fix?
Show answer
On this laptop the primary pointer is the trackpad, so pointer reports fine and the pointer: coarse rule never applies, even though the touchscreen is available. Use any-pointer: coarse, which is true whenever any input, including the touchscreen, is coarse. A second option is to keep the larger padding in the base styles and reduce it only inside @media (hover: hover) and (pointer: fine).
Exercise
Write CSS for .tag links so that each has a minimum block size of 2.75rem, there is 0.5rem of space between them in a wrapping flex container called .tags, and a background change on hover applies only when the primary input can hover precisely.
Show answer
.tags { display: flex; flex-wrap: wrap; gap: 0.5rem; } .tag { display: inline-flex; align-items: center; min-block-size: 2.75rem; padding-inline: 1rem; } @media (hover: hover) and (pointer: fine) { .tag:hover { background: #e8eefc; } }