Scroll snap and smooth scrolling

Intermediate · 7 min

How scroll-snap-type and scroll-snap-align settle a scroller on chosen positions, how scroll-padding fixes sticky headers, and how scroll-behavior works.

Before thisAnimation performanceOverflow and Scroll Containers

Web Dev Simplified · 7 min

Scroll snap and smooth scrolling

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Web Dev SimplifiedReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Scroll snapping has two halves. The container declares `scroll-snap-type` (axis and strictness). Each child that should be a stop declares `scroll-snap-align`.
  • `mandatory` always settles on a snap point after scrolling ends. `proximity` snaps only when the scroll finishes near one, which is gentler and safer for content taller than the container.
  • `scroll-padding` on the container and `scroll-margin` on the items shift where the snapport edge or the item edge lands. They are also the standard fix for a sticky header covering an anchor target.
  • `scroll-behavior: smooth` animates jumps caused by anchor links and by `scrollTo` calls that do not set a behavior. It does not affect the user's own wheel or touch scrolling.
  • Wrap smooth scrolling in `@media (prefers-reduced-motion: no-preference)`, and make horizontal scrollers keyboard-reachable.

The problem snapping solves

Think of a swipeable photo strip on a phone. The user flicks, and the strip should come to rest with one photo lined up, not with two photos each half cut off. Before CSS did it, that took JavaScript listening to scroll events, working out the nearest slide, and animating to it, all on the main thread and fighting the browser’s own momentum scrolling.

Scroll snap moves the decision to the browser. You describe where the resting points are, and the browser’s own scrolling machinery lands on one, including on touch devices where it matters most.

The two halves

.strip {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  gap: 1rem;
}

.strip > img {
  flex: 0 0 80%;
  scroll-snap-align: center;
}

scroll-snap-type goes on the scroll container. It takes an axis (x, y, inline, block, both) and a strictness. scroll-snap-align goes on each child that is a stop, and says which edge of the item lines up with which edge of the container: start, center or end. A container with a type but children without an align snaps nowhere.

Mandatory or proximity

  • mandatory: after a scroll ends, the container must rest on a snap point. Good for carousels and slide decks.
  • proximity: the browser snaps only if the scroll ends near a snap point. Better for long pages with a few sections, where users still need to stop in between.

mandatory has a trap: if an item is bigger than the container on the snap axis, snap points only exist at its edges, and the middle can be hard to reach. Prefer proximity for content of unpredictable size.

scroll-snap-stop: always on an item stops a fast flick from skipping over it, which suits steppers where every slide must be seen.

Adjusting where things land: scroll-padding and scroll-margin

Two properties adjust the landing spot, and they live on opposite sides:

  • scroll-padding goes on the scroll container and insets the area that counts as the snapport.
  • scroll-margin goes on the item and pushes its snap area outward.

They apply not only to snapping but to any scroll-into-view the browser does, including following an anchor link. That makes them the standard fix for a sticky header:

html { scroll-padding-top: 4rem; }   /* header height + a little air */

Now <a href="#pricing"> stops the heading just below the header instead of behind it. If different anchors need different offsets, put scroll-margin-top on the targets instead.

Smooth scrolling

@media (prefers-reduced-motion: no-preference) {
  html { scroll-behavior: smooth; }
}

scroll-behavior: smooth makes jumps animate: following #section, calling scrollIntoView(), or scrollTo({ top: 0 }) without a behavior. It does not alter how wheel and touch scrolling feel. Smooth scrolling over a long distance can feel slow and disorienting, so some users and some operating systems ask for less motion, and the media query above respects that. JavaScript can still override per call with behavior: 'instant'.

Accessibility notes

  • A horizontally scrolling region needs to be reachable without a mouse. Keyboard users can scroll a container only when it can take focus. Making the region focusable (tabindex="0" with an accessible name) solves it, and browsers are increasingly doing this by default for scrollers with no focusable content.
  • Snap does not replace visible controls. Previous and next buttons, or a visible scrollbar, help people who do not swipe.
  • Do not put essential content only reachable via a swipe gesture.

Overscroll and neighbours

Scroll chaining is the effect where reaching the end of an inner scroller starts scrolling the page. overscroll-behavior: contain on the inner container turns that off. Use it for a snap strip inside a page, so a sideways flick at the end does not trigger browser back-navigation gestures on some devices.

What the video covers

Web Dev Simplified’s “Learn CSS Scroll Snap In 6 Minutes” (May 2023, about 6.5 minutes) is a quick tour of the scroll snap properties. I could not read a transcript, and the description gave no timestamps, so which properties get screen time is not described here. This lesson’s scroll-padding, scroll-margin, scroll-behavior and accessibility sections go beyond a 6-minute video, and Kevin Powell’s video on fixed headers (further viewing) covers the sticky-header case. Scroll snap has been in all major browsers for years, since Chrome 69, Firefox 99 and Safari 11 for scroll-snap-type according to MDN’s compatibility data.

Where this fits

Scroll snap and smooth scrolling depend on understanding scroll containers and overflow. The next lesson makes scroll position itself the clock for an animation: scroll-driven animations.

Check yourself

  1. 01You add `scroll-snap-type: x mandatory` to a carousel but nothing snaps. What did you forget?

    The items need `scroll-snap-align` (`start`, `center` or `end`). The container's `scroll-snap-type` says snapping is on; each child's `scroll-snap-align` defines where it can stop. Also check that the container actually overflows on that axis.

  2. 02Why might `mandatory` be a bad choice for a vertical snap list where one item is taller than the viewport?

    With `mandatory` the browser must land on a snap point, so it can make the far part of a tall item hard or impossible to reach, because every scroll ends at an item edge. `proximity` lets the user stop between snap points, and `scroll-snap-stop` and item sizing can also help.

  3. 03A page has a 64px sticky header. Clicking a table-of-contents link scrolls the heading under it. What CSS fixes this?

    `html { scroll-padding-top: 64px; }` or `h2 { scroll-margin-top: 64px; }`. Both change where the browser stops when it scrolls an element into view, leaving room for the header. The first is applied once to the scrolling element, the second to the targets.

  4. 04Does `html { scroll-behavior: smooth }` make mouse-wheel scrolling smoother?

    No. It affects scrolls the browser does on the page's behalf: following an in-page anchor, `element.scrollIntoView()` and `scrollTo()` with no explicit behavior. Wheel, touch and keyboard scrolling are controlled by the browser and the operating system.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark