CSS Reference ยท Scrolling, Interaction and UI
The scroll-snap-type property
Reference for the CSS scroll-snap-type property: turn a scroll container into a snapping carousel, choose the axis and strictness, with a live example.
The scroll-snap-type property turns an element into a snap container: after the user finishes scrolling, the box settles on one of the snap positions defined by its children, instead of stopping at an arbitrary offset. You choose which axis snaps and how strict the snapping is.
The property alone does nothing useful. The children must say where they snap, using scroll-snap-align. The container needs overflow set to something that scrolls, such as auto.
.carousel { overflow-x: auto; scroll-snap-type: x mandatory; }
Try it
Syntax
scroll-snap-type: none; scroll-snap-type: <axis> [ mandatory | proximity ]?; /* axis: x | y | block | inline | both */
- Initial value:
none - Applies to: scroll containers
- Inherited: no
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
none |
No snapping. |
x / y |
Snap along the horizontal or vertical axis. |
block / inline |
Snap along the block or inline axis of the writing mode. |
both |
Snap in both directions independently. |
mandatory |
The container must rest on a snap point whenever one exists. |
proximity |
The browser may snap if the resting position is near a snap point. This is the default strictness when you omit the keyword. |
Usage notes
- Use
mandatoryfor carousels where every position is a slide. Useproximityfor long pages where users should stay free to stop anywhere. - With
mandatory, make sure a snap point is reachable for all content; very tall slides that do not fit the scrollport can trap or hide content that sits between snap points. - Give a keyboard user a way to move: scroll containers should be focusable (for example
tabindex="0") or contain focusable controls, and add buttons if the carousel hides content. - Combine with
scroll-paddingto offset the snap position, andoverscroll-behaviorto stop sideways swipes from navigating back. - Setting it on
htmlsnaps the whole page, but a mandatory page-level snap on long content is rarely a good experience.
Browser support
Supported in every current major browser, with the modern axis and strictness syntax described above.
Related
scroll-snap-alignโ where each child snapsscroll-snap-stopโ prevent skipping slidesoverflowโ makes the box scroll- Lesson: Scroll Snap