CSS Scroll Snap: Building Zero-JavaScript Touch Carousels & Sliders
CSS Scroll Snap locks a scroll container's viewport to discrete child positions after a swipe or scroll. To build a zero-JavaScript carousel, set scroll-snap-type: x mandatory and overflow-x: auto on the parent track, and apply scroll-snap-align: center (or start) to each child slide. Adding scroll-snap-stop: always forces the scroller to stop at every individual slide, preventing multi-card overshooting.
Step 1: The Parent Container: scroll-snap-type & Overflow
Define your scroll container with overflow-x: auto and enable snapping via scroll-snap-type. The first argument specifies the axis (x, y, or both), and the second specifies strictness (mandatory or proximity).
.snap-carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
gap: 16px;
padding: 16px;
/* Hide scrollbar while preserving swipe */
scrollbar-width: none;
-webkit-overflow-scrolling: touch;
}
.snap-carousel::-webkit-scrollbar {
display: none;
}
Step 2: Child Alignment: scroll-snap-align & snap-stop
Each child card defines where it locks relative to the viewport: scroll-snap-align: start, center, or end. Add scroll-snap-stop: always to ensure vigorous swipes don't skip over cards.
.snap-card {
flex: 0 0 85%; /* Shows a preview of next card */
scroll-snap-align: center;
scroll-snap-stop: always;
border-radius: 12px;
background: var(--surface);
border: 1px solid var(--border);
}
@media (min-width: 768px) {
.snap-card {
flex: 0 0 calc(33.333% - 16px);
}
}
Step 3: Accounting for Sticky Headers with scroll-padding
If your site has a sticky navigation bar or fixed floating sidebar, snapped cards may get hidden underneath it. Set scroll-padding-top or scroll-padding-inline on the scroller to offset the snap boundary.
.snap-vertical-feed {
height: 100vh;
overflow-y: auto;
scroll-snap-type: y mandatory;
/* Leaves 80px clearance for fixed top navbar */
scroll-padding-top: 80px;
}
Step 4: Linking Pagination Dots with IntersectionObserver
While the carousel requires zero JavaScript to slide, you can optionally light up active pagination indicators using a lightweight IntersectionObserver.
const cards = document.querySelectorAll('.snap-card');
const dots = document.querySelectorAll('.pagination-dot');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const index = Array.from(cards).indexOf(entry.target);
dots.forEach((dot, i) => dot.classList.toggle('active', i === index));
}
});
}, { threshold: 0.6 });
cards.forEach(card => observer.observe(card));
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Whenever I design responsive widgets or fluid typography, I rely heavily on clamp() and container queries (@container). Testing down to 320px viewport widths in Chrome DevTools will instantly reveal edge cases before your users see them.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What is the difference between mandatory and proximity in scroll-snap-type?
`mandatory` strictly forces the container to snap to a rest position every time scrolling ceases. `proximity` only snaps if the user stops scrolling close to a snap point, allowing free scrolling otherwise.
What does scroll-snap-stop: always do?
By default (`normal`), a rapid flick can sail past multiple slides. Setting `scroll-snap-stop: always` forces the browser to catch and stop at the very next slide, ensuring users don't accidentally skip content.
How do I hide the horizontal scrollbar across all major browsers?
Combine `scrollbar-width: none` (Firefox & modern standards) with `::-webkit-scrollbar { display: none; }` (Chrome, Safari, Edge).
Does CSS Scroll Snap work on vertical page feeds like TikTok or Instagram Reels?
Yes! Declare `scroll-snap-type: y mandatory` on a full-height container (`height: 100vh; overflow-y: scroll`) with each slide set to `height: 100vh; scroll-snap-align: start`.
Can users navigate CSS scroll snap carousels using keyboard arrow keys?
Yes, provided the container has `tabindex="0"`. When focused, pressing Left and Right arrow keys will smoothly snap to the adjacent cards.