everything about web

A Comprehensive Guide to the World of Web Design & Development:

CSS, HTML, Performance, Web Design, and all Aspects of Front-End Development

Advertisements

CSS scroll-snap: Enhancing User Experience with Smooth Scrolling

What is smooth scrolling

Smooth scrolling is a crucial factor in capturing and maintaining the interest of website visitors and enhancing the overall experience. By incorporating scroll-snap CSS into our website, we will be able to gain greater control over scrolling behavior, resulting in a more intuitive and improved smoothness and predictability of the navigation experience, particularly benefiting the touch-based devices.

scroll-snap CSS offers precise control over scr oll behavior and defined snap points, leading to effortless content navigation and a user-friendly browsing experience. In this article, Let's delve into the fundamentals of scroll-snap CSS and example to showcase its potential.

Introducing scroll-snap CSS

In this tutorial, we’ll learn how to create a basic sliding carousel using HTML and CSS. The best part is, creating a basic snapping carousel only requires 2 simple steps. Let’s begin by setting up the HTML structure!

Step 1: Set up the HTML structure

First, Let’s create a parent scroll container <ul> , then add in 3 <li> elements as individual slides, for example:

<ul class="container">
   <li style="background: aqua;">Slide 1</li>
   <li style="background: aquamarine;">Slide 2</li>
   <li style="background: cadetblue;">Slide 3</li>
</ul>
Step 2: Define snapping behavior from CSS

Next, we now can add CSS styles to customize the appearance and behavior of the scroll container, for example:

.container{
    scroll-snap-type: y mandatory;
    overflow-y: scroll;
    overflow-x: hidden;
    width: 300px;
    height: 300px;
}
li{
    scroll-snap-align: start;
    width: 300px;
    height: 300px;
    line-height: 300px;
    text-align: center;
    list-style: none;
}

By setting up scroll-snap-type: y mandatory;, the content inside the scroll container .container will smoothly align and snap to predetermined positions as you scroll up or down vertically.

In this case, we will also utilize the scroll-snap-align: start; property to ensure that each item within the container aligns itself with the starting point of the scroll container.

Take a look at the result

Congratulations! You have already completed the basic sample of the scroll-snap tutorial. If you are interested to explore more available options for snapping behavior, you can refer to the next section for available options about scroll-snap-type values. Happy coding!

To try out a full demo: click me

scroll-snap-type values
valuesdescription
noneThe scroll container will not snap to any snap points
xThe scroll container only snaps to specific positions along x-axis
yThe scroll container only snaps to specific positions along y-axis
mandatoryThe scroll container will always snaps precisely at the defined snap points, regardless of the user’s scrolling behavior
proximityThe scroll container will align with a snap point on the screen if it is not actively being scrolled. This happens based on the scroll settings defined by the browser or device you’re using
blockThe scroll container only snaps to specific positions along its block-axis
inlineThe scroll container only snaps to specific positions along its inline-axis
bothThe scroll container snaps to specific positions in both of its axes independently (potentially snapping to different elements in each axis)

Tips

  • block-axis : main flow of content on a webpage (vertical direction, usually from top to bottom)
  • inline-axis : secondary flow of content on a webpage (horizontal direction, typically from left to right)

By default, in most languages written from left to right (eg: English), the block-axis is from top to bottom, and the inline-axis is from left to right. However, in certain cases or for languages that are written from right to left (eg: Arabic or Hebrew), the block-axis can be from bottom to top and inline-axis is from right to left.

The direction of block-axis and inline-axis can be modified using CSS properties such as writing-mode, flex-direction, or block-progression

Browser compatibility

ChromeEdge FirefoxSafari Opera Chrome Android WebView AndroidiOS Safari Firefox AndroidOpera Android Samsung Internet
scroll-snap-type6979991156696911684810.0
scroll-snap-align6979681156696911684810.0

source:
https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-align
https://developer.mozilla.org/en-US/docs/Web/CSS/scroll-snap-type#browser_compatibility


Leave a comment

If you found this article helpful and would like to show your support, you can buy me a coffee using the link below. Thanks a bunch!

Choose an amount

S$0.99
S$2.00
S$5.00

Or enter a custom amount

S$

Thank you for your contribution!

Donate

Topics:

Clean CSS (5) CSS (35) css pseudo (4) debug (2) design (7) fonts (2) html (8) image (9) JavaScript (1) layout (5) MiniGame (3) optimization (15) performance (10) responsive (9)


Advertisements

Recent Post:

Advertisements

Related Posts

Advertisements
Advertisements