Open In App

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:

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






<!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>                   

Output:

  

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


Article Tags :