sodcalculator

10 Tech How-To

CSS Scroll Snap: Smooth Scrolling Sections

Guide 10-52171 · Updated · Step by step

CSS scroll snap lets you create smooth, section-by-section scrolling by defining snap points that the browser automatically aligns to when the user stops scrolling. You enable it by setting scroll-snap-type on the scroll container and scroll-snap-align on each child section, then you can optionally add scroll-behavior: smooth for a polished glide. This guide covers the exact properties, how to choose the right values, common pitfalls, and real-world examples.

What Is CSS Scroll Snap?

Scroll snap is a CSS module that controls where a scroll operation ends. Instead of letting the viewport rest anywhere within a long page, you define snap positions—usually one per section—so the browser “snaps” to the nearest one when the user stops scrolling. This is especially popular for one-page portfolios, product showcases, and presentation-style sites where each section fills the screen.

The two core properties are:

  • scroll-snap-type on the container—sets the axis (x, y, or both) and strictness (mandatory or proximity).
  • scroll-snap-align on each child—defines which edge of the child aligns to the snap position (start, center, or end).

Without these, the page scrolls freely. With them, each section locks into place, giving a controlled, app-like feel.

How to Enable Scroll Snap

Here is the minimal setup for a vertical, full-screen section scroll:

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

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

That’s it. The container must have a fixed height and overflow set to scroll or auto. Each child needs a defined height (often 100vh) and scroll-snap-align. The mandatory keyword forces the browser to always land on a snap point, while proximity only snaps when the user is close to one—giving more freedom but less consistency.

For horizontal snapping, change the axis to x and make children width: 100% or 100vw.

Choosing Between mandatory and proximity

The scroll-snap-type value determines how strict the snapping is:

  • mandatory—the browser always lands on a snap point. This is ideal for full-screen sections where you never want a half-visible section. However, if a section is taller than the container, the user may not be able to see its middle content, because the snap point forces the top or bottom to align.
  • proximity—the browser snaps only if the scroll position is close enough to a snap point. This allows free scrolling within a large section, but may feel less “locked in.”

For most full-screen section sites, mandatory works well. For long content blocks that exceed the viewport, use proximity to avoid trapping the user.

Adding Smooth Scrolling

Scroll snap controls where the scroll stops, but not how it moves. To get a smooth animated glide between sections, add scroll-behavior: smooth to the container (or the html element for the whole page). This also affects anchor links and programmatic scrollTo calls.

.container {
  scroll-behavior: smooth;
  scroll-snap-type: y mandatory;
}

Note: scroll-behavior applies to the element that is scrolled, not the child elements. If you want a custom easing curve beyond the built-in smooth, you’ll need JavaScript (e.g., the requestAnimationFrame technique or a library), but the native smooth behavior is usually sufficient.

Practical Example: Full-Screen Portfolio

Here’s a complete example you can copy into an HTML file:

<div class="container">
  <section class="section">Home</section>
  <section class="section">About</section>
  <section class="section">Work</section>
  <section class="section">Contact</section>
</div>

<style>
  .container {
    height: 100vh;
    overflow-y: scroll;
    scroll-snap-type: y mandatory;
    scroll-behavior: smooth;
  }
  .section {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    scroll-snap-align: start;
  }
  .section:nth-child(odd) { background: #f0f0f0; }
  .section:nth-child(even) { background: #e0e0e0; }
</style>

This creates four full-screen sections that snap to the top of each section when you scroll. The scroll-behavior: smooth makes the transition between them animated rather than abrupt.

Common Problems and Fixes

Several issues frequently trip up developers:

  • Snap not working—The container must have a fixed height and overflow set to scroll or auto. If the container is the body, set html and body to height: 100% and apply scroll-snap-type to the body.
  • Snap points ignored on some browsers—Older Safari requires the -webkit- prefix on scroll-snap-type. Modern browsers are fine without it, but adding the prefix is safe.
  • Content clipped—If a section is taller than the container, mandatory can make it impossible to see the middle. Use proximity or make sections exactly the container height.
  • Snap feels janky—Ensure no other CSS is interfering with the scroll (e.g., overflow-anchor or touch-action). Also, avoid setting scroll-snap-stop unless you understand it; always forces a stop at every snap point, which can feel rigid.

Accessibility and User Experience

Scroll snap can disorient users who rely on keyboard or assistive technology. Always provide a visible scroll indicator (like a progress bar) and ensure that keyboard focus can move between sections. Also, consider that some users prefer to read at their own pace—mandatory can be frustrating if they want to pause mid-section. Test with real users and consider offering a “disable snap” toggle if your audience needs it.

Alternatives to Scroll Snap

If scroll snap doesn’t fit your needs, other approaches exist:

  • JavaScript libraries—FullPage.js or similar libraries offer more control (e.g., custom animations, keyboard navigation) but add JavaScript overhead.
  • CSS scroll-behavior only—If you just want smooth scrolling without snapping, use scroll-behavior: smooth alone.
  • Intersection Observer—You can build custom scroll-triggered effects (like fading in sections) with the Intersection Observer API, but you still need to handle the snapping yourself.

Frequently Asked Questions

Does scroll snap work on mobile devices?

Yes, scroll snap is supported on all modern mobile browsers, including iOS Safari and Android Chrome. The behavior is especially useful for swipeable carousels and full-screen sections on touch devices. However, test on real devices because the feel can vary slightly from desktop.

Can I use scroll snap with a horizontal slider?

Yes, set scroll-snap-type: x mandatory on the container and give children scroll-snap-align: start (or center). This is a common pattern for image carousels and product galleries. You can also combine with scroll-snap-stop: always to prevent skipping multiple items in one swipe.

Why is my scroll snap not working on the whole page?

Scroll snap applies to a scroll container, not the document by default. If you want the entire page to snap, apply scroll-snap-type to the html element (or body with height: 100%). Also ensure the sections have a defined height that matches the viewport.

How do I create a scroll snap carousel with multiple visible items?

Set the container to scroll-snap-type: x proximity and give each item scroll-snap-align: start with a width like 80% of the container. The proximity value lets the user stop between items if they want, while still snapping to the nearest one.

Is scroll snap the same as smooth scrolling?

No. Smooth scrolling is the animated transition when you scroll (via scroll-behavior: smooth). Scroll snap is the alignment of the scroll position to specific points. They are often used together, but they are separate features.

Can I use scroll snap with CSS grid?

Yes, scroll snap works with any layout, including grid and flexbox. Just ensure the container has a fixed height and overflow set, and the grid items (or children) have scroll-snap-align set. This is useful for creating a grid of cards that snap horizontally.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →