Sub topic 4
Make Links Easy to Click with Padding and display: block
This chapter turns the plain row of links from the previous chapter into a bar with large, reliable click targets. It assumes you can read basic HTML and CSS selectors and know the box model (content, padding, border, margin). It also assumes you already have the nav, ul and li markup, the list reset and the Flexbox row from the earlier chapters.
The problem: a link is only as big as its text
After the list reset and display: flex, the bar looks like a row of words. Each li is a flex item sized to its content, and each a inside it is an inline element, so the clickable area is roughly the size of the letters.
Small targets cause three problems:
- Mouse users must aim precisely.
- Touch users miss and tap the wrong link.
- Users with motor impairments may be unable to hit the target at all.
The fix is to give each link a larger box and make that box part of the layout. Two properties do that: padding for the size and display: block for the layout behavior.
Why padding on an inline element is not enough
An a element is display: inline by default. Inline boxes follow special rules:
- Horizontal padding works as you expect. It pushes neighboring content sideways.
- Vertical padding is painted, and the padded area is clickable, but it does not change the height of the line it sits in.
- The parent
listays as tall as its line of text, and so does the bar. - The link's padding spills out above and below the
liand can overlap whatever is nearby.
You can see the effect in the page below. It contains two bars with the same padding. The second one sets display: block on its links.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Click area demo</title>
<style>
body { font-family: system-ui, sans-serif; margin: 1.5rem; }
.nav-list {
display: flex;
margin: 0 0 3rem;
padding: 0;
list-style: none;
background: #1f2937;
}
.nav-list a {
padding: 0.75rem 1rem;
color: #ffffff;
text-decoration: none;
background: #4b5563;
}
.block-links a {
display: block;
}
</style>
</head>
<body>
<nav aria-label="Inline links">
<ul class="nav-list">
<li><a href="#a">Home</a></li>
<li><a href="#b">Products</a></li>
<li><a href="#c">About</a></li>
</ul>
</nav>
<nav aria-label="Block links">
<ul class="nav-list block-links">
<li><a href="#a">Home</a></li>
<li><a href="#b">Products</a></li>
<li><a href="#c">About</a></li>
</ul>
</nav>
</body>
</html>In the first bar, the gray link backgrounds extend past the dark bar. The bar itself is only one text line tall. In the second bar, the links are block boxes, the li grows to contain them, and the dark bar wraps the padded links.
What display: block changes
A block-level box takes part in layout fully. Its padding, margin and height all contribute to the size of its parent. The final rule for the links is:
.nav-list {
display: flex;
gap: 0.25rem;
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}
.nav-list a {
display: block;
padding: 0.75rem 1rem;
color: #ffffff;
text-decoration: none;
}Following the layout step by step:
.nav-listis a flex container, so eachliis a flex item. Flex items are blockified, which means theliis laid out as a block box.- Each
ais now a block box inside itsli. Its width shrinks to fit its content plus padding, because theliis only as wide as its content. - The
aheight is one line of text plus0.75remof padding above and below. - The
litakes the height of its block child. - The flex container takes the height of its tallest
li.
The whole padded rectangle is now the link. Any click inside it follows the href, and any background you add later (hover, current page) fills the rectangle exactly.
The diagram's "dead zone" branch is the goal of the rest of this chapter. With display: block and padding on the a, the li has almost no area outside its link.
Padding on the link, not on the list item
You could add padding to the li instead. It would enlarge the bar, but the link would stay text-sized, and the padding would be dead space. Always put the padding on the element that owns the href.
The same logic applies to margin. Margin sits outside the border box and is never part of the clickable area. Use margin only when you want a gap that is not clickable. Prefer gap on the flex container for that.
Choosing a size
A target should be comfortable to hit, and there are published minimums:
| Source | Minimum target size |
|---|---|
| WCAG 2.2, success criterion 2.5.8 (level AA) | 24 by 24 CSS pixels |
| WCAG 2.2, success criterion 2.5.5 (level AAA) | 44 by 44 CSS pixels |
| Apple Human Interface Guidelines | 44 by 44 points |
| Material Design | 48 by 48 density-independent pixels |
With padding: 0.75rem 1rem and a 16px root font size, each link has 24px of vertical padding. Add one line of text, typically 18 to 24px tall, and the link is roughly 42 to 48px tall. That meets the 44px guideline on most fonts and clears the AA minimum easily. The width depends on the text, but 1rem of horizontal padding on each side keeps even a short word like "FAQ" wide enough.
Use rem for the padding so the target grows if the user raises their browser's default font size. Pixel padding stays fixed while the text scales, and the link shrinks relative to its label.
The gap between links
The bar uses gap: 0.25rem. That space belongs to the flex container, not to any link, so a click there does nothing. For a navigation bar this is usually acceptable, but there are two cases where you may want to remove it:
- On touch screens, a dead strip of 4px between two links rarely causes harm, but a gap of
1remor more is easy to tap by mistake and then wonder why nothing happened. - When links have hover backgrounds, a gap makes the highlight appear as separate tiles, while no gap makes it look like a continuous segmented bar.
If you want a continuous bar, set gap: 0 and put all the spacing in the link padding. This keeps the whole bar a single clickable surface.
Making the link fill a taller list item
When all the labels fit on one line, every li has the same height and every link fills it. If one label wraps to two lines (a long item on a narrow screen, or after the user enlarges the text), the flex container stretches the other li elements to match the tallest one. Their links keep their own shorter height, and the extra space inside those li elements is a dead zone.
You can close it by making each li a flex container too, so its link stretches in the cross direction:
.nav-list li {
display: flex;
}
.nav-list a {
display: block;
padding: 0.75rem 1rem;
color: #ffffff;
text-decoration: none;
}The a is now a flex item of the li. Flex items have align-self: stretch by default, so the link grows to the full height of the list item. Its display: block is redundant here, because flex items are blockified, but keeping it is harmless and states the intent.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Link background spills above and below the bar | Padding on an inline `a` does not change the parent's height | Add `display: block` to the link |
| Bar grew, but only the text is clickable | Padding was put on `li`, not on `a` | Move the padding to the `a` |
| Clicks in the spacing between links do nothing | Spacing made with `margin` or `gap` | Move the spacing into link padding |
| Underline or hover color covers only the text | `a` is still inline, or the style was set on `li` | Style the `a`, with `display: block` |
| Links look right at 16px but shrink when text is enlarged | Padding set in `px` | Use `rem` or `em` |
| Link is narrower than the bar item you expected | Block width in a flex item shrinks to its content | Set the width on the `li`, or use `flex: 1` on it |
Checking the result in the browser
Open your browser's developer tools and hover over an a element in the Elements panel. The page overlay shows the content box, padding and margin in different colors. Check that:
- The padding color covers the area you expect to click.
- The
liis exactly as tall as thea. - No colored areas stick out above or below the bar.
What comes next
The link is now a single padded rectangle. That is the surface the next chapter styles: the hover and keyboard focus backgrounds fill the full box instead of hugging the text.
Exercise
A developer writes .nav-list a { padding: 1rem 1.5rem; color: white; text-decoration: none; } with no display rule. The links show a gray background that sticks out above and below the dark bar. What is the cause, and what single declaration fixes it?
Show answer
The a elements are inline by default. Vertical padding on an inline box is painted but does not increase the height of the parent li or the bar, so the background overflows. Adding display: block to .nav-list a makes the link a block box, so its padding counts toward the size of the li and the bar.
Exercise
Your bar has gap: 1rem between items, and users report that clicking just to the side of a label sometimes does nothing. How would you change the CSS so that the whole bar has no dead strips, while keeping the same visual spacing between the text of neighboring links?
Show answer
Set gap: 0 on .nav-list and increase the horizontal padding on .nav-list a. With gap: 1rem and 1rem of horizontal padding per side, the text of neighboring links is 3rem apart. With gap: 0, use padding: 0.75rem 1.5rem to get the same 3rem. The space is now inside the links, so it is clickable.
Exercise
On a narrow screen one label wraps to two lines, and the other links in the row now leave empty space inside their li elements. Which rule on the li makes each link stretch to fill its list item?
Show answer
.nav-list li { display: flex; }. The link becomes a flex item of the li, and the default align-self: stretch makes it as tall as the list item.