Horizontal scroll snap carousel in CSS
30 A rail that snaps
hardBuild a horizontal rail of cards that comes to rest neatly on a card rather than half way between two. Keep it usable by keyboard and know why mandatory is a risk.
Stuck? Three hints, smallest first
- 1.
scroll-snap-type: x mandatorygoes on the scrolling container. - 2.
scroll-snap-align: startgoes on each child, and says which edge lines up. - 3.
mandatoryalways snaps, which can make content unreachable if an item is larger than the scrollport.proximityonly snaps when you are already close.
<div class="rail" tabindex="0" role="region" aria-label="Featured routes">
<article class="slide"><h3>Abuja</h3><p>12 hours, one stop</p></article>
<article class="slide"><h3>Kano</h3><p>18 hours, two stops</p></article>
<article class="slide"><h3>Enugu</h3><p>9 hours, direct</p></article>
<article class="slide"><h3>Ibadan</h3><p>2 hours, direct</p></article>
</div>
.rail {
display: flex;
gap: 10px;
padding: 4px;
overflow-x: auto;
scroll-snap-type: x mandatory;
/* So a snapped card is not flush against the container edge. */
scroll-padding-left: 4px;
/* Keeps a swipe from scrolling the page behind once the rail runs out. */
overscroll-behavior-x: contain;
}
.rail:focus-visible {
outline: 2px solid #3055d8;
outline-offset: 2px;
}
.slide {
flex: 0 0 60%; /* no shrinking, so the rail really does overflow */
scroll-snap-align: start;
box-sizing: border-box;
padding: 12px;
border: 1px solid #3055d8;
border-radius: 8px;
background: #eef1fd;
font-size: 0.8rem;
}
.slide h3 { margin: 0 0 4px; font-size: 0.9rem; }
.slide p { margin: 0; color: #5c5c66; }
/* A reader who has asked for less motion gets no smooth scrolling. */
@media (prefers-reduced-motion: no-preference) {
.rail { scroll-behavior: smooth; }
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('snapping is set on the scroll container', /scroll-snap-type:\s*x mandatory/.test(css), true);
test('and an alignment edge on each child', /scroll-snap-align:\s*start/.test(css), true);
test('the container actually scrolls', /overflow-x:\s*auto/.test(css), true);
test('the cards refuse to shrink, so there is something to scroll', /flex:\s*0 0 60%/.test(css), true);
test('a scroll gutter keeps the snapped card off the edge', /scroll-padding-left/.test(css), true);
test('the rail is focusable and named', /tabindex="0"/.test(html) && /aria-label="Featured routes"/.test(html), true);
test('with a focus ring of its own', /\.rail:focus-visible/.test(css), true);
test('smooth scrolling is opt-in for people who tolerate motion', /prefers-reduced-motion: no-preference/.test(css), true);
Output
Snapping needs both halves: a type on the container and an alignment on the children. Miss the second and nothing happens, which is the usual first attempt. mandatory is the sharper edge — the scroller must always come to rest on a snap point, so if a card is wider than the scrollport there are positions the reader simply cannot reach; proximity avoids that by only snapping when you stop nearby. Two accessibility details ride along: a scroll container needs tabindex="0" to be scrollable from the keyboard, and scroll-behavior: smooth is animation, so it is gated behind prefers-reduced-motion: no-preference rather than switched off inside a reduce query.