My Blog

My WordPress Blog

What is CSS Scroll Snapping? How can it be used to build practical interfaces?

Ever wondered how a scrollable container is made with a snapping effect using only CSS? This article will help you understand what it is and how to make a basic one.

Now, before diving into the topic, let’s understand first what is a snapping effect.

A snapping effect in CSS Scroll Snap refers to a behavioral feature where an element, typically during scrolling, aligns itself with specific points or positions. In simple words, the snapping effect in CSS scroll snap is like a magnet that guides your scroll to specific points. Instead of scrolling freely, it gently aligns your scroll to designated positions.

Now that we all know what is a snapping effect, let’s understand the CSS feature that is used to create a scrollable container with a snapping effect.

CSS SCROLL SNAP

CSS Scroll Snap is a CSS feature that provides a way to create a snapping effect when scrolling through a container, making it easier to control the scrolling behavior and align elements within the container.

CSS Scroll Snap is like a friendly guide for your webpage’s scrolling. Imagine you have a long page with different sections, and when you scroll, instead of the content feeling like it’s sliding all over the place, it smoothly stops at specific points, aligning perfectly with your screen which will not only enhance the user experience, but also it will give more control. This is why CSS scroll snap should be used.

Representation of simple scrolling
Simple Scrolling without snapping at any point
Scroll Snapping effect representation
Scrolling with snapping effect after each scroll

This is a comparison between scrolling vs scroll snap. The first gif shows a simple scrolling feature while the second gif shows a snapping effect while scrolling. The reason why scroll snap should be used is that it is a good user experience when compared to simply just swiping.

How can it be used to build practical interfaces?

Now to create one, what we need is a container that can be created using divs, but the important tip here is to use:

  • overflow other than the value visible.
  • a way to display items next to each other which can be achieved using flexbox.

There are a lot of properties css scroll snap feature has, but let’s see the important one that will help us create a snapping effect.

Scroll Snap Type

CSS Scroll Snap Type property defines how the elements will be snapped into focus and in what direction when scrolling ends. It is like telling your webpage how it should react when someone scrolls through it. The content can be moved horizontally, vertically or both and should it be stopped at specific points or whenever it feels natural. Scroll snap type is applied on the parent container. It has different values, let’s see the most commonly used one by one to understand them.

  1. None: None is the default value, so there will be no scroll snap effect.
  2. ‘x’ or ‘y: X or Y means when the value is set on x-axis, scroll snap effect will be on x-axis which is horizontal and y means that the scroll snap effect will be on y-axis which is vertical.
  3. Both: Scroll snap effect is on both axis, horizontally and vertically as well.
  4. Mandatory: In mandatory, scroll will automatically move to snap point after scroll action is finished
  5. Proximity: In proximity, Similar to mandatory, but not as strict. Scroll will automatically move to snap point after scroll action is finished, but in between snap points there is an area without the snap effect. Depends on browser parameters.
Scroll Snap type example with mandatory value

CSS Syntax:
scroll-snap-type: none | x | y | both | mandatory | proximity ;

Scroll Snap Align

CSS Scroll Snap Align property represents the snap position of a box’s snap area within the element’s or container’s snap port. In simple words, it specifies where the elements will be snapped into focus when you stop scrolling. Scroll snap align is applied on the child container. It also has different values, let’s understand the most used one.

  1. None: None is the default value, so there will be no scroll snap effect.
  2. Start: The scroll snap effect will be on the start of the elements both x-axis and y-axis.
  3. End: The scroll snap effect will be on the end of the elements both x-axis and y-axis.
  4. Center: The scroll snap effect will be on the conter of the elements both x-axis and y-axis.
Example which shows scroll snap align effect

CSS Syntax:
scroll-snap-align: none | start | end | center;

Why to use it?

It can be used for things like creating cool image carousels where pictures slide into place, making navigation menus that snap to different sections, or even designing presentations where each slide smoothly appears as you scroll down. It’s like arranging content in a way that feels just right, making the whole website experience more enjoyable and user-friendly.

Browser Support

browsers that supports CSS Scroll Snap feature

This shows the browser support of CSS Scroll Snap feature which is like 10/10 because all of the modern browsers supports this feature and there’s no reason not to use this cool CSS feature.

References:

https://ishadeed.com/article/css-scroll-snap/

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_scroll_snap

https://medium.com/@harish_rajora/css-scroll-snap-property-complete-guide-with-practical-examples-88588f8bc725