What is scroll-snap-type in CSS

scroll-snap-type is a CSS property that helps control how scrolling works on an element. It allows you to specify how the scrolling should snap to certain elements within the scrollable content. For example, you can use it to make sure that the scroll position always snaps to the beginning of an element, or to make sure that the scroll position always snaps to the closest element.

Here's an example of how you might use scroll-snap-type in your CSS:

scroll-snap-type is a CSS property that helps control how scrolling works on an element.

CSS

HTML

In this example, the .scrollable element is a container for a scrollable area, and the div elements within it are the elements that the scroll position should snap to. The scroll-snap-type property is set to x mandatory, which means that the scroll position will always snap to the closest element in the horizontal axis (the x axis). The scroll-snap-align property is set to start, which means that the scroll position will always snap to the start of the element.

The scroll-snap-type property can have the following values:

  • none

    : The element is not snapped to any positions as the user scrolls.

  • mandatory

    : The element is snapped to the nearest snap position as the user scrolls.

  • proximity

    : The element is snapped to the nearest snap position as the user scrolls, but only if the element is close enough to the snap position.

You can also use it in combination with other properties like scroll-snap-points and scroll-snap-destination to further control how scrolling works on your element.