CSS Scroll Snap

Introduction

The “scroll-snap” CSS property is used to control the “snapping” behavior of a container’s scroll positions. “scroll-snap” property, allow web developer create a smoother scrolling experience, especially in cases where the content is divided into distinct sections or elements.

Basic Usage

Scroll snapping is used in combination with the “scroll-snap-type” property on a container element and the “scroll-snap-align” property on elements inside it

<section class="container">
    <div class="class"> </div>
    <div class="class"> </div>
    <div class="class"> </div>
    <div class="class"> </div>
</section>
.container {
    scroll-snap-type: y mandatory
}

.child {
    scroll-snap-align: start
}

result

1
2
3
4
5
6

Parent Properties

#scroll-snap-type

“scroll-snap-type” property is applied to the container(parent) element. It defines the scrolling axis and the type of snapping behavior.

  • scroll-snap-type: none;

    no snapping behavior.

  • scroll-snap-type: x;

    the scroll container snaps to snap position in its horizontal axis only.

  • scroll-snap-type: y;

    the scroll container snaps to snap position in its vertical axis only.

  • scroll-snap-type: both;

    the scroll container snaps to snap position in both of its axes independently (potentially snapping to different elements in each axis).

  • scroll-snap-type: mandatory;

    the scrolling will always snap to the closest snap point after scroll operation.

  • scroll-snap-type: proximity;

    the scrolling will snap to closest snap point if it’s within a certain distance.

#"mandatory" VS "proximity"

mandatory

the scrolling will always snap to the nearest snap point after scroll operation, regardless of how far or close the scroll position is to the snap points. In other words, it enforces a strict snapping behavior, ensuring that the scroll position align precisely with a snap point.

proximity

the scrolling will snap to the nearest snap point only if it’s within a certain distance. If the scroll position is close enough to a snap point, it will snap, otherwise, it will not.

#scroll-padding

“scroll-padding” property in CSS is used to set the padding area for a scroll container. The “scroll-padding” property allows web developer to define additional space around the content inside the scroll container. The extra space influences the calculation of the snap points. It can take one to four values, each representing the padding for the top, right, bottom, and left sides. These value can be specified in pixel, percentages, or other length units.

Child Properties

#scroll-snap-align

“scroll-snap-align” is applied to the child elements inside the container. It defines where the snap point of each child element should be.

  • scroll-snap-align: start;

    the snap point is at the start of the element.

  • scroll-snap-align: center;

    the snap point is the end of the element.

  • scroll-snap-align: end;

    the snap point is at the end of the element.

#scroll-snap-stop

“scroll-snap-stop” property defines whether or not the scroll container is allowed to “pass over” possible snap position. The effect of this property can be noticed on device with a touchpad.

  • scroll-snap-stop: normal;

    when the visual viewport of this element’s scroll container is scrolled, it may “pas over” possible snap positions

  • scroll-snap-stop: always;

    tthe scroll container must not “pass over” a possible snap position, and must snap to the first of this element’s a snap position

#scroll-margin

“scroll-margin” property is used to set additional space around scrollable elements. This property controls how far the element can fit into the scrollable area before “scroll snapping” of shifting of neighboring elements begins. It can take one to four values, each representing the padding for the top, right, bottom, and left sides. These value can be specified in pixel, percentages, or other length units.

Example

#scroll-snap-type

x mandatory

1
2
3
4
5
6

x proximity

1
2
3
4
5
6

y mandatory

1
2
3
4
5
6

y proximity

1
2
3
4
5
6

#scroll-padding

padding 20px on container element

1
2
3
4
5
6

#scroll-snap-align

x-axis with align start

1
2
3
4
5
6

x-axis with align center

1
2
3
4
5
6

x-axis with align end

1
2
3
4
5
6

y-axis with align start

1
2
3
4
5
6

y-axis with align center

1
2
3
4
5
6

y-axis with align end

1
2
3
4
5
6

#scroll-snap-stop

y-axis with stop normal

1
2
3
4
5
6

y-axis with stop always

1
2
3
4
5
6

#scroll-margin

margin right & left on 2nd child

1
2
3
4
5
6