Tutorials CSS Scroll Snap
Ch 55 / 60
Chapter 55 Intermediate 9 min read CSS

CSS Scroll Snap

Carousels, image galleries, and full-page sliding sections traditionally required a JavaScript library to handle "snapping" scroll position to a fixed point. CSS Scroll Snap removes that need entirely. By declaring a scroll container and marking its children as snap targets, the browser handles the snapping, momentum, and touch gestures natively — accessible, performant, and with zero JavaScript.

What you'll learn
scroll-snap-type — enabling snapping
scroll-snap-align — where items snap
mandatory vs proximity strictness
scroll-snap-stop and scroll-padding
Horizontal carousels with zero JS
Vertical full-page snap sections

scroll-snap-type — Enabling Snap

scroll-snap-type goes on the scroll container (the parent with overflow: auto/scroll). It declares which axis snaps and how strictly:

scroll-snap-type: x mandatory;
property
on the scroll container
x / y / both
which axis to snap on
mandatory / proximity
strictness of the snap
Live demo — scroll horizontally, watch each card snap into place
1 · Blue
2 · Green
3 · Orange
4 · Purple
5 · Red
6 · Yellow

scroll-snap-type: x mandatory on the track, scroll-snap-align: start on each card

CSS — the horizontal carousel above
/* ── On the SCROLL CONTAINER ── */
.snap-track {
  display:          flex;
  gap:              10px;
  overflow-x:       auto;
  scroll-snap-type: x mandatory;   /* x-axis, always snaps */
}

/* ── On each CHILD ITEM ── */
.snap-card {
  flex:               0 0 160px;
  scroll-snap-align:  start;    /* snaps to the start edge */
}

/* ── scroll-snap-type values ── */
.x-only     { scroll-snap-type: x mandatory; }   /* horizontal carousel */
.y-only     { scroll-snap-type: y mandatory; }   /* vertical full-page sections */
.both-axes  { scroll-snap-type: both mandatory; }/* 2D scroll grid, e.g. spreadsheet-like view */

/* ── Strictness: mandatory vs proximity ── */
.strict  { scroll-snap-type: x mandatory; }  /* ALWAYS lands on a snap point */
.gentle  { scroll-snap-type: x proximity; }  /* only snaps if ALREADY near a point */

scroll-snap-align — Where Items Snap

scroll-snap-align goes on each child item and decides where, within the visible viewport, that item's edge should land once snapped:

start

item's start edge aligns to container start

center

item centred in the container

end

item's end edge aligns to container end

CSS — scroll-snap-align values
/* scroll-snap-align: start | center | end | none */

.carousel-slide  { scroll-snap-align: start; }   /* most common — image galleries, carousels */
.featured-card   { scroll-snap-align: center; }  /* item centred, e.g. "now showing" highlights */
.last-step       { scroll-snap-align: end; }     /* rare — bottom-anchored content */
.no-snap-item    { scroll-snap-align: none; }    /* opt this child OUT of snapping */

/* ── Different X/Y alignment ── */
.grid-cell { scroll-snap-align: start center; }  /* x-align start, y-align center */

Vertical Full-Page Snap Sections

The same technique on the y-axis powers full-page "story" sections — scroll down and each section snaps to fill the viewport, a pattern often seen on landing pages and product showcases:

Scroll inside the box below — each section snaps fully into view
Section 1 — Intro
Section 2 — Features
Section 3 — Pricing
Section 4 — Contact
CSS — vertical full-page snap sections
/* ── Full-page snapping scroller ── */
.snap-y-track {
  height:           100vh;
  overflow-y:       auto;
  scroll-snap-type: y mandatory;
}

.snap-section {
  height:             100vh;          /* each section fills the viewport */
  scroll-snap-align:  start;
  scroll-snap-stop:   always;       /* never skip past a section on fast scroll */
  display:            flex;
  align-items:        center;
  justify-content:    center;
}

/* ── scroll-snap-stop: always vs normal ──
   normal (default): a fast/flung scroll can skip past
                      multiple snap points in one motion
   always:           forces the scroll to stop at EVERY
                      snap point one at a time, even on
                      a fast fling gesture */

scroll-padding & scroll-margin

When a sticky header or navbar overlaps the scroll container, snapped items can end up partially hidden behind it. scroll-padding (on the container) and scroll-margin (on individual items) create a buffer so snapping accounts for that overlap:

CSS — scroll-padding and scroll-margin
/* ── scroll-padding — on the SCROLL CONTAINER ── */
/* Like normal padding, but only affects WHERE snap points land,
   not the actual layout box. Useful when a sticky header
   covers part of the scroll container. */
.page {
  scroll-snap-type:    y mandatory;
  scroll-padding-top: 64px;     /* account for a 64px sticky navbar */
}

/* ── scroll-margin — on individual ITEMS ── */
/* Adds breathing room around a SPECIFIC item's snap point,
   without affecting layout spacing between items. */
.snap-card {
  scroll-margin-left: 12px;   /* item snaps 12px from the edge, not flush */
}

/* ── Common real-world combo: fixed nav + snap sections ── */
html {
  scroll-snap-type:    y mandatory;
  scroll-padding-top: var(--nav-height);
}
section {
  scroll-snap-align: start;
  min-height:        100vh;
}

Real-World Patterns

CSS — production scroll-snap patterns
/* ── 1. Image gallery / product carousel ── */
.gallery {
  display:          flex;
  gap:              1rem;
  overflow-x:       auto;
  scroll-snap-type: x mandatory;
  padding-inline:   1rem;
}
.gallery img {
  scroll-snap-align: center;
  flex:              0 0 80%;     /* peek next/prev slightly */
}

/* ── 2. Card carousel with "show next" peek ── */
.cards-row {
  display:          flex;
  gap:              1rem;
  overflow-x:       auto;
  scroll-snap-type: x proximity;  /* gentler — free scroll until close */
}
.card { flex: 0 0 280px; scroll-snap-align: start; }

/* ── 3. Mobile-friendly tab/segment switcher ── */
.tabs-track {
  display:          flex;
  overflow-x:       auto;
  scroll-snap-type: x mandatory;
  scroll-snap-stop: always;       /* always land on exactly one tab */
}

/* ── 4. Hide native scrollbar but keep scroll-snap working ── */
.snap-track {
  scrollbar-width: none;          /* Firefox */
}
.snap-track::-webkit-scrollbar { display: none; }  /* Chrome/Safari */

/* ── 5. Smooth scroll behaviour alongside snap ── */
html { scroll-behavior: smooth; }  /* animates jumps to #anchor links too */

Scroll Snap Reference

Property Goes on Purpose
scroll-snap-type Scroll container Enables snapping on x/y/both axis with mandatory/proximity strictness
scroll-snap-align Each child item Where the item aligns within the container when snapped: start/center/end
scroll-snap-stop Each child item always forces stopping at every snap point; normal allows skipping on fast scroll
scroll-padding Scroll container Offsets where snap points land, accounting for sticky headers
scroll-margin Each child item Adds breathing room around one item's specific snap point
scroll-behavior Any scrollable element smooth animates programmatic/anchor scrolls; doesn't affect snap itself
Free Resource
CSS3 Cheat Sheet — Scroll Snap & Carousels
Complete reference covering scroll-snap-type, scroll-snap-align and JS-free carousel patterns.
Download PDF

Try It Yourself

A horizontal product carousel and a vertical full-page snap demo, both pure CSS:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 55

Questions & Answers
These two properties work as a pair across the parent/child relationship:

scroll-snap-type goes on the scroll container — the element with overflow: auto or overflow: scroll. It turns snapping on for that container and specifies the axis (x, y, or both) plus strictness (mandatory or proximity).

scroll-snap-align goes on each child item inside that container. It defines where, within the visible viewport, that specific item's reference edge should land once the browser decides to snap — start, center, or end.

Without scroll-snap-type on the container, scroll-snap-align on children does nothing — snapping has to be enabled at the container level first. The two properties only work together, never independently.
Both are values for the strictness portion of scroll-snap-type (e.g. x mandatory vs x proximity):

mandatory: the browser ALWAYS snaps to the nearest snap point once scrolling momentum settles, no matter how far the scroll position is from that point. This guarantees the user never ends up stuck "between" two items — ideal for carousels and full-page sections where a half-visible state looks broken.

proximity: the browser only snaps if the scroll position is already reasonably close to a snap point when scrolling stops. If you stop scrolling somewhere in the middle, far from any snap point, it simply stays there — no forced jump. This feels gentler and is preferred for things like card rows where you want some freedom to browse without being forced into a position.

Most carousels and galleries use mandatory; card rows that double as free-scrolling content often use proximity.
By default (scroll-snap-stop: normal), a fast scroll gesture — a quick swipe or flick — can carry enough momentum to skip past several snap points in one continuous motion, landing several items further than where the user's finger or scroll wheel actually stopped.

For a photo gallery, skipping a few thumbnails might be fine. But for full-page "story" sections, or a tab switcher where the user expects to move exactly one item per gesture, skipping ahead breaks the experience — content the user meant to see gets flown past entirely.

scroll-snap-stop: always forces the scroll to halt at every single snap point in sequence, even during a fast fling — guaranteeing the user passes through each item one at a time rather than skating past several in a single gesture. This is essential for narrative full-page sections and any UI where every step must be seen.
A sticky/fixed header sits visually on top of the page but doesn't take up space in the normal document flow that the browser's snap calculations are aware of by default. Without any adjustment, a scroll-snapped section can end up positioned so its top portion is hidden directly underneath the sticky header — the snap calculation thinks the very top of the viewport is available space, but visually it's covered.

scroll-padding-top: 64px (matching your header's height) tells the browser's snap algorithm to treat the top 64px of the scroll container as unavailable for landing snap points — effectively shrinking the usable viewport for snap purposes to exclude the header's footprint. The section then snaps to a position that's fully visible below the header, instead of partially obscured beneath it.

This is exactly analogous to how scroll-margin-top on an anchor target prevents a sticky header from covering content when jumping to an #anchor link.
For the core "snap to each slide" mechanics — touch swipe, mouse drag-scroll, keyboard arrow scrolling, and momentum — CSS scroll snap handles all of it natively, with better performance and accessibility than most JavaScript reimplementations, since it uses the browser's own native scrolling and input handling.

What pure CSS scroll snap does NOT provide out of the box:
— Visible navigation dots or arrow buttons: these are just regular HTML/CSS elements you build yourself, optionally wired to JavaScript that calls scrollTo() or sets scrollLeft to jump to a specific slide
— Auto-play / autoadvance: requires a small JavaScript timer calling a scroll method periodically
— "Current slide" indicator state: requires JavaScript using the IntersectionObserver API or scroll event listeners to detect which slide is currently snapped into view

For a large portion of carousel use cases — especially simple image galleries or card rows — scroll snap alone is sufficient. More feature-rich carousels (autoplay, dot indicators, infinite loop) typically still pair scroll-snap CSS with a thin layer of JavaScript for the extra UI chrome, while letting CSS handle all the actual scrolling and snapping physics.
Multiple Choice Questions
Question 1 of 5
Which element should have scroll-snap-type applied?
Question 2 of 5
What's the difference between scroll-snap-type: x mandatory and x proximity?
Question 3 of 5
What does scroll-snap-align: center do?
Question 4 of 5
Why might you add scroll-snap-stop: always to a full-page section scroller?
Question 5 of 5
What is scroll-padding-top used for when a sticky navbar is present?
Chapter Summary
  • scroll-snap-type: goes on the scroll container. x | y | both for axis, plus mandatory | proximity for strictness.
  • scroll-snap-align: goes on each child item. start | center | end defines where the item lands within the viewport when snapped.
  • mandatory vs proximity: mandatory always snaps to the nearest point; proximity only snaps when already close, allowing free scrolling otherwise.
  • scroll-snap-stop: always: forces the scroll to stop at every snap point sequentially, preventing a fast fling from skipping multiple items in one motion.
  • scroll-padding (container) and scroll-margin (item): create buffer space so snapping accounts for sticky headers or desired breathing room around specific items.
  • Horizontal carousels: display: flex; overflow-x: auto; scroll-snap-type: x mandatory on container, flex: 0 0 Npx; scroll-snap-align: start on items.
  • Vertical full-page sections: same pattern on the y-axis with height: 100vh sections — a common landing-page pattern.
  • What scroll-snap doesn't include: navigation dots, arrow buttons, autoplay, and "current slide" indicators still require custom HTML/CSS and optionally a little JavaScript — scroll-snap only handles the scrolling physics itself.
~9 min read
Intermediate level
Chapter 55 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel