Prerequisites
Start with a vertical scroll container using mandatory snap behavior, sections that snap at their start edge, and a fixed header that overlays the top of the viewport. The key symptom is precise: after selecting a later section, its heading is physically behind the header even though the container snapped to that section.
Read the failure before changing CSS
The failure is not “snap did not happen.” A useful check finds that the container still has mandatory vertical snapping, the selected section still has start alignment, and the header is the element covering the heading’s position. That narrows ownership: child alignment is already doing its job; the scroller’s visible region is incomplete.
Implementation decision
Apply a top scroll-padding value to the scroll container equal to the shared fixed header height. Leave the section alignment and snap strictness alone. Scroll padding adjusts the snapport—the area the browser uses to decide where a snapped target should be comfortably visible—rather than adding layout space inside every section.
The expected result is a later section snapping with its heading at or below the header’s lower edge. Its internal heading position remains unchanged, and the existing mandatory/start snap model remains intact. This is a container repair for shared viewport chrome, not a child-content adjustment.
Proof boundary and limitation
Verify visibility geometrically after snapping: compare the heading’s top edge with the fixed header’s bottom edge. Checking only the resulting scroll offset is weaker because borders, fonts, and viewport geometry can affect pixel values.
The first section is the important limitation. At scroll position zero, the browser cannot scroll into negative space to honor the same top offset, so its heading can still be covered. Use child-level scroll margins when targets genuinely need different offsets; use a different interaction model when snap navigation is not the actual requirement.
Working example
export function snapFixture({ scrollPadding = false } = {}) {
const paddingRule = scrollPadding ? 'scroll-padding-top: 50px;' : '';
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
#scroller {
height: 300px;
width: 320px;
overflow-y: auto;
scroll-snap-type: y mandatory;
${paddingRule}
border: 1px solid #345;
}
#fixed-header {
position: fixed;
top: 0;
left: 0;
z-index: 1;
width: 320px;
height: 50px;
padding: 14px 12px;
background: #172554;
color: white;
font: 16px sans-serif;
}
section {
height: 300px;
scroll-snap-align: start;
padding: 0 12px;
font: 16px sans-serif;
}
section:nth-child(odd) { background: #dbeafe; }
section:nth-child(even) { background: #dcfce7; }
h2 { margin: 0; padding-top: 0; line-height: 50px; }
</style>
</head>
<body>
<header id="fixed-header">Fixed header (50px)</header>
<main id="scroller" tabindex="0" aria-label="Snapped sections">
<section id="first"><h2>First section</h2></section>
<section id="second"><h2>Second section</h2></section>
<section id="third"><h2>Third section</h2></section>
</main>
<script>
window.snapToSection = function snapToSection(name) {
const section = document.getElementById(name);
const scroller = document.getElementById('scroller');
if (!section || !['first', 'second', 'third'].includes(name)) {
throw new RangeError('Unknown snap target: ' + name);
}
// offsetTop includes the scroller border, while snapping is calculated from
// its scrollable padding edge. Account for both that border and the
// container-owned scroll padding before stopping short of the snap point.
const scrollPaddingTop = parseFloat(getComputedStyle(scroller).scrollPaddingTop) || 0;
const targetTop = section.offsetTop - scroller.offsetTop - scroller.clientTop - scrollPaddingTop;
// Stop slightly before the adjusted target so mandatory snapping chooses
// the container's actual snap point rather than relying on border offsets.
scroller.scrollTo({ top: targetTop - 12 });
};
</script>
</body>
</html>`;
}
Read the full article: https://chriseugenerodriguez.com/blog/scroll-snap-fixed-header-scroll-padding