Codectionary / Developer documentation / CSS

scroll-behavior and Scroll Snap

scroll-behavior: smooth animates scrolling to anchors instead of jumping instantly. Scroll snap properties (scroll-snap-type and scroll-snap-align) create carousel-like scrolling where content snaps neatly into position, commonly used for image galleries and full-page sections, without any JavaScript.

Syntax

scroll-behavior: smooth;
scroll-snap-type: x mandatory;

Examples

Smooth Anchor Scrolling

Making in-page anchor links (like #section-2) scroll smoothly instead of jumping.

html {
  scroll-behavior: smooth;
}
/* Now <a href="#section-2"> smoothly animates the scroll, no JS needed */

A CSS-Only Snap Carousel

Building a horizontally scrolling gallery that snaps to each item.

.gallery {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  gap: 16px;
}

.gallery img {
  scroll-snap-align: center; /* each image snaps to center as the user scrolls */
  flex: 0 0 80%;
}

Parameters and values

  • scroll-behavior (auto | smooth): Controls whether scrolling to anchors/targets is animated
  • scroll-snap-type (x | y | both, mandatory | proximity): Enables snap scrolling on a container
  • scroll-snap-align (start | center | end): Sets where a child snaps to within the container

Best practices

  • Use scroll-behavior: smooth as a simple, one-line upgrade for anchor link navigation, with no JavaScript required
  • Use mandatory snap type for carousels where content should always land precisely on an item; use proximity for a softer, less forceful snap
  • Combine scroll-snap with overflow: auto/scroll on the container - snap properties do nothing without a scrollable container
  • Respect prefers-reduced-motion by disabling scroll-behavior: smooth for users who have requested reduced motion

At a glance

Purpose
Presentation and layout
File extension
.css
Runs in
Web browsers
Usually used with
HTML and JavaScript

Specifications & further reading

Related CSS documentation