Sub topic 7
Keyboard access and visible focus
This chapter assumes you can write semantic HTML and CSS, know how links and lists work, and can attach basic JavaScript event listeners. It builds on the nav markup and skip link from the earlier chapters.
Why keyboard access matters
Many people navigate without a mouse. They include screen reader users, people with motor impairments, and power users who prefer the keyboard. Navigation is the first thing they meet on every page, so a gap here blocks the whole site.
Keyboard users rely on two things: every control must be reachable, and they must be able to see where they are.
The working model: the tab order
Pressing Tab moves focus to the next focusable element in DOM order. Links with an href and button elements are focusable by default. Enter activates a link. Enter and Space activate a button.
This is why semantic markup does most of the work. A div with a click handler gets none of this behavior unless you rebuild it by hand.
<nav aria-label='Primary'>
<ul>
<li><a href='/products'>Products</a></li>
<li><a href='/pricing'>Pricing</a></li>
<li><a href='/support'>Support</a></li>
</ul>
</nav>Tab visits each link in the order it appears in the source. Keep the visual order and the source order the same. If CSS moves items around with order or position, keyboard focus will jump in a way that looks random.
Making focus visible
Browsers draw a default focus ring. Many stylesheets remove it with outline: none, which leaves keyboard users with no indicator. If you restyle focus, replace the outline. Never delete it.
Use :focus-visible. It shows the ring when the browser decides the user needs it, such as during keyboard navigation, and it usually hides the ring for mouse clicks.
nav a:focus-visible,
nav button:focus-visible {
outline: 3px solid #1a5fd0;
outline-offset: 2px;
}The outline-offset leaves a gap between the element and the ring, which keeps the ring readable against the link's own background. WCAG 2.2 asks for a focus indicator with enough contrast against its surroundings, so check the ring color against both light and dark parts of your header.
Do not trap focus or hide it
Common errors in navigation:
- Using
tabindexvalues greater than 0. They override the natural order and are hard to maintain. Use0or-1only. - Hiding a menu with
opacity: 0or by moving it off-screen while its links stay focusable. Focus then vanishes into invisible items. Usehiddenordisplay: noneso the links leave the tab order. - Letting a sticky header cover the focused element. Add
scroll-margin-toporscroll-padding-topequal to the header height.
html {
scroll-padding-top: 4rem;
}Check your work
Put the mouse aside and press Tab from the top of the page. You should be able to see the focus position at every step, reach every link, and leave the navigation without getting stuck. Press Shift+Tab to confirm the reverse order works.
Exercise
A designer asks you to remove the outline on navigation links because it looks ugly when clicked. What do you do instead?
Show answer
Keep a focus indicator and style it with :focus-visible. This hides the ring for most mouse clicks but still shows a clear, high-contrast outline (for example outline: 3px solid #1a5fd0; outline-offset: 2px;) to keyboard users. Do not use outline: none without a replacement.
Exercise
A dropdown menu is hidden with opacity: 0. Keyboard users report that focus seems to disappear while tabbing. Why, and how do you fix it?
Show answer
The links are still focusable even though they cannot be seen, so Tab moves focus into invisible items. Hide the menu with the hidden attribute or display: none so its links are removed from the tab order until the menu is opened.