A scroll-snap layout can select the correct section and still feel broken: the section heading lands beneath a fixed header. The snap rule is not necessarily wrong. The scrolling container simply has no knowledge that part of the visible area is occupied by persistent chrome.
The implementation decision is to reserve that shared space on the scroll container with scroll-padding-top, using the fixed header’s height. This changes the container’s effective snapport: when a section snaps to start, the browser treats the area below the header as the useful top edge. The section can retain its existing scroll-snap-align: start, and the container can retain mandatory vertical snapping.
That separation matters. Child alignment describes where a target aligns; scroll padding describes the usable viewing region in which it should become visible. Adding spacing to every section may appear to work, but it changes content geometry and repeats a viewport-level concern in each target. For a header shared by all targets, the scroller owns the offset.
The expected result is that later snapped headings appear below the fixed header without weakening snap strictness or moving heading content inside each section. A useful verification checks the geometric relationship, not only a scroll offset: after snapping, the heading’s top edge should be at or below the header’s bottom edge. It should also confirm that the original snap type and child alignment remain in place.
There is an important boundary case. The first target begins at scroll position zero, so a top padding offset cannot create negative scroll space above it. It may still sit beneath a fixed header. If targets need different individual offsets rather than one shared overlay allowance, child-level scroll-margin is the more appropriate tool. And if the interaction requires arbitrary pixel-perfect positioning rather than navigation through snap targets, reconsider whether scroll snap fits the interaction.
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
Top comments (0)