More HTML answers

by Danny
Published 3 min read

A skip link lets keyboard and screen reader users jump past the header and navigation straight to the main content. Here is the markup, the CSS and the common mistakes.

The short answer

#

Put a link as the very first focusable element in the body that points at the id of your main content. Hide it visually until it receives focus, then show it. Do not hide it with display: none, or nobody can reach it.


_10
<body>
_10
<a class="skip-link" href="#main">Skip to content</a>
_10
<header>...</header>
_10
<nav>...</nav>
_10
<main id="main" tabindex="-1">
_10
<h1>Page title</h1>
_10
</main>
_10
</body>


_10
.skip-link {
_10
position: absolute;
_10
left: 1rem;
_10
top: -100%;
_10
}
_10
_10
.skip-link:focus {
_10
top: 1rem;
_10
}

Why it happens

#

Keyboard users move through a page with the Tab key, one focusable element at a time. On most sites the first twenty or more stops are the logo, the main navigation, a search box and account links. Without a shortcut, someone using a keyboard or switch device has to tab through all of that on every page before they reach the thing they came for.

A skip link fixes this with plain HTML: it is an ordinary same-page link, so pressing Enter on it moves the scroll position and the focus point to the target. The next Tab press then continues from inside the main content. It is also one way to meet WCAG success criterion 2.4.1, Bypass Blocks.

Screen reader users often have other ways to jump around, such as landmark and heading navigation, which is why you should use a main element as well. Sighted keyboard users usually do not have those shortcuts, so the skip link is mainly for them.

How to fix it

#

The pattern in the short answer is the most common one. The link sits off-screen until it is focused, then slides into view. Make sure the focused state is clearly styled, with good contrast and a visible outline.


_16
.skip-link {
_16
position: absolute;
_16
left: 1rem;
_16
top: -100%;
_16
z-index: 100;
_16
padding: 0.5rem 1rem;
_16
background: #111;
_16
color: #fff;
_16
border-radius: 0.25rem;
_16
}
_16
_16
.skip-link:focus-visible {
_16
top: 1rem;
_16
outline: 3px solid #ffbf47;
_16
outline-offset: 2px;
_16
}

Using :focus-visible means the link appears for keyboard users but not when someone clicks it with a mouse. If you need to support very old browsers, use :focus instead.

Use a visually hidden utility class

If your project already has a visually hidden class for screen reader text, you can reuse it and undo it on focus.


_10
.visually-hidden:not(:focus):not(:active) {
_10
clip-path: inset(50%);
_10
height: 1px;
_10
width: 1px;
_10
overflow: hidden;
_10
position: absolute;
_10
white-space: nowrap;
_10
}


_10
<a class="visually-hidden" href="#main">Skip to content</a>

When the link gains focus, the selector no longer matches and it renders normally in the document flow. Give it some focus styling so it does not look broken.

Make sure focus really moves

Modern browsers move the sequential focus starting point to the fragment target, so the next Tab lands inside main. Adding tabindex="-1" to the target makes this explicit and lets the element itself receive focus, which helps some screen readers announce where you landed. It does not add the element to the tab order.

If you add tabindex="-1", remove the focus outline on that container only, since it is not interactive:


_10
main:focus {
_10
outline: none;
_10
}

Single-page apps

In a client-rendered app, the router may intercept hash links or reset scroll. Test that the skip link still works after navigation. If the router swallows it, handle the click yourself and call focus() on the main element.


_10
document.querySelector('.skip-link').addEventListener('click', (event) => {
_10
event.preventDefault();
_10
const main = document.getElementById('main');
_10
main.focus();
_10
main.scrollIntoView();
_10
});

Common pitfalls

#
  • Hiding it with display: none or visibility: hidden. Those remove the link from the tab order, so it can never be focused.
  • Not making it the first focusable element. If a cookie banner or logo link comes first, users have to tab past that before they find the skip link.
  • Pointing at an id that does not exist. The link silently does nothing. Check the href matches the target id exactly, including case.
  • Hiding it behind a sticky header. Give it a z-index higher than your header, and check that the target is not scrolled under the header. scroll-margin-top on the target helps.
  • Too many skip links. One to the main content is usually enough. Long pages can use landmarks and headings instead.
  • Vague link text. "Skip to content" or "Skip to main content" is clear. "Click here" is not.
#

Was this resource helpful?