# Scroll Snap & Smooth Scroll

> Build native carousels and smooth-scrolling anchor links without writing any JavaScript.

Subject: CSS | Lesson 36 of 44 | 7 min read
Interactive lesson: https://codewithanimation.in/css/scroll-snap-and-behavior
Previous: [Clip-Path & Masking](https://codewithanimation.in/css/clip-path-and-masking.md) | Next: [CSS Nesting](https://codewithanimation.in/css/css-nesting.md)

## What is CSS Scroll Snapping?

In the past, building a swipeable image carousel or a smooth-scrolling page required complex JavaScript libraries.

Modern CSS provides native scrolling behavior controls. **Scroll Snapping** allows you to lock the scroll position to specific elements (like snapping to the next image in a gallery).

**Smooth Scrolling** allows the browser to smoothly animate down the page when a user clicks a table-of-contents link, instead of instantly jumping.

## Why use native CSS over JavaScript plugins?

Native CSS scrolling is handled by the browser's compositor thread, making it incredibly smooth and completely immune to JavaScript main-thread locking (jank).

It also respects user device preferences automatically (like stopping the snap animation if the user has requested reduced motion), and requires zero megabytes of JS dependencies.

## How do you build a native carousel?

First, tell the scrolling container how to behave: `scroll-snap-type: x mandatory;` (scroll horizontally, and force the browser to always stop on an element).

Second, tell the children where to align when they snap: `scroll-snap-align: center;` (stop with the child exactly in the center of the container).

## Common mistake: The sticky header trap

When you click an anchor link (e.g., `<a href="#section2">`), the browser scrolls down so that the top of `#section2` hits the very top of the window.

If you have a fixed navigation bar at the top of your screen, `#section2` will be hidden behind it! Fix this by adding `scroll-margin-top: 80px;` to the section to give it breathing room.

## Tip: scroll-behavior: smooth

Applying `scroll-behavior: smooth;` to your `<html>` or `<body>` tag instantly turns all harsh anchor link jumps into beautiful, smooth animations across your entire website.

## Try it: Try it

Configure the native CSS carousel by toggling the snap rules, then test the smooth scrolling anchor links in the mock page.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/scroll-snap-and-behavior

## Check yourself

### Question 1

Where must you apply the scroll-snap-align property?

- A. On the scrollable parent container.
- B. On the child elements inside the container.
- C. On the html tag.

Correct answer: B

The parent container defines the rules (`scroll-snap-type: x mandatory`), but the individual child items declare where they want to stop (`scroll-snap-align: center`).

- Why not A: The parent gets scroll-snap-type.
- Why not C: This would try to snap the whole page.

### Question 2

What is the difference between mandatory and proximity scroll snapping?

- A. mandatory forces a snap even if you barely scroll, proximity only snaps if you are close.
- B. mandatory is for vertical scrolling, proximity is for horizontal.
- C. mandatory requires JavaScript, proximity is CSS only.

Correct answer: A

Use `mandatory` for strict carousels where resting between items is unacceptable. Use `proximity` for long reading pages where snapping to a section is nice, but stopping halfway through is also okay.

- Why not B: Both can be used in either direction.
- Why not C: Both are entirely native CSS.

### Question 3

How do you prevent anchor links from jumping underneath a sticky navigation bar?

- A. Add padding-top to the body.
- B. Add margin-top to the sticky header.
- C. Add scroll-margin-top to the target elements.

Correct answer: C

`scroll-margin-top` tells the browser's scrolling engine to stop slightly above the element when navigating to its ID, leaving perfect clearance for your header.

- Why not A: This fixes the initial page load, but not anchor jumps.
- Why not B: This just pushes the header down.

## Remember this

- scroll-snap-type goes on the parent; scroll-snap-align goes on the children.
- Use scroll-margin-top to prevent anchor links from getting buried under sticky headers.
- Apply scroll-behavior: smooth to enable animated anchor jumping.
