9.9 · Layout
Jump links that clear sticky headers: scroll-margin
Jump links land below a sticky header instead of hiding behind it.
Clicking a link to #section scrolls the heading to the
very top of the viewport, right under a sticky header. The heading you
jumped to is hidden.
scroll-margin-block-start on the target leaves room. To
set it once for the whole page, use
scroll-padding-block-start on the scroll container,
usually html.
link.addEventListener('click', (event) => {
event.preventDefault()
scrollTo(0, target.offsetTop - header.offsetHeight)
})
section {
scroll-margin-block-start: 4.5rem;
}
/* or once, for every target */
html { scroll-padding-block-start: 4.5rem; }