Reading history

CSS Scroll Snap and Smooth Scrolling

5 min read 0 views

Build a swipeable carousel with scroll-snap-type and scroll-snap-align, add smooth in-page scrolling, keep targets clear of sticky menus, and respect reduced motion.

Swipeable image carousels, story viewers and one-page websites with menu links that glide to each section used to need JavaScript libraries. Today CSS handles them on its own. Scroll snap makes a scroll container stop neatly at chosen points, and smooth scrolling animates jumps to links on the same page. Both are light, fast and work well with touch screens.

In this lesson you will learn

  • Building a carousel with scroll-snap-type and scroll-snap-align
  • The difference between mandatory and proximity
  • Smooth scrolling with scroll-behavior
  • Stopping headings hiding under a sticky menu with scroll-margin-top
  • Respecting users who prefer reduced motion

You should know Flexbox, overflow and positioning.

A Scroll-Snap Carousel

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Scroll snap</title>
  <style>
    body { font-family: sans-serif; margin: 12px; font-size: 14px; }
    .carousel {
      display: flex; gap: 10px; overflow-x: auto;
      scroll-snap-type: x mandatory;           /* snap horizontally, always */
      padding-bottom: 6px;
    }
    .slide {
      flex: 0 0 80%;
      height: 90px; border-radius: 8px; color: #fff;
      display: grid; place-items: center; font-size: 18px;
      scroll-snap-align: center;                /* each slide stops in the middle */
    }
    .slide:nth-child(1) { background: #ef5350; }
    .slide:nth-child(2) { background: #ab47bc; }
    .slide:nth-child(3) { background: #42a5f5; }
    .slide:nth-child(4) { background: #26a69a; }
  </style>
</head>
<body>
  <div class="carousel">
    <div class="slide">Slide 1</div><div class="slide">Slide 2</div>
    <div class="slide">Slide 3</div><div class="slide">Slide 4</div>
  </div>
  <p>Scroll sideways: each slide snaps into place.</p>
</body>
</html>

Result

  • The carousel is a flex row with overflow-x: auto, so it scrolls sideways. flex: 0 0 80% keeps each slide at 80% of the width, leaving a peek of the next slide as a hint to swipe.
  • scroll-snap-type: x mandatory goes on the container: snap along the x-axis, and always come to rest on a snap point.
  • scroll-snap-align: center goes on each child: the slide’s centre lines up with the container’s centre. start and end are the other choices.
  • Use proximity instead of mandatory when the content is long; the browser then snaps only if the user stops near a snap point, so they can still rest between them.
  • scroll-snap-stop: always on the slides stops a fast swipe from skipping past several slides at once.

Smooth Scrolling and a Sticky Menu

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Smooth scroll and sticky</title>
  <style>
    html { scroll-behavior: smooth; }
    body { font-family: sans-serif; margin: 0; font-size: 14px; }
    nav {
      position: sticky; top: 0;              /* stays at the top while you scroll */
      background: #263238; padding: 8px; display: flex; gap: 12px;
    }
    nav a { color: #fff; }
    section { padding: 12px; min-height: 160px; scroll-margin-top: 40px; }
    #one { background: #e3f2fd; } #two { background: #f1f8e9; } #three { background: #fff3e0; }
    @media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
  </style>
</head>
<body>
  <nav><a href="#one">One</a><a href="#two">Two</a><a href="#three">Three</a></nav>
  <section id="one"><h3>Section one</h3><p>Click a link above: the page glides there.</p></section>
  <section id="two"><h3>Section two</h3><p>The menu sticks to the top.</p></section>
  <section id="three"><h3>Section three</h3><p>scroll-margin-top stops the heading hiding under the menu.</p></section>
</body>
</html>

Result

  • html { scroll-behavior: smooth; } makes jumps to #one, #two and other same-page links glide instead of jump.
  • position: sticky; top: 0 keeps the menu at the top while the page scrolls underneath.
  • A sticky menu would normally cover the top of the section you jump to. scroll-margin-top: 40px on the sections leaves that much space above them when they become a scroll target.
  • The prefers-reduced-motion: reduce media query switches smooth scrolling off for people who have asked their device for less movement, because long animated scrolls can cause dizziness.

Property Reference

PropertySet onCommon values
scroll-snap-typeContainerx mandatory, y proximity, both mandatory
scroll-snap-alignChildrenstart, center, end
scroll-snap-stopChildrennormal, always
scroll-paddingContainerSpace kept free at the edge, e.g. for a fixed header
scroll-marginTarget elementSpace kept around the element when scrolled to
scroll-behaviorScroll container (often html)auto, smooth

Making Carousels Usable

  • Visible arrows or dots help people who use a mouse, as not everyone knows to scroll sideways with shift and the mouse wheel.
  • Make the container focusable with tabindex="0" and give it an aria-label so keyboard users can scroll it with the arrow keys.
  • Avoid auto-advancing slides, or provide a pause button.

Common Mistakes

  1. Putting scroll-snap-type on the slides instead of the scrolling container.
  2. Forgetting overflow: without a scrollable container nothing can snap.
  3. Using mandatory with slides taller than the screen, which can make parts of a slide impossible to read.
  4. Ignoring reduced-motion preferences for smooth scrolling.

Practice Exercises

Exercise 1. Make a vertical full-screen “story” page where each section fills the screen and snaps to its top.

Show answer
main { height: 100vh; overflow-y: auto; scroll-snap-type: y mandatory; }
section { height: 100vh; scroll-snap-align: start; }

Exercise 2. A fixed header is 64px tall. Make every in-page jump land just below it, using one rule on html.

Show answer
html { scroll-padding-top: 64px; }

Frequently Asked Questions

Can JavaScript scroll smoothly too?

Yes. element.scrollIntoView({ behavior: “smooth” }) and window.scrollTo({ top: 0, behavior: “smooth” }) both animate the scroll, and they work well for “next slide” buttons.

Does scroll snap work on phones?

Yes. It is supported in all modern mobile and desktop browsers, and on touch screens it feels like a native app’s swipe gallery.

Summary

  • scroll-snap-type on the container and scroll-snap-align on children create snapping.
  • scroll-behavior: smooth animates same-page jumps.
  • scroll-margin-top or scroll-padding-top keeps targets clear of sticky headers.
  • Turn smooth scrolling off for prefers-reduced-motion: reduce.

Next: Web Performance Basics.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *