CSS scroll snap
The CSS scroll snap module provides properties that let you control the panning and scrolling behavior by defining snap positions. Content can be snapped into position as the user scrolls overflowing content within a scroll container, providing paging and scroll positioning.
This module includes the scroll container scroll-padding properties to adjust the optimal viewing region of paging during scroll-into-view operations. It also includes scroll-margin and scroll-alignment, set on the scroll container's children, to adjust the children's visual area when that child is scrolled into view, as well as a property to force scrolling to stop on individual children.
Scroll snap in action
To view scroll snapping in the box below, scroll up-and-down and left-and-right through the grid of 45 numbered boxes in the scrollable viewport. Click "Play" in the example below to view the or edit the source in the MDN Playground:
With scroll snap, one of the numbered boxes that you scroll to will snap into place. The initial CSS makes the numbered box snap into the center of the viewport. Use the sliders to change the block and inline snap positions.
Using snap properties, you can allow or block the scrolling past an element, a numbered box in this case. Select the "Prevent scrolling past boxes" checkbox to force all scrolling actions to be limited to scrolling to an adjacent box.
To compare scroll snapping to regular scrolling, check the "disable snapping" checkbox and try scrolling again.
Reference
Properties on containers
Properties on children
Events
scrollsnapchange
Experimentalscrollsnapchanging
Experimental
Interfaces
SnapEvent
ExperimentalSnapEvent.snapTargetBlock
ExperimentalSnapEvent.snapTargetInline
Experimental
Guides
- Basic concepts of CSS scroll snap
-
An overview and examples of CSS scroll snap features.
- Using scroll snap events
-
A guide to using the
scrollsnapchanging
andscrollsnapchange
scroll snap events that are fired when the browser determines a new snap target is pending or selected.
Related concepts
:target
pseudo-classoverflow
CSS property- Element
scroll()
method - Element
scrollBy()
method - Element
scrollIntoView()
method - Element
scrollTo()
method - Element
scroll
event - Element
scrollend
event scrollbar
ARIA role- Scroll container glossary term
Specifications
Specification |
---|
CSS Scroll Snap Module Level 1 |
CSS Scroll Snap Module Level 2 |
See also
- CSS overflow module
- CSS scrollbars styling module
- Keyboard-only scrolling areas on adrianroselli.com (2022)
- Scroll snap examples on CodePen (2022)
- Well-controlled scrolling with CSS scroll snap on web.dev (2021)
- Practical CSS scroll snapping/ on CSS-Tricks (2020)
- CSS scroll snap on 12 Days of Web (2019)