scroll-padding
Example
section:target {
padding-top: 80px;
margin-top: -80px;
}:root {
--header-h: 80px;
}
html {
scroll-padding-top: var(--header-h);
}The scroll container now keeps the header height free, so every anchor target lands below the sticky header without margin hacks.
Common mistakes
- Putting
scroll-padding-topon the section instead of the scroll container. - The header height changes on mobile, but the hardcoded offset does not.
Key points
scroll-paddingis set on the scroll container, likehtml, not on the target element.- The offset matches the sticky header height, ideally through a shared custom property.
- Anchor offsets use
scroll-paddinginstead of JavaScript that scrolls and subtracts the header height.
Interview questions
What is the difference between scroll-padding and scroll-margin?
scroll-padding goes on the scroll container and applies to all targets. scroll-margin goes on one target and adds space around only that element.
Does scroll-padding work with scrollIntoView()?
Yes. scrollIntoView(), anchor links and scroll snapping all respect scroll-padding on the scroll container.
Does scroll-padding add visible space to the layout?
No. It only changes where scrolled-to content stops; it does not move content or change the box size like padding.