CSS Scroll Snap: A Comprehensive Guide to Creating Smooth Scrollable Containers
This article explains the CSS Scroll Snap feature, covering its purpose, basic setup, key properties such as scroll‑snap‑type, scroll‑snap‑align, scroll‑snap‑stop, scroll‑padding, and scroll‑margin, and demonstrates practical use cases with code examples for building responsive scrollable interfaces.
CSS Scroll Snap allows developers to create smooth, controllable scrolling experiences without relying on JavaScript plugins. It is especially useful for mobile and tablet interfaces where users swipe to view more content, such as image galleries or friend lists.
Why Use CSS Scroll Snap
Scroll Snap enhances user experience by providing native, easy-to‑implement scrolling behavior, eliminating the need for complex JavaScript solutions.
Basics of a Scroll Container
To build a scrollable container you need to set overflow and arrange items inline (e.g., using Flexbox).
<div class="section">
<div class="section__item">Item 1</div>
<div class="section__item">Item 2</div>
<div class="section__item">Item 3</div>
<div class="section__item">Item 4</div>
<div class="section__item">Item 5</div>
</div> .section {
white-space: nowrap;
overflow-x: auto;
}Modern implementations prefer Flexbox:
.section {
display: flex;
overflow-x: auto;
}Problems with Simple Scrolling
Without Snap, scrolling can feel imprecise, especially on touch devices, as items may stop between logical positions.
CSS Scroll Snap Introduction
Apply scroll-snap-type to the container and scroll-snap-align to its children.
.section {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.section__item {
scroll-snap-align: start;
}The mandatory keyword forces the browser to stop at each snap point, while proximity (the default) allows a softer snap.
Scroll Snap Type
The scroll-snap-type property defines the axis ( x for horizontal, y for vertical) and the strictness ( mandatory or proximity ).
/* Horizontal */
.section {
display: flex;
overflow-x: auto;
scroll-snap-type: x;
}
/* Vertical */
.section {
height: 250px;
overflow-y: auto;
scroll-snap-type: y;
}Scroll Snapping Alignment
Children can align to start , center , or end snap points.
Using Scroll‑Snap‑Stop
To prevent skipping items when scrolling fast, set scroll-snap-stop: always on each child.
.section__item {
scroll-snap-align: start;
scroll-snap-stop: always;
}Scroll Snap Padding
scroll-padding adds padding inside the container, shifting the snap positions.
.section {
overflow-y: auto;
scroll-snap-type: y mandatory;
scroll-padding: 50px 0 0 0;
}Scroll Snap Margin
scroll-margin defines space between items that influences where they snap.
.item-2 {
scroll-margin-left: 20px;
}Practical Use Cases
Image List
.images-list {
display: flex;
overflow-x: auto;
scroll-snap-type: x;
gap: 1rem;
-webkit-overflow-scrolling: touch;
}
.images-list img {
scroll-snap-align: start;
}Friend List
.list {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
gap: 1rem;
scroll-padding: 48px;
padding-bottom: 32px;
-webkit-overflow-scrolling: touch;
}
.list-item {
scroll-snap-align: start;
}Avatar List (center alignment)
.list {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
-webkit-overflow-scrolling: touch;
}
.list-item {
scroll-snap-align: center;
}Full‑Screen Vertical Scrolling
<main>
<section class="section section-1"></section>
<section class="section section-2"></section>
<section class="section section-3"></section>
<section class="section section-4"></section>
<section class="section section-5"></section>
</main> main {
height: 100vh;
overflow-y: auto;
scroll-snap-type: y mandatory;
-webkit-overflow-scrolling: touch;
}
.section {
height: 100vh;
scroll-snap-align: start;
}Block and Inline Scroll Snap Types
Both inline and block values are valid for scroll-snap-type .
main {
scroll-snap-type: inline mandatory;
}Accessibility Considerations
Ensure that scroll snapping does not hinder users from reading content; avoid overly restrictive snapping that blocks free scrolling.
.wrapper {
scroll-snap-type: y mandatory;
}
h2 {
scroll-snap-align: start;
}Conclusion
The CSS Scroll Snap module provides a powerful, native way to create controlled scrolling experiences across devices, reducing reliance on JavaScript and improving performance and accessibility.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.