Sub topic 4
Make links easy to click with padding and display: block
This chapter assumes you know basic HTML elements and attributes, basic CSS selectors and properties, and the CSS box model (margin, padding, border). It picks up from the previous chapter, where the <ul> became a horizontal Flexbox row. Now the links inside that row need a click area that is large and predictable.
Why the default link is hard to click
An <a> element is display: inline by default. An inline box has a content area that matches the height of its text. Vertical padding on an inline box is painted, but it does not change the height of the line it sits in, and it does not push neighbors away. Vertical margin on an inline box has no effect at all.
The practical result is a small click target. The reader must aim at the letters themselves. On a touch screen that is frustrating, and for anyone with limited motor control it can be a real barrier.
Here is the bar as it stands after the layout chapter, with no styling on the links:
.nav-list {
display: flex;
gap: 0.25rem;
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}Each <li> is a flex item, but the <a> inside it is still inline and only as large as its text.
Make the link fill its list item
The fix is two declarations on the link:
.nav-list a {
display: block;
padding: 0.75rem 1rem;
color: #ffffff;
text-decoration: none;
}display: blockturns the link into a block box. Block boxes respect vertical padding when calculating their height, and they stretch to the full width of their container.padding: 0.75rem 1remadds space inside the link: 0.75rem above and below, 1rem left and right. The padding is part of the link, so it is clickable.
Because the <li> is a flex item sized by its content, the block-level link makes the <li> as large as the text plus the padding. The link then fills that whole rectangle. Clicking anywhere in the padded area follows the link.
Why padding on the link, not on the list item
You could put the padding on the <li> instead. The text would look identical, but the click area would be smaller: only the text would be a link, and the padded edge of the <li> would be dead space. Putting the padding on the <a> makes the padding part of the clickable element.
The same reasoning applies to margins. A margin is outside the element, so it is never clickable. Use gap on the flex container for space between items, and use padding on the link for the click area.
Common errors
- Padding on an inline link. If you forget
display: block, the background and hover color may extend above and below the text, but the neighboring items and the bar's height do not adjust. The bar looks cramped and the shapes overlap. - Padding on the
<li>instead of the<a>. The bar looks right until you click the edge of an item and nothing happens. - Fixed heights. Setting
height: 48pxon the link works until the user increases their font size. Padding inremunits grows with the user's text size. Prefer it over fixed pixel heights. - Targets too small. WCAG 2.2 sets a minimum target size of 24 by 24 CSS pixels (success criterion 2.5.8). With 0.75rem padding at a 16px root size and normal line height, each link is about 46 px tall, which clears that minimum comfortably.
Check your understanding
Exercise
A link in your nav bar has padding: 0.75rem 1rem but no display declaration. The background color on hover bleeds over the bar's edges and the bar's height does not change. Why, and what one declaration fixes it?
Show answer
The <a> is inline by default, and vertical padding on an inline box is painted but does not affect the line's height or the layout of surrounding elements. Adding display: block to the link makes it a block box, so the vertical padding counts toward its height, the <li> grows to fit it, and the whole padded rectangle is clickable.
Exercise
Why is it better to use gap on the flex container for spacing between nav items than to add margin to each link?
Show answer
Margin is outside the link's box, so it adds no clickable area, and it needs extra rules to avoid doubled or leading and trailing space. gap spaces the items only between each other and keeps all the click area inside the padding of each link.