Swipe through product photos in a shopping app. As soon as you lift your finger, the row of photos keeps sliding by itself and stops exactly on the next photo. It never stops halfway, with only half a photo showing.
That effect is called scroll snap: a scrolling area always stops at set points (called snap points), not just anywhere. It only takes CSS, no JavaScript.
A gallery that stops neatly
This example has a gallery with three slides, then the CSS:
<div class="gallery">
<div class="slide">Slide 1</div>
<div class="slide">Slide 2</div>
<div class="slide">Slide 3</div>
</div>
<style>
.gallery {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.slide {
flex: 0 0 70%;
scroll-snap-align: start;
}
</style>
.galleryis the box that scrolls.display: flexputs the slides side by side, andoverflow-x: autolets the gallery scroll sideways.scroll-snap-type: x mandatoryon.galleryturns snapping on.xmeans the horizontal axis (sideways).mandatorymeans it must snap: every time the scrolling stops, the gallery always moves onto a snap point.flex: 0 0 70%makes each slide 70% as wide as the gallery, and stops it from shrinking.scroll-snap-align: starton each slide sets its snap point: the slide's starting (left) edge lines up with the gallery's left edge.
The result: when you swipe the gallery a little and let go, it slides on by itself until a slide sits exactly at the left edge.
Notice that snapping needs two sides. The box that scrolls gets scroll-snap-type, and each of its children gets scroll-snap-align. If one of the two properties is missing, snapping does not work.
The values to choose from
scroll-snap-type, written on the box that scrolls:
xfor scrolling sideways,yfor scrolling down,bothfor both directions.mandatory: always moves onto a snap point.proximity: only does so when the scrolling stops close to a snap point.
scroll-snap-align, written on each child:
start: the child's starting edge lines up with the box's starting edge. The starting edge is the left edge forx, and the top edge fory.center: the child's centre lines up with the box's centre.end: the child's ending edge lines up with the box's ending edge.
Snapping downwards
These rules make a list of posts that scrolls down:
.feed {
height: 300px;
overflow-y: auto;
scroll-snap-type: y mandatory;
}
.post {
height: 300px;
scroll-snap-align: center;
}
It works the same way as the gallery. The differences: the axis is y, and the box uses overflow-y: auto. The list scrolls down, and each .post stops exactly in the middle of .feed. Each .post is as tall as .feed, so the result is like a video feed: one swipe moves you one video.
⚠️ Why won't the slides stop in the right place?
- The properties swapped.
scroll-snap-alignon the box that scrolls, orscroll-snap-typeon a slide, has no effect. - The wrong axis. A gallery that scrolls sideways needs
x.y mandatoryon a horizontal gallery has no effect. - A box that cannot scroll. A box only scrolls when it has
overflow: autoand content longer than the box. If either one is missing, nothing moves, so nothing snaps. - A sticky header covering slides.
scroll-padding-top: 60pxon the box that scrolls moves the snap point down by that amount. Use the header's height as the value.
TL;DR: Scroll snap makes scrolling always stop at set points (snap points). The box that scrolls needs overflow: auto and scroll-snap-type, for example x mandatory for scrolling sideways or y mandatory for scrolling down. Each of its children needs scroll-snap-align, which sets the part of the child that lines up with the box: start (the starting edge: left or top), center (the centre) or end (the ending edge). mandatory always moves onto a snap point, and proximity only does so when the scrolling stops close to one.
⚡ Turn it on for the box that scrolls
Write:
.gallery {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
Meaning: .gallery is a gallery that holds several slides. display: flex puts the slides side by side, and overflow-x: auto lets the gallery scroll sideways. scroll-snap-type: x mandatory turns scroll snap on. Together with the next step, the scrolling always stops exactly on one slide, not between two slides. x means the horizontal axis (sideways), and mandatory means it must: every time the scrolling stops, the gallery moves onto a slide.
⚡ A snap point on each child
Write:
.slide {
flex: 0 0 70%;
scroll-snap-align: start;
}
Guess first: the gallery has scroll-snap-type, but the slides have no scroll-snap-align. Does snapping work?
No. Snapping needs two sides: the box that scrolls gets scroll-snap-type, and each child gets scroll-snap-align.
Meaning: flex: 0 0 70% makes each slide 70% as wide as the gallery. scroll-snap-align sets the part of the slide that lines up with the gallery. start lines the slide's left edge up with the gallery's left edge. There are also center (the centre) and end (the right edge).
⚡ Downwards works too
Meaning: To scroll down, change the axis to y (so y mandatory) and use overflow-y: auto. Then a list of posts that scrolls down stops exactly on each post, like a video feed: one swipe moves you one video.