The CSS scroll-snap-type property enables snap scrolling, where the scroll position locks onto specific points. This creates smooth, precise, app-like navigation in sliders, carousels, galleries, timelines, and full-page scroll layouts.
Note
β Determines the axis + strictness of snapping
β Requires scroll-snap-align on child elements
π¦ Syntax
scroll-snap-type syntax
scroll-snap-type: none | x | y | block | inline | both mandatory | both proximity;| Value | Description |
|---|---|
| none | No snapping (default) |
| x | Snaps horizontally |
| y | Snaps vertically |
| block | Snap along block axis (vertical for English) |
| inline | Snap along inline axis (horizontal for English) |
| both | Snap in both directions |
| mandatory | Snap must occur β cannot stop between snap points |
| proximity | Snap only when near a snap point |
Note
π How Snapping Works
scroll-snap-type goes on the container, while scroll-snap-align goes on the children.
Scroll container
.container {
overflow-x: auto;
scroll-snap-type: x mandatory;
}Snap targets
.slide {
scroll-snap-align: center;
}π¨ 1. Horizontal Slider
Horizontal snapping
.slider {
display: flex;
overflow-x: scroll;
scroll-snap-type: x mandatory;
}
.slide {
scroll-snap-align: start;
}Each slide snaps to the left edge.
π¨ 2. Vertical Step-by-Step Sections
Vertical snapping
body {
scroll-snap-type: y mandatory;
}
section {
height: 100vh;
scroll-snap-align: start;
}Great for full-page scroll experiences.
π¨ 3. Proximity Snapping (Soft Snapping)
Proximity
.gallery {
overflow-x: auto;
scroll-snap-type: x proximity;
}Snaps only when close β the user can still stop mid-way.
π¨ 4. Snapping in Both Directions
Both directions
.grid-scroll {
overflow: auto;
scroll-snap-type: both mandatory;
}
.item {
scroll-snap-align: center;
}Useful for maps, large canvases, or 2D UI layouts.
π¨ 5. Inline & Block Logical Snapping
Respects writing-mode; useful for international layouts.
Logical snap types
.logical-scroll {
scroll-snap-type: block mandatory; /*.vertical for English*/
}π Combine With scroll-padding & scroll-snap-stop
scroll-snap ecosystem
.container {
scroll-snap-type: x mandatory;
scroll-padding-left: 20px;
}
.item {
scroll-snap-align: start;
scroll-snap-stop: always;
}This combination gives precise and strict snapping behavior.
π§ͺ Real-World Use Cases
1οΈβ£ Carousels / Sliders
Carousel
.carousel {
display: flex;
overflow-x: scroll;
scroll-snap-type: x mandatory;
}2οΈβ£ Onboarding Screens
Onboarding
.screen {
height: 100vh;
scroll-snap-align: start;
}3οΈβ£ Product Image Viewer
Product viewer
.product-images {
scroll-snap-type: x proximity;
}4οΈβ£ Timeline Navigation
Timeline
.timeline {
scroll-snap-type: x mandatory;
}β οΈ Common Mistakes
- β Forgetting to add scroll-snap-align to child elements
- β Expecting smooth animation without scroll-behavior: smooth
- β Using mandatory when free scrolling is desired
- β Snapping without enough scrollable overflow
Note
scroll-snap-align = element alignment
scroll-snap-stop = strictness
π₯ Summary
scroll-snap-type is the core of scroll snapping. It defines the snapping axis and behavior, making scroll interactions smooth, elegant, and predictable.