CSS Scroll Snap feature allows you to control a scrolling experience on a specific element.
In order to do that, you need to declare scroll snapping positions.
Generally this feature is useful with image carousels or sliders where you need to push a container and bring the next one into focus.
scroll-snap-type
To enforce strict scrolling on a container, you need to use scroll-snap-type property.
Values
Main keyword values are none, x, y, both, block and inline.
Optionally you can also add a value next to the main keyword values from these two values: mandatory or proximity.
The global values work as well if parent element has scroll-snap-type property defined. You can inherit, initial or unset (in-case you don’t want to enforce it).
scroll-snap-align
You also need to specify scroll-snap-align property for the child elements of the main container in order to set the alignment within the container.
The value are none, start, end and center. You can also use start end together, in that case the first block is set as start and the second as inline.
Examples
Without controlled Scrolling
scroll-snap-type: none
It’s basically same result.
scroll-snap-type: x mandatory
Look at this controlled scrolling now:
With mandatory scroll type, the scroll offset will be adjusted as you can see in the example above.
The same rule applied to scroll-snap-type: y mandatory
scroll-snap-type: x proximity
With proximity scroll type, the scroll offset may get adjusted or may not.
The same rule applied to scroll-snap-type: y proximity
In a 2D image grid, you might want to set scroll-snap-type: both mandatory to make it scrollable on both axis while forcing a mandatory snap view.
property scroll scroll-behavior scroll-snap-type