Résumé

The anchor gap that grew with the header

Clicking a nav link scrolled the target section to just below the header, leaving a strip of the previous section visible in between. It looked like a margin. It was two of them, added together.

Measuring first

The instinct is to look at the header and conclude it is too tall. Measuring settles it in one line: the gap was 88px and the header was 72px.

That ratio is the tell. A too-tall header produces a gap the same size as itself. A doubled offset produces a gap larger than the header, and it grows and shrinks with it. Two numbers, one subtraction, and the whole investigation is over.

The cause

The anchor offset was being applied in two places at once: a scroll-padding-top on the scroll container, and a scroll-margin-top on the target section. Those two properties are documented as composing, which is exactly what made this easy to miss — neither is wrong on its own, and the CSS is doing precisely what it was told.

The fix was to delete one of them. An earlier version of this had also tried to paper over it with a negative scroll-margin-top large enough to cancel the positive one, which "fixed" the gap by making the arithmetic harder to reason about and would have broken again silently the next time the header changed size.

The part that is actually interesting

A hardcoded offset cannot be correct here, because the header is not a fixed height. It is a single row on desktop and two rows under 720px, and both grow with the reader's browser font size and zoom level. Any value in rem or px is wrong somewhere.

So the offset is now measured: --header-offset is written from the rendered header's own height, kept live by a ResizeObserver, and removed on unmount. It is used in exactly one place, and the guard asserts that no section has reintroduced a second offset.

The guard clicks every nav link at 1440px, 900px, 390px and 320px and asserts the section's top edge is flush with the header's bottom edge within a pixel. It waits for scrolling to settle rather than sleeping a fixed interval, because smooth-scroll duration scales with distance and a short timeout measures a scroll still in flight — which reads as a 94px gap for a layout that is perfectly correct.