Scroll Snap Carousel
JavaScript carousels used to require hundreds of lines of code: tracking touch positions, calculating velocity, animating transitions, handling keyboard events, and re-implementing behavior the browser already knows how to do. CSS scroll snap gives you all of it in two properties. The browser handles the physics, the snapping, the keyboard navigation, and the touch behavior. You describe where to stop; the browser gets there.
This page is the demo. Scroll the document normally and you will feel the browser snap to the highlighted panels between reading sections. The site header and footer stay reachable: scroll to the top or bottom of the page to reach them. Long prose sections are intentionally not snap points, so you can read without the tall-content trap that mandatory creates. Use the live controls below to change snap type, alignment, and stop behavior as you scroll. Jump to the first concept section if you prefer to read before you experiment.
Snapping the whole document is a teaching exercise, not a pattern you should copy onto a typical content site. Depending on the settings you choose in the controls, scrolling may feel clunky, jumpy, or hard to control. Real pages need careful planning: predictable section heights, room for headers and footers, and a clear reason snap improves the experience instead of fighting normal reading flow.
Scroll snap shines when the scroll container has a fixed height (or a fixed width for horizontal scrolling) and every snap child is sized to fit that box. That is why carousels, card strips, and full-viewport slide decks are the common use cases. For long-form articles and documentation, proximity snapping on a bounded carousel is usually the better fit than snapping the page itself.
Feel the Snap
Scroll slowly and release halfway between this panel and the next. With mandatory, the page lands on a snap boundary. Switch to proximity in the controls and notice the difference.
Mandatory vs. Proximity
scroll-snap-type takes two values: an axis and a strictness. The axis is x, y, or both. The strictness is mandatory or proximity.
mandatory forces the browser to snap to a boundary whenever scrolling stops, no exceptions. Release the scroll anywhere and it always jumps to the nearest snap point. The experience is consistent and predictable, which makes it ideal for carousels where every slide must be fully visible.
The danger with mandatory is tall content. If a snap section is taller than the viewport, the browser must land either at the top of that section or the top of the next one. Everything in the middle becomes permanently unreachable. Never use mandatory when content height is unpredictable. That is why only the panels on this page are snap targets, not these reading sections.
proximity is more forgiving. The browser snaps only when the scroll position lands close enough to a boundary, with the browser deciding what close enough means. You can stop mid-scroll and leave sections partially visible. Switch to proximity now and try a slow scroll to feel the difference. Then try none to disable snapping entirely.
/* Container */
.page {
scroll-snap-type: y mandatory; /* or proximity */
overflow-y: auto;
height: 100svh;
}
/* Each snap child */
.snap-panel {
scroll-snap-align: start;
}
Alignment
Switch between start, center, and end in the controls. Each value aligns a different edge of the panel to the viewport. Center is useful for media-heavy slides; start is right for most page layouts.
scroll-snap-align
The scroll-snap-align property goes on each snap child and controls which edge aligns to the scroll container. start aligns the leading edge of the child to the leading edge of the container, meaning the top of the panel aligns below the header for vertical scrolling. center centers the child in the container. end aligns the trailing edge.
Try all three using the controls now. The difference between start and center is subtle on tall panels but becomes obvious when a snap child is shorter than the viewport.
When a sticky or fixed header sits above the snap container, it eats into the visible area of each snapped section. Use scroll-padding-top on the scroll container to offset all snap positions inward so the top of each panel lands below the header. This page sets that offset to var(--site-header-bar-height) automatically.
html {
scroll-snap-type: y mandatory;
scroll-padding-top: var(--site-header-bar-height);
}
.snap-panel {
scroll-snap-align: start; /* or center, end */
}
One Panel at a Time
Try a fast swipe past this panel with snap-stop off. Momentum can skip panels entirely. Turn on snap-stop: always in the controls and try again.
scroll-snap-stop: always
By default, scroll momentum can carry past multiple snap points in a single gesture. A fast swipe may skip two or three panels and land several slides ahead. This is fine for a gallery where users want to fast-scroll to the end, but it breaks any experience that depends on sequential viewing.
scroll-snap-stop: always on each snap child forces the browser to stop at every snap point it passes, one at a time, regardless of velocity. Enable it in the controls and try a fast swipe again. The browser stops at each panel in order.
This property only works meaningfully with mandatory. With proximity the strictness is already loose enough that stop enforcement is inconsistent.
.snap-panel {
scroll-snap-align: start;
scroll-snap-stop: always; /* default is normal */
}
Native Accessibility
Arrow keys, Tab, and touch swipe all work with snap behavior and no JavaScript. Page Up and Page Down land on snap points more reliably than holding the arrow keys to scroll line by line.
Keyboard and Touch
Scroll snap is a native browser behavior. Keyboard navigation, touch swipe, and focus movement all work without additional code. This is a significant advantage over JavaScript carousels, which must re-implement all of this and frequently get accessibility wrong.
On the keyboard, Page Up and Page Down trigger snapping more reliably than holding the arrow keys. Arrow keys scroll in small increments; the browser may pass several snap points before it decides to stop. Page keys move a larger chunk of the viewport in one action, so the scroll position lands closer to a snap boundary and the snap behavior feels intentional. Try both on this page with snapping enabled.
For horizontal carousels, add overscroll-behavior-x: contain to prevent the end-of-carousel swipe from triggering the browser's back/forward navigation gesture.
For users who prefer reduced motion, the snap positioning itself is fine to keep since it is not animation-based. Guard any decorative scroll-driven animations layered on top with a prefers-reduced-motion query, but leave the snap intact.
.horizontal-carousel {
overflow-x: auto;
scroll-snap-type: x mandatory;
overscroll-behavior-x: contain;
}
@media (prefers-reduced-motion: reduce) {
/* Guard decorative animations, not the snap itself */
.slide-animation {
animation: none;
transition: none;
}
}
Build a Snap Carousel
For your portfolio demo, build a horizontal scroll-snapping card carousel using only CSS. No JavaScript. The carousel should contain at least five cards, snap one card at a time, and work correctly on both touch and keyboard. Place it at unit-4/advanced/snap-carousel.html with its own stylesheet.
Your carousel must use scroll-snap-type: x mandatory on the container, scroll-snap-align: start on each card, and overscroll-behavior-x: contain to prevent scroll chaining. Choose whether to use scroll-snap-stop: always and explain your choice in a CSS comment.
The card content is up to you: images, article previews, product tiles, or anything else that makes sense for your portfolio. Focus on the snap behavior being correct and the layout holding up at narrow viewport widths.
/* Starter structure */
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
overscroll-behavior-x: contain;
gap: var(--spacing-md);
padding-inline: var(--spacing-md);
}
.carousel-card {
flex: 0 0 clamp(260px, 80vw, 360px);
scroll-snap-align: start;
}
Hide the scrollbar visually if you want a cleaner look, but never remove it from the accessibility tree. Use scrollbar-width: none on Firefox and ::-webkit-scrollbar { display: none; } on Chromium, but make sure the carousel is still navigable by keyboard.
Teaching Video Checklist
When you record your teaching video, make sure your teammates come away understanding these concepts:
-
The two-property system:
scroll-snap-typeon the container andscroll-snap-alignon children, demonstrated on your own carousel -
The difference between
mandatoryandproximityshown live with a slow scroll and a release mid-section for each -
The tall-content trap with
mandatoryand why it makes content unreachable -
What
scroll-snap-stop: alwaysprevents, shown with a fast swipe before and after enabling it - Your horizontal card carousel on the portfolio, demonstrated on both desktop and a mobile emulator
Resources
- MDN: scroll-snap-type — Full reference for axis and strictness values
- web.dev: Well-controlled scrolling with CSS Scroll Snap — Comprehensive guide with visual diagrams for every property combination
- CSS-Tricks: Practical CSS Scroll Snapping — Real-world patterns including carousels, full-page layouts, and horizontal galleries
-
Chrome Developers: Carousels with CSS — The new
::scroll-button()and::scroll-marker()pseudo-elements in Chrome 135+ for native previous/next controls without JavaScript