CSS Scroll Snap
CSS Scroll Snap (scroll-snap-type on the container, scroll-snap-align on children) lets the Compositor Thread handle carousel and section snapping natively. It respects touch inertia and keyboard navigation without JS scroll timers fighting the user's finger.
Mandatory vs Proximity Scroll Snap
Test how snap-type, snap-stop, and scroll-padding affect rest positions.
Under the Hood
A JS carousel hijacks the wheel and fights momentum; CSS Scroll Snap lets the native physics engine predict where a fling will land and gently magnetizes it to the nearest snap line.
- Snap Area Registration: During Render Tree & Layout, each child's
scroll-snap-align(start,center,end) registers a target coordinate. - Strictness:
mandatoryalways snaps to a target after scrolling stops;proximitysnaps only when the rest point lands near a target. - Padding Offset:
scroll-padding-inlineinsets the snapport so sticky headers or peek gutters never overlap the snapped card.
.carousel {
display: flex;
gap: 1rem;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-padding-inline: 1rem;
}
.carousel > .slide {
flex: 0 0 85%;
scroll-snap-align: start;
scroll-snap-stop: always;
}Works seamlessly with Dynamic Viewport Units and CSS Logical Properties.
Problem Genealogy
- 2012–2018: Heavy JS carousel libraries reimplemented touch physics, breaking trackpad inertia, accessibility focus, and
Ctrl+Fscrolling. - 2019+: CSS Scroll Snap Level 1 unified snap behavior across Chromium, Gecko, and WebKit.
Production Pitfalls
- Trapping tall content with
mandatory: Usingy mandatoryon sections taller than the viewport prevents users from reading the bottom half of a section. - Skipping slides on fast swipes: Without
scroll-snap-stop: always, a fast swipe jumps across multiple slides at once.
- When not to use
scroll-snap-type: y mandatory: Never applymandatoryvertical snapping to long-form articles or variable-height pages; useproximityor omit it.
Connections & Logical Next Steps
Scroll snap positions derive from laid-out child snap-area boxes and scroll-padding.
Scroll snap aligns scrollport rest positions, whereas dynamic viewport units resize snapports as mobile toolbars animate.