Full answer
When a link points to #section, the browser scrolls so the top of that element touches the top of the viewport. A fixed or sticky header sits on top of that spot and hides the first lines of the target.
There are two simple fixes, and both use scroll offsets that the browser takes into account.
scroll-margin-topon the targets leaves space above each one. It is part of the element, so it works even if the header height differs per page.scroll-padding-topon the scrolling container (usuallyhtml) applies to every target in it. This is the shortest solution when one header covers the whole site.
Smooth scrolling is scroll-behavior: smooth on the scroll container. It animates anchor jumps and scrollIntoView calls. Some users get dizzy from large animated movement, so wrap it in a check for the reduced motion preference: use smooth scrolling only when prefers-reduced-motion: no-preference.
Clicking the Fees link scrolls so that the section starts 64px below the top, exactly under the header, because of scroll-padding-top. The Notes section has its own scroll-margin-top: 100px, which adds a larger gap for that one target. Both are applied while the page scrolls smoothly if the user has no reduced motion preference.
Do not use scroll-behavior: smooth on very long pages without thinking. A jump across many screens takes a long time and can feel slow.
<style>
html { scroll-padding-top: 64px; }
@media (prefers-reduced-motion: no-preference) {
html { scroll-behavior: smooth; }
}
body { margin: 0; font-family: sans-serif; }
header { position: fixed; inset: 0 0 auto 0; height: 64px; background: #333; color: #fff; padding: 0 16px; display: flex; align-items: center; gap: 16px; }
header a { color: #fff; }
section { height: 700px; padding-top: 80px; }
#notes { scroll-margin-top: 100px; }
</style>
<header id="hdr"><a href="#fees" id="l1">Fees</a><a href="#notes" id="l2">Notes</a></header>
<section id="intro">Intro</section>
<section id="fees">Fees</section>
<section id="notes">Notes</section>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Anchor jumps align the target's top with the viewport top.
- scroll-margin-top on a target and scroll-padding-top on the scroller add an offset.
- scroll-behavior: smooth animates anchor jumps and scrollIntoView.
- Wrap smooth scrolling in prefers-reduced-motion: no-preference.
- Use a custom property for the header height.
Common mistakes
- Adding padding-top to the body to hide the header problem. It does nothing for scroll targets.
- Applying smooth scrolling for everyone, including users who asked for reduced motion.
- Hard-coding the header height in many places instead of using one custom property.
Follow-ups
- What is the difference between scroll-margin and scroll-padding?
- How does JavaScript scrollIntoView use smooth scrolling?
Tips
- Say that scroll-padding goes on the scroller and scroll-margin goes on the target.
- Keep the header height in a custom property and use it for both the header and the scroll offset.