Sub topic 2
Make Navigation Links Easy to Click
Assume you know basic HTML, CSS selectors, and the box model. This chapter focuses on the link box itself, its interaction states, and the accessibility information around it.
Expand the Link’s Click Area
A link can contain only its text, but users should be able to click its surrounding space. Apply padding to the link and set display: block, which makes the link occupy the available width of its list item.
<nav aria-label="Main navigation">
<ul class="nav-list">
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>.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: white;
text-decoration: none;
}The nav element creates a navigation landmark. The unordered list keeps the links grouped as a list. The padding increases the usable click and touch area without changing the link destination.
Style Hover and Keyboard Focus
Hover styles help pointer users. Keyboard users need a visible focus style when they move through links with the Tab key. Use :focus-visible so the focus indicator appears when keyboard focus needs to be shown.
.nav-list a:hover,
.nav-list a:focus-visible {
background: #374151;
}
.nav-list a:focus-visible {
outline: 2px solid #38bdf8;
outline-offset: -2px;
}Do not remove the outline without replacing it with another clear indicator. A color change alone may be difficult to see.
Mark the Current Page
The aria-current="page" attribute identifies the link for the page currently displayed. Style that same attribute so the visual and accessibility states agree.
.nav-list a[aria-current="page"] {
background: #111827;
border-bottom: 3px solid #38bdf8;
}Keep the Landmark Understandable
A page can contain several navigation landmarks, such as main navigation and footer navigation. Give each one a distinct accessible label with aria-label. Keep real links in the tab order, and ensure every focused link remains visible against its background.
Exercise
Why should padding be applied to the anchor instead of only to its list item?
Show answer
Padding on the anchor expands the link's actual clickable and keyboard-focusable area.