Scroll Snap and CSS Carousels: scroll-snap-type, ::scroll-button and ::scroll-marker
Making scrolling land in the right places: scroll-snap-type, mandatory and proximity, scroll-snap-align and scroll-snap-stop, scroll-padding and scroll-margin, then CSS carousels with ::scroll-button, ::scroll-marker and :target-current.
kvi5hnuCode Sprout team member8 min readOn this page
When a container scrolls through a row of cards, photos or slides, it should come to rest with an item neatly aligned, not halfway between two. Scroll snap does that: you declare snap positions, and the browser adjusts where scrolling ends, for touch, trackpad, wheel, keyboard and programmatic scrolling alike. Newer features build complete carousels on top of it, with previous and next buttons and navigation dots, without JavaScript.
Scroll snap
.gallery {
display: flex; gap: 1rem; overflow-x: auto;
scroll-snap-type: x mandatory; /* on the scroll container */
}
.gallery > img { scroll-snap-align: center; } /* on the items */
scroll-snap-type, on the container
It takes an axis, x, y, block, inline or both, and a strictness:
mandatory: scrolling always ends on a snap position. Use it when every rest point should show a whole item, as in carousels.proximity(default): it snaps only when scrolling ends near a snap position. Use it for long content with occasional snap points, so users can still rest between them.
Tested in Chrome, in a 300px-wide container of 200px items, scrollTo({ left: 130 }) ended at 200px with both mandatory and proximity, and scrollTo({ left: 60 }) with mandatory ended at 0. So programmatic scrolling snaps too.
With mandatory, avoid items larger than the container: they can become impossible to read fully. Browsers do let you scroll within an item taller or wider than the scrollport, but proximity is safer for variable content.
scroll-snap-align, on the items
Which edge of the item lines up with the container: start, center, end or none (default). Two values set the block and inline axes separately: scroll-snap-align: none start.

The same request, scrollTo(200), ends at a different position for each alignment: the browser picks the snap position nearest to where scrolling would have ended.
scroll-snap-stop
normal (default) lets a fast swipe fly past several items. always makes scrolling stop at the next snap position, even after a fast flick, which suits one-slide-at-a-time interfaces like stories or onboarding.
scroll-padding and scroll-margin
scroll-padding, on the container, shrinks the area that snapping aligns to, like an inset. It's essential when a sticky header or a fixed button would cover snapped items. Tested in Chrome: withscroll-padding-inline: 20px, the same scroll ended at 180px instead of 200px, leaving 20px before the item.scroll-margin, on an item, adds space around it for snapping only.
Both also apply to scrolling to #fragment links, scrollIntoView() and focus scrolling, even without scroll snap. A common use is html { scroll-padding-top: 4rem; } so in-page links don't land under a sticky header.
When the layout changes, like a resize or added content, the browser re-snaps to keep the same item aligned.
Snap events and state
- Scroll-state container queries (article 79) can style the contents of the snapped item:
container-type: scroll-stateon the items, then@container scroll-state(snapped: x) { .slide-inner { … } }. As with every container query, the container itself can't be styled by it. - In Chrome 129, the
scrollsnapchangeandscrollsnapchangingevents report which element is snapped. Elsewhere, usescrollendand measure.
CSS carousels

Chrome 135 added pseudo-elements that turn a scroll container into a carousel, with the browser handling the buttons, the dots, keyboard support and the accessibility semantics.
::scroll-button()
A scroll container can generate up to four buttons, ::scroll-button(left), (right), (up) and (down), or the logical (inline-start), (inline-end), (block-start) and (block-end). A button exists only when its content isn't none:
.carousel::scroll-button(left) { content: "←" / "Previous"; } /* the text after / is the accessible name */
.carousel::scroll-button(right) { content: "→" / "Next"; }
.carousel::scroll-button(*) { inline-size: 2.5rem; aspect-ratio: 1; border-radius: 50%; border: 0; }
.carousel::scroll-button(*):disabled { opacity: 0.3; }
Pressing one scrolls the container by about one page, respecting snap positions. Buttons are :disabled when there's nothing more to scroll in their direction. They're placed next to the scroller by default; anchor positioning (article 59) puts them over it:
.carousel { anchor-name: --carousel; }
.carousel::scroll-button(*) { position: absolute; position-anchor: --carousel; }
.carousel::scroll-button(left) { position-area: center span-right; justify-self: start; }
.carousel::scroll-button(right) { position-area: center span-left; justify-self: end; }
::scroll-marker and scroll-marker-group
Dots that show the current slide and jump to any slide:
.carousel { scroll-marker-group: after; } /* create a ::scroll-marker-group after the scroller */
.carousel > .slide::scroll-marker { content: ""; display: block; inline-size: 0.7rem; block-size: 0.7rem; border-radius: 50%; border: 2px solid; }
.carousel > .slide::scroll-marker:target-current { background: currentColor; } /* the current slide */
.carousel::scroll-marker-group { display: flex; gap: 0.4rem; justify-content: center; }
scroll-marker-group: beforeorafteron the scroller creates the group. Tested in Chrome,aftercomputed toafter links. The defaultlinksmode makes markers behave like a list of links, tabbed between.tabsmakes them a tab list, with arrow keys moving between markers.- Each item's
::scroll-marker, withcontent, becomes a marker in that group. Activating it scrolls to the item. :target-currentmatches the marker of the item currently scrolled into view.
To use real HTML links as markers instead, put scroll-target-group: auto on a container of <a href="#slide-2"> links (Chrome 140). They then get :target-current too, like a table of contents that highlights the section in view.
::column for paginated carousels
With multi-column layout (article 73) on a scroll container, each column fragment is a ::column pseudo-element, which can have a scroll marker and a snap alignment. A list of 20 small items can be paged three at a time, with one dot per page:
.paged { columns: 1; column-fill: auto; block-size: 12rem; overflow-x: auto; scroll-snap-type: x mandatory; scroll-marker-group: after; }
.paged::column { scroll-snap-align: center; }
.paged::column::scroll-marker { content: ""; /* … */ }
scroll-initial-target
scroll-initial-target: nearest on an item makes the container start scrolled to it, for example to start in the middle of a list (Chrome 133).
Support and fallback
::scroll-button, ::scroll-marker, scroll-marker-group, :target-current and ::column are in Chrome 135 only so far. Other browsers ignore them and show a normal scroll-snapping container, which still works with touch, a trackpad, the keyboard and the scrollbar. That makes them a safe enhancement on top of scroll snap. Check with @supports selector(::scroll-button(*)) before hiding other navigation.
Accessibility
- A snapping scroll container must be reachable from the keyboard. If it contains no focusable items, add
tabindex="0"and an accessible name,aria-labelon arole="region". - Generated scroll buttons and markers have built-in roles and keyboard support. Give buttons an accessible name with
content: "→" / "Next". - Don't auto-advance carousels. If you must, add a pause control and stop for
prefers-reduced-motion: reduce(article 112). scroll-behavior: smoothanimates button and marker scrolling; disable it with reduced motion.- Make partially visible neighbors obvious, so users can tell there's more to scroll.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.carousel {
display: grid; grid-auto-flow: column; grid-auto-columns: 75%; gap: 0.6rem;
overflow-x: auto; overscroll-behavior-x: contain;
scroll-snap-type: x mandatory; scroll-behavior: smooth;
padding-block: 0.3rem; scrollbar-width: thin;
anchor-name: --carousel;
scroll-marker-group: after;
}
.slide {
scroll-snap-align: center; block-size: 8rem; border-radius: 10px;
display: grid; place-items: center; color: white; font: 800 1.3rem system-ui, sans-serif; background: var(--c);
}
/* buttons: Chrome 135+, positioned over the carousel with anchor positioning */
.carousel::scroll-button(*) {
inline-size: 2.2rem; aspect-ratio: 1; border-radius: 50%; border: 0;
background: white; color: #2a2118; font: 700 1.1rem system-ui; box-shadow: 0 2px 6px rgb(0 0 0 / 0.3);
position: absolute; position-anchor: --carousel; cursor: pointer;
}
.carousel::scroll-button(*):disabled { opacity: 0.35; cursor: default; }
.carousel::scroll-button(left) { content: "←" / "Previous"; position-area: center span-right; justify-self: start; margin-inline-start: 0.3rem; }
.carousel::scroll-button(right) { content: "→" / "Next"; position-area: center span-left; justify-self: end; margin-inline-end: 0.3rem; }
/* dots */
.carousel::scroll-marker-group { display: flex; justify-content: center; gap: 0.45rem; margin-block-start: 0.5rem; }
.slide::scroll-marker { content: ""; display: block; inline-size: 0.7rem; block-size: 0.7rem; border-radius: 50%; border: 2px solid #2a2118; }
.slide::scroll-marker:target-current { background: #2a2118; }
.note { margin-block-start: 1.2rem; color: #555; }
@media (prefers-reduced-motion: reduce) { .carousel { scroll-behavior: auto; } }
Each swipe or scroll comes to rest with a destination centered, because of scroll-snap-type: x mandatory and scroll-snap-align: center. In Chrome, the arrows and dots are pseudo-elements of the scroller: the arrows disable themselves at each end, and the filled dot follows the current slide. In other browsers, it's still a working, swipeable row.
What's current
- Scroll snap,
scroll-padding,scroll-marginandscroll-snap-stop: all browsers. ::scroll-button(),::scroll-marker,scroll-marker-group,:target-currentand::column: Chrome 135.scroll-target-group: Chrome 140.scroll-initial-target: Chrome 133.scrollsnapchangeandscrollsnapchangingevents: Chrome 129.
Common questions
Why doesn't my container snap?
scroll-snap-type goes on the scroll container, which must actually scroll, and scroll-snap-align on its direct items.
Mandatory or proximity?
mandatory for carousels and slides; proximity for long pages with a few snap points.
Why does a sticky header cover my snapped item or #anchor target?
Add scroll-padding-top with the header's height to the scroll container, or html for the page.
Do I still need a JavaScript carousel? For Chromium only, no. Elsewhere, the CSS version falls back to a plain scroller; add JavaScript buttons only if they're essential.
Can I hide the carousel's scrollbar?
Yes: scrollbar-width: none hides it, and the element still scrolls with touch, the trackpad and the keyboard. Only do it when buttons, markers or a peeking next slide make the scrolling obvious, because the scrollbar is often the only sign there's more content (article 43).
Summary
scroll-snap-type: x mandatoryon the container andscroll-snap-alignon items make scrolling land on items.proximitysnaps only near a snap point;scroll-snap-stop: alwaysstops at every item.scroll-paddingandscroll-marginadjust where items land, for snapping and for in-page links.::scroll-button(),::scroll-markerwithscroll-marker-group, and:target-currentbuild carousels in CSS, in Chrome 135.- The fallback is a normal snapping scroller; keep it keyboard-accessible, and don't auto-advance.
Practice questions
Write CSS for a horizontal row of cards that always comes to rest with a card's left edge aligned, 1rem in from the container's edge.
Answer
.row { display: flex; gap: 1rem; overflow-x: auto; scroll-snap-type: x mandatory; scroll-padding-inline-start: 1rem; }
.row > .card { flex: 0 0 80%; scroll-snap-align: start; }
A full-screen stories viewer lets a fast swipe skip three stories. How do you stop at the next one?
Answer
.story { scroll-snap-align: start; scroll-snap-stop: always; }
Links to #sections scroll the heading under a 4rem sticky header. Fix it.
Answer
html { scroll-padding-top: 4rem; }
Or scroll-margin-top: 4rem on the headings.
Write the CSS to add next and previous buttons with accessible names to a .carousel in Chrome.
Answer
.carousel::scroll-button(left) { content: "‹" / "Previous slide"; }
.carousel::scroll-button(right) { content: "›" / "Next slide"; }
.carousel::scroll-button(*):disabled { opacity: 0.4; }
A scroll button exists only when its content isn't none, and the text after the / is its accessible name.
Review quiz
Test yourself on Part 16 with the CSS Part 16 review quiz.
Previous: View Transitions: Animating Between Page States and Between Pages
Next: Modular CSS: Components, Variants, Naming Conventions and BEM

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.