CSS | Scroll Snap type

The Scroll Snap type property is an inbuilt property in the Scroll Snap module. Without the Scroll Snap module, the image gallery will look ridiculous. Before the Scroll Snap module, this effect can be achieved by JavaScript but during this days Scroll Snap with the CSS can be achieved.

This property is useful to stop scrolling at some specific point of the page. You can use this property in the gallery section of your web-page. It will give you the full control of the scrolling.

Syntax:



scroll-snap-type: none | [ x | y | block | inline | both ] [ mandatory | proximity ]

Property Values: This property uses many values as mentioned above and described below:

  • none: This property disable the scroll snapping that will ignore the snapping points.
  • x: This property enables the scroll snapping along with the x-axis that works on snap position of horizontal axis.
  • y: This property enables the scroll snapping along with the y-axis that works on snap position of vertical axis.
  • block: This property enables the scroll snapping along with the block-axis that works on snap position of block axis.
  • inline: This property enables the scroll snapping along with the inline-axis that works on snap position of inline axis.
  • both: This property enables the scroll snapping with the both-axis included x, y, block and inline axis.
  • mandatory: This property enables specific strict value that go to the specific scroll position when there is no scrolling.
  • proximity: This property enables specific strict value that go to the specific scroll position. If scrolling action gets pretty close to a snap position if not close then it will act normally and should not snap at all.

Example: Below example will illustrate the Scroll Snap type property:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        CSS Scroll Snap Type
    </title>
    <style>
        h1 {
            color: green;
        }
          
          
        .container {
            width: 500px;
            height: 200px;
            margin-left: auto;
            margin-right: auto;
            border: 2px solid black;
            overflow: scroll;
            position: relative;
        }
          
        .element {
            width: 480px;
            height: 180px;
            scroll-snap-align: start;
            scroll-snap-stop: normal;
            color: white;
            font-size: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
          
        .y-mandatory {
            scroll-snap-type: y mandatory;
        }
          
        .element:nth-child(1) {
            background:
        }
          
        .element:nth-child(2) {
            background: 
        }
          
        .element:nth-child(3) {
            background: 
        }
    </style>
</head>
  
<body>
    <center>
        <h1>GeeksforGeeks</h1>
        <h4>CSS Scroll Snap Type</h4>
        <div class="container y-scroll y-mandatory">
            <div class="wrapper">
                <div class="element"></div>
                <div class="element"></div>
                <div class="element"></div>
            </div>
        </div>
    </center>
</body>
  
</html>                    

chevron_right


Output:

Supported Browsers: The browsers supported by Scroll Snap Type are listed below:

  • Google Chrome 69
  • Internet Explorer 11
  • Firefox 68
  • Safari 11


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.