On this page
- Key Takeaways
- Why the URL changes but focus may not
- Keyboard-only diagnostic test
- A focusable main-content target
- Diagnose targets, rendering, and sticky headers
- Check the target ID
- Check when the target exists
- Check what the header covers
- Test with assistive technology
- Common Mistakes
- Best Practices
- Conclusion
A skip link can update the URL fragment and scroll to the main content while keyboard focus remains elsewhere. If the next Tab press takes the user back through navigation, the link has not delivered the intended keyboard experience. This guide shows how to diagnose a skip link not moving keyboard focus and how to make the destination focusable.
Key Takeaways
- A fragment change, a scroll, and a focus change are related but distinct behaviors. Check each one.
- The skip link’s fragment must match a unique ID on a target that exists when the link is activated.
- A main-content container with
tabindex="-1"can receive focus without becoming an ordinary Tab stop. - Test the next Tab press and assistive technology behavior as well as the visible scroll. One browser test does not establish WCAG conformance.
Why the URL changes but focus may not
A link such as <a href="#main-content">Skip to main content</a> points to an element with id="main-content". Activating it navigates to that fragment. The browser may scroll the target into view, but the address bar and scroll position alone do not prove that the target became the active element or that subsequent keyboard navigation will start where you expect.
A plain <main> element is not normally an ordinary Tab stop. Adding tabindex="-1" makes it available for programmatic focus without adding it to the normal sequential Tab order. Fragment-navigation and focus behavior can vary by browser and document setup, so verify the actual result rather than assuming the fragment change is enough.
WCAG’s bypass-blocks criterion addresses providing a way to bypass repeated content. A skip link is one possible technique, but its presence alone does not establish that a page conforms. The official WCAG guidance and browser documentation could not be consulted for this article; check those sources alongside your project’s applicable accessibility requirements.
Advertisement
Keyboard-only diagnostic test
Use a page where the skip link appears before repeated navigation and where main content contains at least one interactive element. Run this test in each browser your project supports:
- Reload the page and use Tab until the skip link is visibly focused. It should be discoverable without a mouse.
- Press Enter. Check that the URL fragment matches the intended target ID and that the page scrolls to the main content.
- Without pressing Tab again, check for a visible focus indication on the target. A missing outline is a warning, but it does not by itself prove that focus failed: the target may have focus without an appropriate focus style.
- Press Tab once. It should move to the next appropriate interactive element in the main content, rather than sending you back through the repeated navigation.
- Continue through the page with Tab and Shift+Tab. Check that the order makes sense and that no focus indicator is hidden behind a sticky header.
As a supplementary check, a developer can inspect document.activeElement in the browser console immediately after activating the link. It should identify the intended target for the implementation below. This inspection is not part of the keyboard-only test and does not replace checking visible focus.
The Tab test checks the sequential focus order of interactive elements; it does not establish the reading order of all text. Test that separately with a screen reader, as described below.
A focusable main-content target
The following standalone HTML example provides a native fragment link, a focusable destination, and a small JavaScript enhancement that explicitly focuses the destination when the link is activated. Save it as an HTML file and open it in a browser. The example has not been executed in this environment; verify it in your supported browsers.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Skip link example</title>
<style>
.skip-link {
position: absolute;
top: -5rem;
left: 0.5rem;
z-index: 1001;
padding: 0.75rem;
color: #fff;
background: #000;
}
.skip-link:focus {
top: 0.5rem;
}
header {
position: sticky;
top: 0;
z-index: 10;
background: #fff;
}
main {
scroll-margin-top: 5rem;
}
main:focus {
outline: 3px solid #005fcc;
outline-offset: 3px;
}
</style>
</head>
<body>
<a class="skip-link" href="#main-content">Skip to main content</a>
<header>
<nav aria-label="Primary navigation">
<a href="#section-one">Section one</a>
<a href="#section-two">Section two</a>
</nav>
</header>
<main id="main-content" tabindex="-1">
<h1>Page content</h1>
<p>The main content starts here.</p>
<p><a href="#section-one">Go to section one</a></p>
<section id="section-one"><h2>Section one</h2></section>
<section id="section-two"><h2>Section two</h2></section>
</main>
<script>
const skipLink = document.querySelector('.skip-link');
skipLink.addEventListener('click', (event) => {
// Keep modified clicks and alternate-button behavior native.
if (event.defaultPrevented || event.button !== 0 ||
event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) {
return;
}
const target = document.getElementById('main-content');
if (!target) return;
event.preventDefault();
window.location.hash = 'main-content';
target.focus();
target.scrollIntoView({ block: 'start' });
});
</script>
</body>
</html>
The link still has a real href, so it has a native fragment destination even if JavaScript is unavailable. Whether native navigation alone produces the desired focus behavior must still be tested. With JavaScript enabled, the handler updates the fragment, focuses the target, and requests scrolling. It runs for ordinary activation of this link, including keyboard activation; it does not move focus automatically on page load.
The focus outline is deliberately visible on main:focus. Adapt it to your design, but do not remove it without providing an equally clear indicator. The 5rem scroll margin is an example, not a universal header height: adjust it to keep the target and its focus indication visible beneath your actual header.
Advertisement
Diagnose targets, rendering, and sticky headers
Check the target ID
Confirm that the fragment and ID match exactly, and that the ID is unique. A typo, duplicate ID, or link to the wrong element can make the scroll or focus result confusing. Place the ID on the element you intend to receive focus, rather than on a hidden anchor elsewhere.
Avoid tabindex="0" on the main container unless you intentionally want it in the normal Tab sequence. Do not use positive tabindex values to force the order: they can make keyboard navigation harder to predict.
Check when the target exists
In a client-rendered page, the skip link may be available before the main target is mounted, or a route change may replace the target after it receives focus. Inspect the DOM at activation time and check the active element again after rendering completes.
Prefer rendering the destination with the initial page where practical. If the destination must be created later, coordinate the user-initiated skip action with rendering: ensure the correct, visible target exists before focusing it. Do not silently discard the action or move focus later without regard to what the user has done in the meantime. Also check your router’s fragment and history behavior; it may manage navigation differently from the standalone example.
Check what the header covers
A sticky or fixed header can cover the destination even when scrolling and focus both work. Test at different viewport sizes and zoom levels. Use a suitable scroll-margin-top on the destination or another layout-aware offset, and check that the heading and focus indicator remain visible. If header height changes responsively, a fixed offset may need corresponding adjustments.
Test with assistive technology
Repeat the journey with the screen readers and browsers relevant to your audience. Activate the skip link, listen for what is announced, then read forward from the destination and navigate interactive elements. Check that users can reach the main heading and content without being sent back through the repeated navigation.
Screen reader reading or virtual-cursor position is not always the same thing as DOM keyboard focus. A successful document.activeElement check therefore does not prove the complete reading experience is correct. Record the browser, operating system, screen reader, and test steps when reporting a failure; behavior can differ across combinations.
Common Mistakes
- Treating the hash as proof of focus. Check visible focus and the next Tab stop as well.
- Removing outlines globally. A correctly focused target can still be unusable if its focus is invisible.
- Focusing a missing or hidden target. Account for asynchronous rendering and route transitions.
- Fixing only the scroll position. A header offset does not repair an incorrect keyboard focus position.
- Declaring conformance from one test. Test relevant browser and assistive-technology combinations and assess the applicable requirements across the page.
Best Practices
Keep the skip link early in the document and visible when focused. Give the destination a unique, stable ID, and use a meaningful main-content container. Make focus changes a response to the user’s navigation action, not an unsolicited page-load behavior. Recheck the interaction after changes to routing, headers, layout, or rendering.
Conclusion
Diagnose the fragment, scroll, visible focus, and next keyboard stop separately. A focusable main target and an explicit, user-initiated focus action can resolve many failures, but the final check is the experience across the browsers and assistive technologies your audience uses.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.