LambdaTest Community

Find answers, support, and inspiration from other users

Questions
LambdaTest Community
Answered

What is CSS Scroll Snap? What are its properties?

Can anyone please explain me what is CSS Scroll Snap and also i want to know what are its properties.

Up Vote Down Vote 1 Votes
Posted 3 months ago

Answers


CSS Scroll Snap is a property triggered during the course of normal scrolling but here instead of stopping at the point where the user stopped the scroll event, the webpage snaps to a certain position described by the web developer. For example, if a user scrolls to half of the div box, the div box would automatically snap to the top of its edge (position depends on the developer). This process is similar to locking the viewport to a certain area of the webpage so that whenever the user is not in that area, the webpage automatically snaps to it.

To understand CSS scroll snap, it is better to see the difference first. The following GIF is created while scrolling normally (without applying any scroll-snap property).

The following GIF is created after applying the scroll-snap property.

If you missed it, in normal scrolling, I am able to stop in the middle of two different sections but it does not happen while I have the scroll-snap activated. It automatically snaps back. CSS scroll snap can be applied to any element in the webpage that the developer wants and on different axes too. Unlike the CSS position property, the CSS scroll snap requires the declaration of different properties to work together.

Properties of CSS Scroll Snap

The CSS scroll snap feature contains two properties that can be broadly categorized into the parent or the container property and the child property.

The Parent or Container Property (Scroll-Snap-Type):

This property needs to be specified to the parent of the element or the container element of the web page. The property that falls under this category is called scroll-snap-type. Scroll-snap-type property defines the type of behavior the developer wants to give to the scrolling container element. It contains two values that define the direction of the scroll and the behavior of the scroll.

Direction Of Scroll

The direction of the scroll is the direction in which you would like to apply the scroll-snap-type property. It has values of either x (for the horizontal direction) or y (for the vertical direction) or both (for scrolling in both of the directions).

Behaviour Of The Scroll

The behavior of the scroll means the type of scroll-snap you would like to apply. This contains two different values:

Mandatory: Mandatory value always takes the element to the scrolling point decided by the user irrespective of his scroll position.

Proximity: Proximity value takes the element to the scrolling point only when the user is closer to the scrolling point.

The following pen demonstrates the use of a scroll-snap on with both the values with the direction of the scroll being vertical. The code used is as follows:

For scroll-snap-type: y mandatory

Note: Scroll slowly in the result section to view the CSS scroll snap effect.

Up Vote Down Vote 0 Votes
Posted 3 months ago
205 Views
1 Answer
3 months ago