Lesson 33 of 50
cssCSS Scroll‑Snap
6 mins
Course progress66%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 33 of 50
css6 mins
Keep these ideas in mind as you work through the lesson.
Scroll‑snap gives you native carousel‑like behavior without any JavaScript. The browser automatically stops scrolling at defined snap points.
| Property | Description |
|---|---|
scroll-snap-type | Sets the axis (x, y, or both) and the strictness (mandatory vs proximity). |
scroll-snap-align | Determines which part of the child element aligns to the container’s snap point (start, center, end). |
scroll-snap-stop | normal (default) or always – forces the scroll to always stop at each snap point. |
<div class="scroller">
<img src="1.jpg" class="item" />
<img src="2.jpg" class="item" />
<img src="3.jpg" class="item" />
</div>
.scroller {
display:flex;
overflow-x:auto;
scroll-snap-type:x mandatory;
gap:1rem;
padding:1rem;
}
.item {
flex:0 0 80%; /* each image takes 80% of viewport */
scroll-snap-align:start;
border-radius:8px;
}
Users can swipe through images; the browser snaps each image to the start edge.
scroll-behavior:smooth; for a gentle glide.scroll-snap-stop:always; if you want hard stops on each item.tabindex="0").prefers-reduced-motion – you may want to disable auto‑scroll or snap animation for users who prefer minimal motion.All modern browsers (Chrome, Edge, Safari, Firefox) support scroll-snap. For older browsers, a simple overflow:auto still works, albeit without snapping.