On this page
An anchor link can navigate to the right section while leaving its heading hidden behind a sticky header. Add a scroll offset so the browser has room to place the target below the header, then check focus and navigation separately.
Key Takeaways
- Use
scroll-margin-topon fragment targets to provide space below a top-aligned sticky header. - Set the offset to the header’s actual height plus any desired gap; revisit it when the header changes size.
- A visible heading is not proof that keyboard focus is visible or that focus order is meaningful.
- Test browser history, reduced-motion settings, and targets near both ends of the document.
Why an anchor link is hidden behind a sticky header
A URL fragment such as #installation identifies an element with the matching id. When the browser navigates to it, it may scroll that element into view. A sticky header remains at the top of the viewport as the page scrolls, so it can cover the target’s upper edge.
The browser cannot always place the target at the requested offset. For example, a heading close to the bottom of the document may not have enough content below it to permit further scrolling.
Advertisement
Reproduce the problem and add a CSS offset
Save the following as anchor-demo.html and open it in a browser. It uses only HTML and CSS. First, temporarily remove the scroll-margin-top declaration, scroll down, and activate Details. Then restore the declaration and try again.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sticky header anchor demo</title>
<style>
:root {
/* This demo's header is exactly 5rem tall. */
--header-height: 5rem;
--anchor-gap: 0.75rem;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: sans-serif;
line-height: 1.5;
}
header {
position: sticky;
top: 0;
z-index: 1;
height: var(--header-height);
display: flex;
align-items: center;
padding-inline: 1rem;
background: #222;
}
nav {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
nav a {
color: #fff;
}
main {
max-width: 48rem;
margin-inline: auto;
padding-inline: 1rem;
}
section {
min-height: 70vh;
}
h1[id],
h2[id] {
scroll-margin-top: calc(var(--header-height) + var(--anchor-gap));
}
:target {
background-color: #fff3a3;
}
:focus-visible {
outline: 3px solid #b00020;
outline-offset: 3px;
}
@media (prefers-reduced-motion: no-preference) {
html {
scroll-behavior: smooth;
}
}
</style>
</head>
<body>
<header>
<nav aria-label="Page sections">
<a href="#overview">Overview</a>
<a href="#details">Details</a>
</nav>
</header>
<main>
<section>
<h1 id="overview" tabindex="-1">Overview</h1>
<p>Use the links in the sticky header to move between sections.</p>
</section>
<section>
<h2 id="details" tabindex="-1">Details</h2>
<p>This heading is the second fragment target.</p>
<p>Use the browser's Back and Forward controls after navigating.</p>
</section>
</main>
</body>
</html>
The offset is applied to the headings that have IDs. The extra 0.75rem leaves a small gap between the header and a target when the browser can scroll far enough. The :target highlight makes the current fragment easier to identify; it is not a focus indicator.
The fixed header height keeps the calculation simple for this demonstration. In a real design, check the rendered height at narrow widths, text zoom, and other responsive breakpoints. If the header can grow, update the offset to match or use a layout that keeps its height predictable. Do not let a fixed height clip navigation text when it wraps.
The example has not been executed in this environment. MDN’s scroll-margin documentation and the WCAG guidance on focus order and visible focus could not be consulted here; review those sources and test against your supported browsers and accessibility requirements before deploying.
An alternative: scroll padding
If one scrolling container needs the same inset for many targets, scroll-padding-top on that container may be more convenient than setting a margin on each target. For page scrolling, a starting point is:
html {
scroll-padding-top: 5.75rem;
}
Choose the element that actually scrolls and verify the behavior in your supported browsers. If you combine scroll padding and target scroll margins, their effects can both contribute to the space; avoid adding both without checking the result.
Check focus, scrolling, and browser history
A fragment jump, a change in the focused element, and the visual scroll position are related but distinct. Browsers can differ in how fragment navigation affects focus and where sequential keyboard navigation continues. The headings in the example have tabindex="-1", which makes them programmatically focusable without adding them to the normal Tab sequence. That does not guarantee identical focus behavior across browsers or assistive technologies.
Use a keyboard and your supported browser and screen-reader combinations to check the following:
- Keyboard navigation: Tab to a section link and activate it with Enter. Confirm that the target is visible, then press Tab again and check that navigation continues in a meaningful order. Inspect the actual focus position rather than assuming the highlighted heading has focus.
- Visible focus: Check that whichever element has focus has a discernible indicator and is not obscured by the sticky header. The example styles
:focus-visible, but real components and browser behavior still need testing. - Back and Forward: Navigate between both fragment links, then use the browser’s Back and Forward controls. Check the fragment, target visibility, and focus. History traversal can restore a previous scroll position rather than reproducing the exact initial jump.
- Reduced motion: Enable the operating system or browser’s reduced-motion preference and repeat the navigation. The example requests smooth scrolling only when
no-preferenceis reported; it leaves the browser’s default behavior otherwise. - Page boundaries: Test an early target and a final target with little content after it. Scrolling is constrained by the document’s start and end, so the desired gap may not always be achievable. If a bottom target remains covered, consider adding meaningful space after it or changing the layout, then retest.
- Responsive layout: Repeat with a narrow viewport and enlarged text. Measure the header’s rendered height and check that the offset still clears it.
Advertisement
Common Mistakes
- Offsetting only one heading. Apply the rule to every element that can be a fragment destination, or use an appropriate container-level inset.
- Guessing the header height once. A header that wraps, expands, or changes at a breakpoint can become taller than the offset.
- Treating
:targetas keyboard focus. A highlighted destination and the currently focused element are not necessarily the same. - Forcing focus after every jump. JavaScript focus management can be useful for custom interactions, but it can also change reading and keyboard order. Test it with the intended interaction and assistive technologies instead of adding it automatically.
- Assuming the offset overrides scroll limits. Near the beginning or end of a document, there may not be enough scrollable space to produce the requested position.
Best Practices
Use native fragment links and unique, descriptive IDs for ordinary in-page navigation. Keep the sticky header’s size and target offset in sync, and make focus indicators visible independently of target highlighting. Test with keyboard navigation, browser history, reduced motion, responsive layouts, and the assistive technologies your project supports.
Conclusion
scroll-margin-top is a concise way to make fragment targets visible below a sticky header. It solves the positioning problem, not the entire accessibility problem: verify focus, motion, history, and page-edge behavior as well.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.