Sub topic 5
Adding a skip link
A skip link is a link at the very start of a page that jumps keyboard users past repeated blocks, usually the header and primary navigation, straight to the main content. This chapter assumes you know basic HTML and CSS, understand how fragment links (href="#id") work, and have seen the nav landmark from the previous chapter.
Why skip links exist
A mouse user sees the page and clicks the article they want. A keyboard user presses Tab to move focus from one interactive element to the next. If your header has a logo link, six navigation links, a search field, and an account button, a keyboard user must press Tab about nine times on every page before reaching anything unique to that page.
This hurts several groups:
- People who cannot use a mouse because of a motor impairment.
- Screen reader users, who often navigate by Tab as well as by landmarks.
- Switch and voice-control users, for whom each extra step is slow.
- Sighted keyboard users who prefer not to use a mouse.
WCAG 2.2 success criterion 2.4.1, "Bypass Blocks" (Level A), requires a mechanism to skip blocks of content repeated across pages. Landmarks such as main can satisfy it for screen reader users who have landmark shortcuts. A skip link is the mechanism that also works for keyboard-only users without assistive technology, so most sites provide one.
The markup
The skip link is an ordinary anchor that points at the id of the main content region. Place it as the first focusable element in the document, before the header.
<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>Three details matter here:
- Order. It must come first in the DOM. If a cookie banner or a chat widget appears earlier in the source, those become the first Tab stops, and the skip link is no longer first.
- Target. The
hrefmust match anidthat exists on the page. A typo produces a link that does nothing, and nothing in the browser warns you. - Text. Say where it goes: "Skip to main content". Avoid "Skip" alone, which is ambiguous when read out of context in a list of links.
Hiding it until focus
Most designs do not want a visible "Skip to main content" link above the logo for every mouse visitor. The standard technique moves the link off-screen, then moves it back when it receives keyboard focus.
.skip-link {
position: absolute;
left: 0.5rem;
top: -4rem;
padding: 0.5rem 1rem;
background: #fff;
color: #000;
}
.skip-link:focus {
top: 0.5rem;
}
nav a:focus-visible,
nav button:focus-visible {
outline: 3px solid #1a5fd0;
outline-offset: 2px;
}
nav a[aria-current='page'] {
font-weight: 700;
text-decoration: underline;
}The .skip-link rule places the link above the top edge of the viewport, so it is invisible but still focusable. When a keyboard user tabs to it, :focus pulls it into view at the top-left. The remaining rules style focus and the current page in the navigation, which later chapters cover in detail.
Hiding techniques that break the link
How you hide the link decides whether it works. Compare the options:
| Technique | Focusable? | Result |
|---|---|---|
| `top: -4rem` (off-screen positioning) | Yes | Works. The link is reachable and can be revealed on focus. |
| `display: none` | No | Broken. The link is removed from the tab order. |
| `visibility: hidden` | No | Broken. Same problem. |
| `hidden` attribute | No | Broken. It applies `display: none`. |
| `opacity: 0` with no focus style | Yes | Keyboard focus lands on an invisible element, which is confusing. |
The rule is that the link must be hidden only visually, never removed from the accessibility tree and tab order, and it must become visible when focused. A sighted keyboard user who tabs onto an invisible control has no idea where focus is.
Make the revealed link readable
When revealed, the link needs enough contrast and a stable position. The sample uses black text on a white background. If your header has a dark or image background, the white box still stands out. Also check that the link is not clipped by a parent with overflow: hidden. Because it uses position: absolute, an ancestor with position: relative and overflow: hidden can cut it off. Keep the skip link as a direct child of body where possible.
Making the target receive focus
Activating a fragment link scrolls the page to the target. In current browsers it also moves the sequential focus starting point to the target. That means the next Tab press goes to the first focusable element inside or after #main, which is the behavior you want.
Older browsers and some assistive technology combinations do not do this reliably. A common defensive fix is to make the target programmatically focusable with tabindex="-1":
<main id="main" tabindex="-1">
<h1>Home</h1>
</main>A tabindex of -1 means the element can receive focus from a script or a fragment navigation, but it is not added to the normal Tab order. Without this, in some browsers the next Tab press would return to the top of the page.
If you add it, suppress the outline that now appears around the whole main region when focused, since a large box around the page content is noise, not information:
main:focus {
outline: none;
}Do this only for an element that is a programmatic focus target and not a control. Removing outlines from links and buttons would harm keyboard users.
Skipping to a heading instead
Some teams target the h1 rather than main. That announces the page title to screen reader users right away, and h1 needs tabindex="-1" to receive focus. Either choice satisfies the criterion. Pick one and use it consistently across templates.
Common errors
- The link is not first. A banner, modal trigger, or injected widget sits before it in the DOM.
- The target id is missing or duplicated. Templates for different page types sometimes use
id="content"in one andid="main"in another. Standardize the id in a shared layout. display: nonehiding. The link never appears in Tab order, so no one can use it.- Single-page apps. Client-side routing may replace the content without a full page load. In that case focus can stay on the old link, and you may need to move focus to the new
h1ormainin your router after navigation. - Multiple skip links. One link is usually enough. Offering several, such as "Skip to search" and "Skip to footer", can help on complex pages, but each one adds noise on every page, so add them only when a real task needs them.
- Hidden by a sticky header. If a fixed header overlaps the target, the focused heading can scroll underneath it. Set
scroll-margin-topon the target to match the header height.
main {
scroll-margin-top: 4rem;
}Verifying it works
You can check a skip link in under a minute:
- Load the page and click once in the empty browser area, or press the browser address bar's Tab key, so focus starts at the top of the document.
- Press Tab once. The skip link should appear visibly, with a clear outline or background.
- Press Enter. The view should move to the main content.
- Press Tab again. Focus should land on the first interactive element in the main content, not back in the header.
- Press Shift+Tab from the first link on the page and confirm nothing focusable exists before the skip link.
Run the test on every distinct page template, because the target id is the part that most often differs between templates.
Exercise
A developer hides the skip link with .skip-link { display: none; } and shows it on hover with .skip-link:hover { display: block; }. Why does this fail the people it is meant for, and what should replace it?
Show answer
A keyboard user never hovers, and display: none removes the link from the tab order, so it can never receive focus and so never appears. Replace it with off-screen positioning, such as position: absolute; top: -4rem;, and reveal it with a :focus rule that sets top: 0.5rem. The link stays focusable and becomes visible exactly when a keyboard user reaches it.
Exercise
After activating the skip link, pressing Tab sends focus back to the logo at the top of the page in one browser. The target is <main id="main">. What single attribute fixes this, and what CSS should accompany it?
Show answer
Add tabindex="-1" to <main id="main"> so the fragment navigation can move focus to it, which sets the starting point for the next Tab press. Accompany it with main:focus { outline: none; } so the whole content region does not show a large focus outline. Keep outlines on links and buttons.