Keep CSS scroll snap targets visible below a fixed header
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…
Scroll snap layouts often appear to fail when the section heading appears beneath a fixed header. The issue arises because the scrolling container does not recognize that the persistent chrome, such as a fixed header, occupies part of the visible area. To resolve this, developers can reserve space on the scrolling container using scroll-padding-top, setting it to the height of the fixed header.
This alters the container's effective snapport, making the area below the header the usable top edge for snap alignment. By maintaining the section's existing scroll-snap-align: start and the container's mandatory vertical snapping, the layout remains consistent. The key difference lies in how child alignment and scroll padding are defined.
Adding padding to each section may seem like a solution, but it changes the content geometry and reiterates viewport-level concerns in each target. Instead, the scroller (fixed header) owns the offset, ensuring that later snapped headings appear below the fixed header without compromising strictness or altering heading content within each section.
Verification should focus on the geometric relationship between the heading and header, checking that the heading's top edge is at or below the header's bottom edge after snapping. This approach also confirms that the original snap type and child alignment are preserved. A notable exception occurs with the first target, which starts at scroll position zero.
In this case, a top padding offset cannot create negative scroll space above it, potentially placing it beneath the fixed header. For scenarios requiring distinct individual offsets rather than a shared overlay allowance, child-level scroll-margin is more appropriate. Additionally, if the interaction demands pixel-perfect positioning rather than navigation through snap targets, it may be more suitable to reconsider the use of scroll snap.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.