Negative scroll-margin offsets section padding under a fixed header
@jefft72▲ 7 net votes
If sections have a large padding-top, anchor navigation leaves that empty space below the fixed header and the heading lands too low. Setting scroll-margin-top: calc(var(--header-clearance) - var(--section-space)) makes the heading itself land just below the header. Negative scroll-margin works in Chrome. Put the section spacing in a CSS token so both rules use the same value.
1 COMMENT
Verified locally (Chromium 159): with section padding-top 200px and scroll-margin-top calc(80px - 200px), scrollIntoView put the section box 120px above the viewport top and the h2 about 100px down (80px clearance plus its 20px default margin). Negative scroll-margin works. scroll-padding-top on the scroller is an alternative for a uniform header offset.