- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- Scroll Snap
CSS Effects
Scroll Snap
Create deliberate scroll destinations, offsets, and snapping without trapping users. You will build anchor offsets, smooth scrolling, horizontal snap galleries, proximity and mandatory snapping, and a reduced-motion fallback.
Two everyday scrolling problems have CSS fixes. A sticky header hides the heading you just jumped to, and a photo carousel stops halfway between two pictures. scroll-padding solves the first and scroll snap solves the second.
Both should help the reader, never fight them. Gentle help is fine; locking the scroll so someone cannot reach part of the content is not. This lesson connects with CSS Overflow and with Preference Media Queries for turning motion off.
Stop the sticky header from covering headings
Example
css
html {
scroll-behavior: smooth;
scroll-padding-block-start: 5rem;
}
section {
scroll-margin-block-start: 5rem;
}scroll-behavior: smooth makes jumps to #section links glide instead of snapping. scroll-padding-block-start: 5rem tells the page "there is a 5rem header at the top, stop below it". You can set the space on the page (scroll-padding, on the scroll container) or on each target (scroll-margin, on the section). Either one is enough; pick whichever is easier for your layout.
Make a carousel stop on each item
Example
css
.gallery {
display: grid;
grid-auto-flow: column;
grid-auto-columns: min(85%, 24rem);
overflow-x: auto;
scroll-snap-type: inline proximity;
}
.gallery > * {
scroll-snap-align: start;
}scroll-snap-type on the container turns snapping on, and scroll-snap-align on each item says where it should line up. With proximity, the browser only snaps when you finish scrolling close to an item. mandatory always snaps, which feels firm on a carousel but can trap readers when an item is taller than the screen or when they zoom in. Start with proximity.
Smooth scrolling should also respect people who get motion sick:
Example
css
@media (prefers-reduced-motion: reduce) {
html { scroll-behavior: auto; }
}Keep scrolling inside a panel
overscroll-behavior controls scroll chaining at a container boundary. Standard scrollbar properties can adjust width and color, but contrast and platform conventions must remain usable.
Example
css
.panel {
overflow: auto;
overscroll-behavior: contain;
scrollbar-width: thin;
scrollbar-color: #64748b #e2e8f0;
}overscroll-behavior: contain stops a common annoyance: you scroll to the end of a chat box or a modal and the whole page behind it starts scrolling. scrollbar-width and scrollbar-color change the scrollbar, but keep it easy to see.
When the page jumps by itself
Browsers normally adjust the scroll position when content above the viewport changes size. If that correction is harmful in a specific dynamic region, overflow-anchor: none opts that region out of anchor selection. Use it narrowly because disabling scroll anchoring can reintroduce unexpected page jumps.
Example
css
.live-scoreboard {
overflow-anchor: none;
}Scrolling Properties
| Property | Purpose | Example |
|---|---|---|
| scroll-behavior | Controls programmatic and anchor scrolling. | scroll-behavior: smooth |
| scroll-snap-type | Sets the snap axis and strictness. | scroll-snap-type: x mandatory |
| scroll-snap-align | Sets an item's snap position. | scroll-snap-align: start |
| scroll-margin | Adds space around a snap or anchor target. | scroll-margin-block: 5rem |
| overflow-anchor | Opts a region out of scroll-anchor selection. | overflow-anchor: none |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Scroll Snap example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
html {
scroll-behavior: smooth;
scroll-padding-block-start: 5rem;
}
section {
scroll-margin-block-start: 5rem;
}
.gallery {
display: grid;
grid-auto-flow: column;
grid-auto-columns: min(85%, 24rem);
overflow-x: auto;
scroll-snap-type: inline proximity;
}
.gallery > * {
scroll-snap-align: start;
}
@media (prefers-reduced-motion: reduce) {
html { scroll-behavior: auto; }
}
.panel {
overflow: auto;
overscroll-behavior: contain;
scrollbar-width: thin;
scrollbar-color: #64748b #e2e8f0;
}
.live-scoreboard {
overflow-anchor: none;
}
</style>
</head>
<body>
<main><nav><a href="#target">Jump to target</a></nav><section class="gallery"><article>Slide one</article><article>Slide two</article><article>Slide three</article></section><section id="target"><h2>Anchor target</h2></section></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| scroll-snap-type | 69 | 79 | 99 | 11 |
| scroll-behavior: smooth | 61 | 79 | 36 | 15.4 |
| overflow-anchor | 56 | 79 | 66 | 27 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Test it the way people scroll
Try it with a touchpad, a mouse wheel, a phone, the keyboard (arrow keys and Page Down) and at 200% zoom. Snapping that feels nice with one of them can feel broken with another.
Scroll Snap exercises
Smooth scrolling
Let the scroller scroll smoothly when a link or script moves it.
The HTML your code works on
<style>
.scroller { height: 150px; overflow-y: scroll; }
.slide { height: 150px; }
</style>
<div class="scroller">
<div class="slide">1</div>
<div class="slide">2</div>
<div class="slide">3</div>
</div>Result
