The scroll-snap-type
property needs to know the axis along which scroll snapping happens. This can be x
, y
, or the logical mappings block
or inline
. You can also use the keyword both
to have scroll snapping work along both axes.
You can also pass in the keywords mandatory
or proximity
. The mandatory
keyword tells the browser whether the content has to snap to a certain point, no matter where the scroll is. The proximity
keyword means that the content may snap to the point, but does not have to.
Using mandatory
creates a very consistent scrolling experience — you know the browser will always snap to each defined point. This means that you can be confident that something you expect to be at the top of the screen will be there when scrolling finishes. However, it can cause problems if the content is larger than you expect — users may find themselves in the frustrating position of never being able to scroll and view a certain point in the content. Therefore, the use of mandatory
should be carefully considered and only used in situations where you know how much content is on the screen or scrollable section at any time.
Note: Never use mandatory
if the content inside one of your child elements will overflow the parent container because user will not be able to scroll the overflowing content into view.
The proximity
value only snaps child elements to a position when it is close by, with the browsers determining the exact distance. Click "Play" to edit the example below in the MDN Playground. Alternate the scroll-snap-type
value between mandatory
and proximity
to see the effect this has on the scroll experience.
<article class="scroller">
<section>
<h2>Section one</h2>
<p>
Turnip greens yarrow ricebean rutabaga endive cauliflower sea lettuce
kohlrabi amaranth water spinach avocado daikon napa cabbage asparagus
winter purslane kale. Celery potato scallion desert raisin horseradish
spinach carrot soko.
</p>
</section>
<section>
<h2>Section two</h2>
<p>
Turnip greens yarrow ricebean rutabaga endive cauliflower sea lettuce
kohlrabi amaranth water spinach avocado daikon napa cabbage asparagus
winter purslane kale. Celery potato scallion desert raisin horseradish
spinach carrot soko.
</p>
</section>
<section>
<h2>Section three</h2>
<p>
Turnip greens yarrow ricebean rutabaga endive cauliflower sea lettuce
kohlrabi amaranth water spinach avocado daikon napa cabbage asparagus
winter purslane kale. Celery potato scallion desert raisin horseradish
spinach carrot soko.
</p>
</section>
</article>
.scroller {
height: 300px;
overflow-y: scroll;
scroll-snap-type: y mandatory;
}
.scroller section {
scroll-snap-align: start;
}
In the above example, both height: 300px;
and overflow-y: scroll;
are set on the scroll container. If the content doesn't overflow its container, there is nothing to scroll.