Scroll-snap
Learn scroll-snap-type and scroll-snap-align properties.
What is scroll-snap-type?
The scroll-snap-type property decides how items align after a scroll in a scroll container. It takes two values: axis and strictness. The axis can be x, y, or both; the strictness is mandatory or proximity.
For example, scroll-snap-type: y mandatory forces vertical scrolls to always settle on a snap point. This is handy for slideshows or galleries.
Forcing stops with scroll-snap-stop
Sometimes a fast scroll makes the user skip multiple snap points. The scroll-snap-stop: always declaration forces a stop at each snap point; no skipping. The default value is normal.
For example, in a presentation where each slide must be seen, you add scroll-snap-stop: always to each slide. Even a fast scroll stops at the next slide.
Exercise Vertical scroll snapping
Write two blocks. First, a CSS block for the .scroller selector; this block must contain the scroll-snap-type: y mandatory declaration. Second, a CSS block for the .scroller > section selector; this block must contain the scroll-snap-align: start declaration.
Show example solution
/* vertical scroll snapping */
.scroller {
scroll-snap-type: y mandatory;
}
.scroller > section {
scroll-snap-align: start;
}
Test yourself
-
What is the difference between mandatory and proximity?
- mandatory works only on the x axis, proximity only on the y axis
- mandatory always snaps to the point, proximity snaps only when close
- mandatory scrolls faster, proximity slower
- mandatory is for desktop, proximity is for mobile only
Show answer
mandatory always snaps to the point, proximity snaps only when close — Correct. mandatory always snaps; proximity snaps only when close.
-
Write the alignment position
/* gallery item alignment */ .gallery > * { scroll-snap-align: ____; }Show answer
/* gallery item alignment */ .gallery > * { scroll-snap-align: center; } -
Match each property to its meaning
- scroll-snap-type
- Sets axis and strictness
- scroll-snap-align
- Item alignment to snap point
- scroll-snap-stop
- Forces a stop at each snap point
- scroll-padding
- Adds padding for snap points