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.
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:
on the scroll container
which axis to snap on
strictness of the snap
scroll-snap-type: x mandatory on the track, scroll-snap-align: start on each card
/* ── 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:
item's start edge aligns to container start
item centred in the container
item's end edge aligns to container end
/* 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:
/* ── 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:
/* ── 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
/* ── 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 |
Try It Yourself
A horizontal product carousel and a vertical full-page snap demo, both pure CSS:
Quick Check — Chapter 55
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.
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.
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.
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.
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.
-
scroll-snap-type: goes on the scroll container.
x | y | bothfor axis, plusmandatory | proximityfor strictness. -
scroll-snap-align: goes on each child item.
start | center | enddefines 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 mandatoryon container,flex: 0 0 Npx; scroll-snap-align: starton items. -
Vertical full-page sections: same pattern on
the y-axis with
height: 100vhsections — 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.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.