Lesson 13 of 30
Carousels and image galleries used to mean a JS library computing scroll offsets and animating to them by hand. Scroll snap gets the 'snap to the next item' behavior natively, for free, from the browser's own scrolling.
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
Which two properties, and on which elements, implement CSS scroll snap?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A carousel or paginated gallery needs one core behavior: when you stop scrolling or swiping, the viewport settles cleanly on an item boundary instead of stopping halfway between two items. Historically that meant a JS carousel library: listen for scroll or touch events, calculate which item is closest, then animate the scroll position to line it up exactly. That's real work to get right, and it fights the browser's own native scrolling and momentum instead of using it.
Scroll snap is two properties working together, no JavaScript required:
scroll-snap-type, set on the scroll container — turns snapping on for that axis (x, y, or both) and picks the strictness.scroll-snap-align, set on the children — marks where each child should align to when it becomes the snap target (start, center, or end).The browser handles the rest: native touch momentum, native scrollbar dragging, native keyboard scrolling, all snapping to item boundaries automatically, using the same scrolling physics the rest of the page already has.
.gallery {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.gallery img {
scroll-snap-align: center;
flex: 0 0 80%;
}
.gallery is a horizontally scrolling flex container; because of scroll-snap-type: x mandatory, any scroll gesture along the x-axis will always come to rest with one image centered, never stopped between two. Swap mandatory for proximity and the browser only snaps when the scroll naturally comes to rest near a snap point — scroll a long way past several images quickly and it won't yank you back to the nearest one, but a gentle stop close to an image boundary still snaps there.
scroll-snap-type/scroll-snap-align pairing — it's safe to reach for as the default mechanism for this kind of UI rather than a JS library.scrollTo/scrollIntoView, plus appropriate ARIA if it's presented as a carousel widget.mandatory can trap keyboard and assistive-tech scrolling in surprising ways if items are wider than the viewport or spacing is uneven — always test actual keyboard scrolling (arrow keys, Page Down) and not just mouse/touch drag.overflow-x: auto or similar) — scroll snap properties do nothing on an element that isn't scrolling in the first place."How would you build an image carousel without a JS library?" is a real question that tests whether a candidate knows scroll snap exists as the native mechanism, and — just as important — whether they know it isn't the whole answer, since accessible carousel controls still need to be built on top of it.