Scroll Snapping – Control the Scrolling Experience with CSS

One aspect in which websites lag behind native applications is the smooth user experience. The perception of a smooth experience largely comes from the user interface’s responsiveness to user interactions, with the action of scrolling content being a crucial part of those interactions.

Given these limitations, frontend developers, out of necessity, turned to JavaScript libraries to alter and maneuver the scrolling experience of a web page. However, as you probably know, additional libraries also contribute to bloated code and additional JS computations that negatively affect website performance in most cases.

With the introduction of CSS Scroll Snap specifications, you have the option to control the scrolling behavior of a web page (to a certain extent) by using web standards and without relying on heavy external libraries. This post explains how to use CSS Scroll Snap.

I also cover the parts that usually cause trouble in real projects: a sticky header that hides the snapped content, users skipping slides with one fast swipe, and knowing in JavaScript which element the browser snapped to.

CSS Scroll Snap – Basic Usage

You can scroll a page or any element in several ways: using a mouse, a keyboard, or touch gestures on touch screens.

Unlike the standard, i.e., linear scrolling experience where the scroll follows the device you scroll with exactly, scroll snapping allows you to “jump” (Snap!) to a specific point during the scroll.

For the sake of writing convenience, this post refers to these points as snap points.

Scroll snapping is done by setting the scroll-snap-type property on a container (parent) and the scroll-snap-align property on the elements inside that container (child elements).

When you scroll the container, it will jump to those children according to the settings you determined, namely it will snap to those elements located in the same container. The code in its most basic form looks like this:

<div class="snap-container">
  <section class="child"></section>
  <section class="child"></section>
  <section class="child"></section>
</div>
.snap-container {
  scroll-snap-type: y mandatory;
}

.child {
  scroll-snap-align: start;
}

As noted, the scroll snapping properties are applied both to the container and to the elements within it. One could say that the operation is similar to Flexbox or to CSS Grid in which the parent element becomes a flex container or a grid container. In this case, it can be said that the parent element becomes a snap container.

For the sake of writing convenience, this post refers to that parent element as a container or snap container.

The following section explains the main property that refers only to the parent element, namely the container.

The scroll-snap-type feature

scroll-snap-type determines how “strict” those snap points will be, or in other words, how rigidly they will be enforced. Beyond the first value that indicates the directionality of the scroll, namely horizontal or vertical scroll (x or y), there is a second value that can be either mandatory or proximity.

“mandatory” vs. “proximity”

The value mandatory indicates that the browser must jump to a snap point once the user stops scrolling. The value proximity is less strict and indicates that the browser may jump to a snap point if it deems it appropriate.

In general, it can be said that proximity comes into play when a user stops scrolling a few pixels away from the snap point.

An example will explain this better than words. Pick a value in the following example, then scroll the cards and let go between two of them. Can you see and understand the differences?

Demo: scroll-snap-type

Scroll the cards and let go between two of them: swipe, drag the scrollbar, or hold Shift and use the mouse wheel.

After the scroll: no snapping, stopped at 0px

If you’re wondering how the scrollbars are styled, take a look at the post Styling Scrollbars with CSS.

The scroll-snap-align feature

The scroll-snap-align feature refers only to the elements inside the container (child elements). It allows you to determine which part of the element is supposed to snap to the container. The property can take four values: none, start, center, & end.

These are of course relative to the direction of the scroll. If you are scrolling vertically, start refers to the top edge of the element. If you are scrolling horizontally, it refers to the left edge. The values end & center follow the same principle.

Be aware! Changing the document’s direction also affects this property. On right-to-left (RTL) websites, the start value refers to the right edge.

The following example explains the operation of this property. Pick a value and watch the carousel move the card to the dashed snap line, then scroll it yourself.

Demo: scroll-snap-align
Result: the start edge of card 3 sits on the line

Before wrapping up this property, note that you can set a different value for vertical and horizontal directions in the following way:

.child {
  scroll-snap-align: start end;
}

The scroll-snap-stop feature

By default, a fast scroll can carry the user past several snap points at once. On a phone, one strong swipe through a carousel can skip three or four cards before the scroll settles.

The scroll-snap-stop feature, set on the child elements, controls this behavior. It takes two values: normal (the default), which lets the scroll pass over snap points, and always, which forces the scroll to stop at the next snap point.

.carousel {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}

.card {
  scroll-snap-align: start;
  scroll-snap-stop: always;
}

In the following example, the “Fling 3 cards” button scrolls the carousel three cards forward in one move. With normal it lands on card 4. With always it stops at card 2. On a touch screen, a quick flick shows the same difference. A mouse wheel can’t show it, because each wheel step is a separate scroll.

Safari is the exception for the button. It ignores scroll-snap-stop for scrolls triggered by JavaScript, so in Safari the button reaches card 4 in both modes. Swipe the cards instead.

Demo: scroll-snap-stop
Last scroll: -Cards passed: -

On a touch screen, flick the cards. With a mouse, use the button: each wheel step is a separate short scroll, so the wheel can't show the difference.

Use always where every item matters, such as carousels, onboarding steps or stories. On a long list it forces users to move one item at a time, which gets frustrating fast.

For a simple carousel, these properties cover much of what you once needed a library like Slick for.

The next section provides a more practical example to understand the capabilities.

Example of Using Scroll Snap on a Full Screen

Take a look at the following HTML:

<div class="scroll-container">
  <section>
    <h2>Section 1</h2>
  </section>
  <section>
    <h2>Section 2</h2>
  </section>
  <section>
    <h2>Section 3</h2>
  </section>
  <section>
    <h2>Section 4</h2>
  </section>
</div>

As covered earlier, scroll snapping requires two main CSS properties: the first is scroll-snap-type on the container – in our case, the element with the class scroll-container. The second is scroll-snap-align for the elements within the container (the direct children) – in our case, the section elements.

Beyond that, you also need to define the height of the container and, of course, set the property overflow: scroll to allow scrolling. The CSS looks like this:

.scroll-container {
  height: 100vh;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}

section {
  height: 100vh;
  scroll-snap-align: center;
}

In this example, the scroll-snap-type property is set to y (i.e., vertical scrolling) and the second value as mandatory. This means that once the user stops scrolling, the scroll position will always jump to the nearest snap point.

Alternatively, you could set the value to proximity, which would determine that the scroll position will only snap to a snap point if the user stopped scrolling near any snap point.

Setting precise animations or physics to enforce these snap points is not performed by this feature but is determined by the User Agent (the browser) used.

If your content might be taller than the container (in this case 100vh), using mandatory can be problematic and cause parts of the content to be hidden above or below the visible area. Use proximity instead when content height is unpredictable.

On mobile, 100vh can actually be taller than the visible area because it includes the space behind the browser’s address bar. Consider using svh (small viewport height) instead of vh for scroll-snap containers to avoid overflow on phones.

But if you know that the content will always fit the viewport, then using the mandatory value can create a more consistent user experience:

See the Pen
Full Screen scroll-snap Demo
by Roee Yossef (@roeey)
on CodePen.

Scroll Snap with a Sticky Header: scroll-padding & scroll-margin

Most sites have a sticky header. Combine it with scroll snap and you will quickly run into a problem: the browser snaps each section to the top edge of the container, right under the header. The section’s heading ends up hidden.

The fix is an offset that tells the browser where the visible area really starts. You can set it in one of two places:

  • scroll-padding on the container – one offset for every snap point inside it.
  • scroll-margin on the child elements – an offset for each element separately.

Scroll the page in the following example, or use the 1-4 buttons in its header. With no offset, every section heading slides under the header. Switch to either fix and the headings land just below it.

Demo: sticky header + scroll snap
Sticky header · 56px
Section 1
Section 2
Section 3
Section 4
Section heading: hidden behind the header

Both options give the same result here. In most cases scroll-padding on the container is the simpler choice, since you set it once to match the header height:

.scroll-container {
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
  scroll-padding-top: 56px; /* the header height */
}

Use scroll-margin when a specific element needs a different offset, for example a section that has its own sticky sub-header.

If your header isn’t sticking in the first place, see Troubleshooting position: sticky.

The same fix works for anchor links

These properties are not limited to scroll snap. They also apply when the browser scrolls to an anchor, for example when a reader clicks a link in a table of contents.

Set scroll-padding-top on the html element and every anchor jump on the page will stop below the sticky header:

html {
  scroll-padding-top: 80px;
}

Intersection Observer API

With CSS Scroll Snap, your page already feels more like a native application. To enhance this further, you can add animations or transitions based on scrolling. This can be done by implementing a parallax scrolling effect or alternatively, by using the Intersection Observer API.

The Intersection Observer API allows you to create an observer that watches how elements intersect with the viewport, triggering a function whenever such an intersection occurs.

It’s much more efficient than libraries that rely on a constant listening to the scroll event.

You can create an observer that watches when each section enters and exits the viewport, for example:

const sections = [...document.querySelectorAll('section')]

const options = {
  rootMargin: '0px',
  threshold: 0.25
}

const callback = (entries) => {
  entries.forEach((entry) => {
    if (entry.intersectionRatio >= 0.25) {
      entry.target.classList.add("is-visible");
    } else {
      entry.target.classList.remove("is-visible");
    }
  })
}

const observer = new IntersectionObserver(callback, options)

sections.forEach((section, index) => {
  observer.observe(section)
})

In this example, a callback function is triggered each time a section intersects or actually when 25% of it intersects with the viewport (threshold). In other words, when 25% of the section enters the viewport, the class is-visible is added to that section and removed when the section does not meet this criterion.

Then simply add the animation, for example, the following CSS:

section .content {
  opacity: 0;
}

section.is-visible .content {
  opacity: 1;
  transition: opacity 1000ms;
}

And here is the demo of the final result:

See the Pen
Full Screen scroll-snap Demo – Intersection Observer
by Roee Yossef (@roeey)
on CodePen.

Scroll Snap Events in JavaScript

The Intersection Observer tells you what is visible. It doesn’t tell you which element the browser actually snapped to, or when the snap finished. Newer scroll events close that gap.

The scrollend event

The scrollend event fires once, when the scroll has finished, including the snap movement at the end. Before it existed, the common approach was a scroll listener with a timeout, which runs dozens of times per second and hurts scrolling performance.

const container = document.querySelector('.scroll-container');

container.addEventListener('scrollend', () => {
  const index = Math.round(container.scrollTop / container.clientHeight);
  console.log('Snapped to section', index + 1);
});

Chrome and Edge have supported scrollend since version 114 and Firefox since 109. Safari added it in version 26.2 (December 2025), so it now works in all major browsers.

scrollsnapchanging and scrollsnapchange

These two events were built for scroll snap. Each one hands you the snapped element directly, so there is no position math. Use event.snapTargetBlock for vertical scrolling and event.snapTargetInline for horizontal scrolling.

  • scrollsnapchanging fires during the scroll, as soon as the browser picks a new snap target. Use it for instant feedback, such as highlighting the next dot in a carousel.
  • scrollsnapchange fires when the scroll ends and the new target is final. Use it for actions that should run once, such as loading content or sending an analytics event.
const carousel = document.querySelector('.carousel');

carousel.addEventListener('scrollsnapchanging', (event) => {
  // The slide the scroll is heading to (not final yet)
  event.snapTargetInline.classList.add('is-next');
});

carousel.addEventListener('scrollsnapchange', (event) => {
  // The slide the scroll settled on
  carousel.querySelector('.is-next')?.classList.remove('is-next');
  event.snapTargetInline.classList.add('is-active');
});

Click “Next” in the following example and watch the three cards under the slides. scrollsnapchanging lights up first, while the slide is still moving. scrollsnapchange and scrollend light up together when the scroll stops.

Now scroll the slides just a little and let go, so the slide springs back. scrollend still fires, but the two snap events stay silent, because the snap target never changed.

Demo: snap events
Slide1
Slide2
Slide3
Slide4
Slide5

Each card lights up when its event fires. Times count from the moment the scroll starts.

scrollsnapchangingFires while the scroll is still moving, as soon as the browser picks a new slide.
Waiting for a scroll
scrollsnapchangeFires when the scroll stops on a new slide. The target is final.
Waiting for a scroll
scrollendFires when any scroll stops. It doesn't say which slide.
Waiting for a scroll

These two events work only in Chrome and Edge 129+ for now. Firefox and Safari don’t support them yet, so check for support and fall back to scrollend:

if ('onscrollsnapchange' in window) {
  carousel.addEventListener('scrollsnapchange', onSnap);
} else {
  carousel.addEventListener('scrollend', onSnapFallback);
}

Browser Support

The CSS properties covered in this post work in all major browsers and have done so for years. The JavaScript events are newer, and the two snap-specific ones are still Chromium-only. Here are the minimum versions:

Feature Chrome Firefox Safari
scroll-snap-type,
scroll-snap-align
69 68 11
scroll-snap-stop 75 103 15
scroll-padding,
scroll-margin
69 68 14.1
scrollend event 114 109 26.2
scrollsnapchanging,
scrollsnapchange
129 No No

Edge is built on Chromium: it supports the CSS properties from version 79 and the events from the same versions as Chrome.

The Intersection Observer API is supported in all modern browsers as well.

If you still need to support very old browsers, you can wrap the scroll snap styles in a feature query (@supports) to provide a fallback browsing experience for older browsers.

@supports (scroll-snap-type: y mandatory) {
  .scroll-container {
    height: 100vh;
    overflow-y: scroll;
    scroll-snap-type: y mandatory;
  }

  section {
    height: 100vh;
    scroll-snap-align: center;
  }
}

FAQs

Common questions about CSS Scroll Snap:

What is CSS Scroll Snap?
CSS Scroll Snap is a set of CSS properties that allow you to control the scrolling behavior of a container so it "snaps" to specific child elements. Instead of free-flowing linear scrolling, the viewport locks onto predefined snap points, creating a smoother, more app-like experience without JavaScript.
What is the difference between mandatory and proximity in scroll-snap-type?
mandatory forces the browser to always snap to a snap point when scrolling stops, regardless of position. proximity only snaps when the user stops scrolling close to a snap point. Use mandatory when content fits the viewport perfectly, and proximity when content height is unpredictable.
What does scroll-snap-align do?
scroll-snap-align is set on child elements inside the snap container. It determines which part of the element aligns with the container's snap position. Possible values are start, center, end, and none. You can also set different values for vertical and horizontal axes (e.g., start end).
Can I use CSS Scroll Snap in all browsers?
Yes. The scroll snap properties, including scroll-snap-stop, scroll-padding and scroll-margin, work in all modern browsers: Chrome, Edge, Firefox and Safari. The scrollend event works in all of them too (Safari since 26.2). Only the scrollsnapchanging and scrollsnapchange events are limited to Chrome and Edge 129+ for now.
How can I combine CSS Scroll Snap with animations?
You can use the Intersection Observer API alongside CSS Scroll Snap. The observer watches when elements enter or leave the viewport and triggers a callback where you can add or remove CSS classes that control animations, such as fade-in or slide-in transitions.
Why does my sticky header cover the section after it snaps?
The browser snaps each section to the top edge of the scroll container, and the sticky header sits on top of that edge. Add scroll-padding-top with the header height to the container, or scroll-margin-top to the sections, and the snapped content will land below the header.
How do I know which element is snapped using JavaScript?
In Chrome and Edge 129+, listen to the scrollsnapchange event and read event.snapTargetBlock or event.snapTargetInline. In other browsers, listen to the scrollend event and calculate the element from the container's scroll position.

Summary

CSS Scroll Snap needs only two properties to get started: scroll-snap-type on the container and scroll-snap-align on its children. Add scroll-snap-stop when users must not skip items, and scroll-padding or scroll-margin when a sticky header covers the snapped content.

On the JavaScript side, scrollend now works in all major browsers, and scrollsnapchange gives you the snapped element directly in Chrome and Edge.

The world of web is increasingly approaching that of native apps and offers an open and accessible alternative to them. While these CSS features are not a complete replacement for JavaScript libraries that allow more control, they offer a significant advantage: simplicity and reliability.

By adopting web standards where possible, you can enjoy the best of both worlds: impressive and smooth websites that meet client expectations, while maintaining excellent site performance.

Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!