Skip to content
Tutorials Center
My learning

Topics

Lesson 107 of 139 ยท Modern CSS

Scroll Snap

Build smooth carousels and galleries with CSS scroll snap, then polish scrolling with scroll-padding, scroll-margin, overscroll-behavior and scrollbar-gutter.

Swipe through photos on a phone and each picture settles neatly in place. It never stops halfway between two images. That behavior is called snapping, and CSS can do it for any scrolling container, with no JavaScript at all.

In this lesson you will build a horizontal carousel and a full-height vertical "slides" layout with scroll snap. Then you will learn a group of related properties that make scrolling feel polished: scroll-padding and scroll-margin for fixed headers, overscroll-behavior to stop scrolling from leaking into the page, and scrollbar-gutter to stop layouts from jumping when a scrollbar appears.

Two properties to get started

Scroll snap needs two pieces:

  1. On the scroll container (the element with overflow: auto or scroll), set scroll-snap-type. It says which direction snaps and how strictly.
  2. On each child, set scroll-snap-align. It says which part of the child lines up with the container: its start, center or end.
CSS
.carousel {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}

.carousel > * {
  flex: 0 0 80%;
  scroll-snap-align: center;
}

That is the whole recipe. When the user stops scrolling, the browser picks the nearest snap position and glides there.

A snapping carousel

Scroll sideways with a trackpad, a shift+mouse-wheel or the scrollbar. Each card glides to the center. Then change center to start and see how the snapping point moves to the left edge.

mandatory or proximity?

The second word in scroll-snap-type sets the strictness:

  • mandatory always snaps. When scrolling stops, the container must rest on a snap position. That suits carousels where every item is roughly the size of the viewport.
  • proximity snaps only when the scroll stops close to a snap position. Otherwise it leaves the scroll where the user put it. That suits long content with occasional landmarks.

Be careful with mandatory when an item is taller (or wider) than the container. Browsers try to let users scroll through oversized items, but the experience can still feel like fighting the page. If items can grow, choose proximity.

The first word is the axis: x, y, both, or the logical inline and block (see Writing Modes and Logical Properties).

Vertical full-screen sections

The same idea works vertically. Make a container exactly as tall as the viewport, give each section the same height, and snap on the y axis:

Vertical snapping sections

Notice the class must-stop. Normally a fast flick can fly past several snap points. scroll-snap-stop: always tells the browser not to skip that element: scrolling must stop there first. Use it sparingly, because forcing stops can frustrate people who know where they want to go.

Full-screen snapping is not always friendly. It takes control away from users, it can trap people who zoom in, and keyboard users may struggle when content overflows a section. Use it for genuinely slide-like content, and test with the keyboard and at 200% zoom.

Making room: scroll-padding and scroll-margin

Snap positions line up with the container's edge by default. Often that edge isn't where you want things to land. A sticky header might cover the top, or you might want a peek of the previous card.

  • scroll-padding goes on the container. It shrinks the area used for snapping, as if the container had extra padding.
  • scroll-margin goes on a child. It adds space around that child's snap area.

The most useful everyday application has nothing to do with carousels. When someone follows an in-page link such as #pricing, the browser scrolls that section to the very top, right under your sticky header. One line fixes it:

CSS
html {
  scroll-padding-top: 5rem;   /* height of the sticky header */
}

This works for anchor links and scrollIntoView(), even without scroll snapping. Here it is in action:

scroll-padding with a sticky header

Click Hours or Contact. The heading lands just below the dark header instead of hiding behind it. Delete the scroll-padding-top line and try again to see the problem it solves. (scroll-behavior: smooth animates the jump; Motion, Performance and prefers-reduced-motion explains why you should switch it off for users who prefer reduced motion.)

Stopping scroll chaining: overscroll-behavior

Scroll to the bottom of a small scrolling box, keep scrolling, and the page behind it starts to scroll. That is called scroll chaining. It is annoying in chat windows, side drawers and modals.

CSS
.chat-messages {
  overflow-y: auto;
  overscroll-behavior: contain;
}

The values:

  • auto: the default; scrolling chains to the parent.
  • contain: scrolling stays inside this element. Local effects such as the bounce at the edge on touch devices still happen.
  • none: like contain, and also turns off those edge effects, including pull-to-refresh when used on the page itself.

You can set each axis separately with overscroll-behavior-x and overscroll-behavior-y. A horizontal carousel often uses overscroll-behavior-x: contain so that swiping past the last card doesn't trigger the browser's "go back" gesture.

No more layout jumps: scrollbar-gutter

On systems with classic scrollbars, such as most Windows and Linux setups, a scrollbar takes up width. When content grows tall enough to need one, the scrollbar appears and everything shifts sideways. On a page that loads content dynamically, that jump is jarring.

CSS
.panel {
  overflow-y: auto;
  scrollbar-gutter: stable;
}

scrollbar-gutter: stable reserves the space for the scrollbar even when there's nothing to scroll, so the content never moves. stable both-edges reserves the same space on the opposite side too, keeping content visually centered. On devices with overlay scrollbars, which float above the content (typical on phones and macOS), the property has no visible effect, because those scrollbars never take up space.

Scroll snap and accessibility

A few habits keep snapping pleasant for everyone:

  • Keep native scrolling. Snap works with trackpads, touch, the mouse wheel, scrollbars and keyboard arrows. Don't hide the scrollbar on desktop, where it may be the only visible hint that more content exists.
  • Give carousels visible controls or at least a partial "peek" of the next item, so people know they can scroll.
  • Make the container focusable if it has no focusable children (tabindex="0" with an accessible name), so keyboard users can scroll it.
  • Prefer proximity whenever content height is unpredictable.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pair of properties creates basic scroll snapping?

    The container decides the axis and strictness with scroll-snap-type; each child decides which edge lines up with scroll-snap-align.
  2. 2. Your sticky header hides headings when users follow in-page links. What fixes it?

    scroll-padding-top moves the scroll target down by the header's height, and it works for anchor links even without snapping.
  3. 3. When is proximity a better choice than mandatory?

    mandatory always forces a snap, which can make oversized items hard to read. proximity snaps only near a snap point.
  4. 4. What does overscroll-behavior: contain prevent?

    It stops scroll chaining. To stop skipping snap points you would use scroll-snap-stop: always.

Summary

  • scroll-snap-type: x mandatory (or y, both, proximity) turns a scroll container into a snapping one.
  • scroll-snap-align: start | center | end on each child sets its snap position; scroll-snap-stop: always prevents skipping it.
  • scroll-padding (container) and scroll-margin (child) adjust where things land, including for anchor links under sticky headers.
  • overscroll-behavior: contain stops scrolling from leaking into the page.
  • scrollbar-gutter: stable reserves scrollbar space so layouts don't jump.
  • Keep native scrolling visible, and test snapping with the keyboard and at high zoom.

Next: Anchor Positioning, where you will tether tooltips and menus to other elements using only CSS.