css - CSS Scroll Snap
CSS Scroll Snap is a modern CSS feature that allows developers to create controlled scrolling experiences where content automatically “snaps” into position after the user scrolls. It is commonly used in image sliders, carousels, onboarding screens, product showcases, fullscreen websites, and mobile applications.
Before Scroll Snap existed, developers relied heavily on JavaScript libraries to create smooth snapping effects. CSS Scroll Snap provides a native browser-based solution that is faster, cleaner, and easier to maintain.
Why CSS Scroll Snap is Important
Normal scrolling allows users to stop at any arbitrary point on the page. While this is suitable for regular documents, some interfaces benefit from controlled positioning.
Examples include:
-
Horizontal image galleries
-
Fullscreen sections
-
Slide-based presentations
-
Product cards
-
Story-like mobile interfaces
-
Media browsing layouts
CSS Scroll Snap improves user experience by ensuring elements align properly after scrolling ends.
Basic Concept
CSS Scroll Snap works using two main parts:
-
Scroll Container
-
Snap Elements
The scroll container defines the scrolling behavior, while child elements define where snapping should occur.
Core Properties of CSS Scroll Snap
1. scroll-snap-type
This property is applied to the scrolling container.
It specifies:
-
The scrolling direction
-
The snapping behavior
Syntax
scroll-snap-type: x mandatory;
Values
| Value | Description |
|---|---|
| x | Horizontal snapping |
| y | Vertical snapping |
| both | Both directions |
| mandatory | Browser must snap |
| proximity | Snap only if close |
Example
.container {
scroll-snap-type: x mandatory;
}
This creates mandatory horizontal snapping.
Creating a Horizontal Scroll Snap Layout
HTML
<div class="container">
<div class="item">Slide 1</div>
<div class="item">Slide 2</div>
<div class="item">Slide 3</div>
</div>
CSS
.container {
display: flex;
overflow-x: scroll;
scroll-snap-type: x mandatory;
width: 100%;
}
.item {
min-width: 100%;
height: 300px;
scroll-snap-align: start;
}
Explanation
-
overflow-x: scrollenables horizontal scrolling. -
scroll-snap-type: x mandatoryenables snapping horizontally. -
scroll-snap-align: starttells each item where snapping should occur. -
min-width: 100%ensures one slide fills the screen.
2. scroll-snap-align
This property is applied to child elements.
It defines where the element snaps inside the container.
Values
| Value | Description |
|---|---|
| start | Snap starts at beginning |
| center | Snap at center |
| end | Snap at end |
Example
.item {
scroll-snap-align: center;
}
The element will align at the center during snapping.
Vertical Scroll Snap
CSS Scroll Snap also supports vertical layouts.
Example
.container {
height: 100vh;
overflow-y: scroll;
scroll-snap-type: y mandatory;
}
.section {
height: 100vh;
scroll-snap-align: start;
}
This creates fullscreen vertically snapping sections.
3. scroll-padding
Sometimes fixed headers hide snapped content.scroll-padding creates extra space inside the container.
Example
.container {
scroll-padding-top: 80px;
}
If a navbar height is 80px, the snapped content remains visible below it.
4. scroll-margin
This property adds spacing to individual snap items.
Example
.item {
scroll-margin-left: 20px;
}
It adjusts the snapping position of the item.
Mandatory vs Proximity
Mandatory
scroll-snap-type: x mandatory;
The browser must snap to an item.
Proximity
scroll-snap-type: x proximity;
The browser snaps only when scrolling stops near a snap point.
Difference
| Feature | Mandatory | Proximity |
|---|---|---|
| Strict snapping | Yes | No |
| More controlled | Yes | Moderate |
| More natural feel | No | Yes |
Smooth Scrolling with Scroll Snap
You can combine snapping with smooth scrolling.
Example
html {
scroll-behavior: smooth;
}
This creates smooth transitions between snapped sections.
Fullscreen Website Example
HTML
<div class="container">
<section class="page">Home</section>
<section class="page">About</section>
<section class="page">Services</section>
<section class="page">Contact</section>
</div>
CSS
.container {
height: 100vh;
overflow-y: scroll;
scroll-snap-type: y mandatory;
}
.page {
height: 100vh;
scroll-snap-align: start;
display: flex;
justify-content: center;
align-items: center;
font-size: 40px;
}
This creates a modern section-based scrolling website.
Scroll Snap with Image Carousel
HTML
<div class="gallery">
<img src="1.jpg">
<img src="2.jpg">
<img src="3.jpg">
</div>
CSS
.gallery {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.gallery img {
width: 100%;
scroll-snap-align: center;
}
This creates a snapping image gallery without JavaScript.
Browser Support
CSS Scroll Snap is supported in modern browsers:
-
Google Chrome
-
Firefox
-
Safari
-
Microsoft Edge
Older browsers may require fallback behavior.
Advantages of CSS Scroll Snap
1. Less JavaScript
Most snapping interfaces can be built entirely with CSS.
2. Better Performance
Native browser rendering is faster than JavaScript-based animations.
3. Mobile Friendly
Works especially well on touch devices.
4. Cleaner Code
Simplifies slider and scrolling implementations.
5. Better User Experience
Provides structured and predictable navigation.
Limitations
1. Limited Custom Animation Control
Scroll Snap controls positioning, not advanced animations.
2. Browser Differences
Some browsers may behave slightly differently with momentum scrolling.
3. Accessibility Concerns
Overly aggressive snapping may frustrate some users.
Developers should avoid forcing snapping in long reading layouts.
Best Practices
Use Proximity for Reading Interfaces
scroll-snap-type: y proximity;
This feels more natural.
Use Mandatory for Slideshows
scroll-snap-type: x mandatory;
This ensures precise snapping.
Avoid Excessive Snapping
Too much snapping can reduce usability.
Test on Mobile Devices
Touch behavior varies between devices.
Real-World Applications
Streaming Platforms
Media cards snap horizontally.
Social Media Stories
Each story snaps into place.
Ecommerce Websites
Product sections snap during browsing.
Portfolio Websites
Fullscreen sections transition smoothly.
Presentation Websites
Slides snap like PowerPoint pages.
Combining Scroll Snap with Other CSS Features
CSS Scroll Snap works well with:
-
Flexbox
-
CSS Grid
-
Smooth scrolling
-
CSS animations
-
Sticky positioning
Example Using Flexbox
.container {
display: flex;
overflow-x: scroll;
scroll-snap-type: x mandatory;
}
.card {
flex: 0 0 100%;
scroll-snap-align: start;
}
Example Using Grid
.container {
display: grid;
grid-auto-flow: column;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
Future of Scroll Snap
CSS Scroll Snap is becoming increasingly important in modern UI design, especially for mobile-first applications and immersive interfaces.
As browsers improve support and developers combine it with newer CSS APIs, Scroll Snap will continue replacing many JavaScript-based slider systems.
Conclusion
CSS Scroll Snap is a powerful feature that enables smooth, structured, and controlled scrolling experiences using pure CSS. It simplifies the creation of carousels, fullscreen layouts, galleries, and modern mobile interfaces without depending heavily on JavaScript.
By understanding properties such as scroll-snap-type, scroll-snap-align, scroll-padding, and scroll-margin, developers can build responsive and highly interactive scrolling layouts efficiently.