Scroll Snap — CSS

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…

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>

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:

scroll-snap-align, written on each child:

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?

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.

What you will learn