How to add a skip link: the HTML code and the keyboard test


A skip link is a “Skip to main content” link at the very top of the page: someone using the keyboard presses Enter and lands in the content without tabbing through the whole menu. It takes one line of HTML and a few lines of CSS, no JavaScript. Here is the code to copy, and the three-key test that tells you whether it works from the keyboard.
From the template to the three-key test
#How to create a skip link in HTML and CSS?
Without a skip link, anyone who moves through the page with Tab has to get past the whole header on every page. The W3C, which publishes the web accessibility guidelines (WCAG), talks about dozens of keystrokes before the main content: slow going, and physically painful for some people.
The code has three parts: a link to #main-content at the start of <body>, id="main-content" on <main>, and a CSS rule that hides the link until it gets focus. The GOV.UK Design System places the link right after the opening <body> tag, or right after the cookie banner when there is one. The link points to <main>, the element that holds the page's main content. WordPress adds this link itself as soon as a block theme's template contains <main>, and a template without one leaves all its pages without it.
<body>
<a class="skip-link" href="#main-content">Skip to main content</a>
<header>…</header>
<main id="main-content">…</main>
</body>The link can stay visible all the time, or show up only when it gets focus, that is, when Tab reaches it. A link that's always visible helps more people: keyboard users also include people who rely on screen magnification or voice control. In our example, Valcerne Mountain Guides shows it on focus. For its focus outline, see our entry on focus.
#How to visually hide the skip link and keep it reachable?
With display: none or the hidden attribute, the browser lays out the page as if the link weren't there, so it can't take focus. visibility: hidden blocks focus too: in all three cases, Tab skips the link.
The WebAIM Million 2026 looked at the home pages of the million most popular websites. One skip link in ten was broken, either hidden in a way that made it inaccessible or pointing to a target missing from the page. To hide yours, use a visually hidden technique, which takes the link off the screen but keeps it reachable from the keyboard, and undo it when the link gets focus.
Avoid
Marker 1. The Valcerne logo, which gets focus on the first press of Tab..skip-link {
display: none;
}Correct
Marker 1. The “Skip to main content” link, shown over the logo on the first press of Tab..skip-link {
position: absolute;
top: 1rem;
left: 1rem;
z-index: 100;
clip-path: inset(50%);
width: 1px;
height: 1px;
overflow: hidden;
white-space: nowrap;
}
.skip-link:focus {
clip-path: none;
width: auto;
height: auto;
padding: 0.75rem 1rem;
background: #111111;
color: #ffffff;
box-shadow: 0 6px 18px rgb(0 0 0 / 0.35);
}
“Read more” links: make them accessible without changing the design
#Why the page scrolls but the next Tab goes back to the header?
A smooth-scroll script catches the click and scrolls the page to the target itself, so the browser never follows the link. Focus stays on “Skip to main content”, and the next Tab heads back into the header. Try it below, or open the page with the script.
“Skip to main content” appears at the top left.
The script scrolls the page down to the course title. Focus stays on “Skip to main content”, now out of view.
Focus moves to the logo, and the page scrolls back up to it.
#How to keep smooth scrolling without breaking the skip link?
Since 2016, when someone follows a link to an anchor like #main-content, the HTML specification requires the browser to move the starting point of keyboard navigation to the target. The next Tab starts from there in the tab order.
The smooth-scroll script calls preventDefault(), which stops the browser from following the link. The starting point never moves, so Tab goes to the element right after the link: the logo.
Remove the script and put scroll-behavior: smooth on html, the root element. That property smooths the scroll when the page jumps to an anchor. Set on body, it doesn't apply to the page's scrolling. Wrap it in @media (prefers-reduced-motion: no-preference), so people who turned on reduced motion on their device still get an instant jump.
Avoid
Marker 1. The Valcerne logo, which gets focus on the second press of Tab.document.querySelectorAll('a[href^="#"]').forEach((link) => {
link.addEventListener("click", (event) => {
event.preventDefault();
document
.querySelector(link.getAttribute("href"))
.scrollIntoView({ behavior: "smooth" });
});
});Correct
Marker 1. The “gear list” link, the first link in the main content, which gets focus on the second press of Tab.@media (prefers-reduced-motion: no-preference) {
html {
scroll-behavior: smooth;
}
}#Does the skip link target need tabindex="-1"?
For the keyboard, no: the HTML specification already requires the browser to move the starting point of keyboard navigation to the target. tabindex="-1" is no longer what makes the link work from the keyboard. A script that focuses an element itself still needs it, such as one that moves focus to the new heading after a React Router link. A 2016 Chrome blog post still asked for it while some browsers lacked that starting point. With a screen reader, nothing guarantees that reading picks up at the target without tabindex: test that separately, for example with NVDA.
The attribute has a side effect: <main> takes focus on a mouse click inside it, and an outline appears around it if your focus style targets :focus. The 2016 post advised removing that outline.
#How to test a skip link on every page template?
For each page template, load a page whose main content has a link, a button or a field, with the cookie banner closed. On a page without one, or with the banner still open, the test can fail even though the link works. Press Tab, Enter, Tab: the second Tab must land in the main content. Don't click in the page along the way, since a click also moves the starting point of keyboard navigation.
“Skip to main content” appears at the top left.
The page scrolls down to the course title. No outline appears: without tabindex, <main> doesn't take focus.
Focus enters the content, on the “gear list” link.
With or without the script, the page scrolls, so only the second Tab shows whether the link works. If focus goes back to the header, check that <main> has id="main-content" and that no script catches the click. If a sticky header covers the top of the content, set scroll-padding-top on html to reserve its height. The code is in our section on sticky headers.
WCAG asks every page for a way past the content repeated across a site. With your skip link in place, check these points on each template.
#Why Safari skips the link when you press Tab?
Safari on a Mac skips every link on Tab, “Skip to main content” included, unless one setting is on. Turn on “Press Tab to highlight each item on a webpage” in Safari > Settings > Advanced. With it off, Option+Tab reaches links.
Marker 1. The “Press Tab to highlight each item on a webpage” checkbox, turned on, with the note on Option+Tab below it.The three-key test covers the keyboard. A full accessibility audit also checks reading with screen readers, and Includdy runs these audits.


