Sub topic 5
Adding a skip link
This chapter assumes you know basic HTML and CSS, including how links and lists work, and that you can read a short CSS rule. It builds on the semantic navigation markup from the previous chapter.
Why a skip link exists
A keyboard user moves through a page with the Tab key. Focus follows the order of the HTML. If your header holds a logo and a navigation with eight links, a keyboard user must press Tab about nine times on every page before reaching the content. A screen reader user hears the same links again on each page.
A skip link is the first link on the page. It jumps focus straight to the main content. Most mouse users never see it. Keyboard users get it on their first Tab press.
Screen reader users can also jump between landmarks, but a skip link helps people who use only a keyboard, switch devices, or a screen magnifier. It is also a requirement under WCAG 2.2 success criterion 2.4.1, Bypass Blocks.
The markup
Put the link before the header, and give the main element an id that matches the link's href.
<a class="skip-link" href="#main">Skip to main content</a>
<header>
<nav aria-label="Primary">
<ul>
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/support">Support</a></li>
</ul>
</nav>
</header>
<main id="main">
<h1>Home</h1>
</main>The link text says where it goes. "Skip to main content" is clear. "Skip" alone is not.
Hiding it until focus
The link should be off-screen until it receives focus. Do not use display: none or visibility: hidden. Those remove the link from the tab order, so keyboard users could never reach it.
.skip-link {
position: absolute;
left: 0.5rem;
top: -4rem;
padding: 0.5rem 1rem;
background: #fff;
color: #000;
}
.skip-link:focus {
top: 0.5rem;
}Moving the link above the viewport with top: -4rem keeps it focusable. The :focus rule slides it into view. Give it a solid background and enough contrast so it stays readable over whatever sits underneath.
Common errors
- The target does not exist. If
href="#main"has no matchingid="main", nothing happens. Check that the two match exactly, including case. - The link is not first. If another focusable element comes before it, the shortcut costs an extra Tab press.
- Hidden with
display: none. The link is never focusable. - Focus does not follow the jump. Some older browsers scroll to the target but leave keyboard focus at the link. If you see this, add
tabindex="-1"to themainelement so it can receive focus. - Several skip links with no purpose. One link to main content is usually enough. Add more only for pages with large repeated blocks, such as a long sidebar.
Exercise
A developer hides the skip link with display: none and shows it with .skip-link:focus { display: block; }. Why does a keyboard user never see it, and how would you fix the CSS?
Show answer
An element with display: none is removed from the tab order, so it can never receive focus and the :focus rule never applies. Move it off-screen instead, for example position: absolute; top: -4rem;, and change top to 0.5rem in the :focus rule.