CSS | Scroll Snap type - GeeksforGeeks

30 Sep, 2019
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:
<!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:url("https://www.geeksforgeeks.org/wp-content/uploads/html-768x256.png");        }                  .element:nth-child(2) {            background: url("https://www.geeksforgeeks.org/wp-content/uploads/CSS-768x256.png");        }                  .element:nth-child(3) {            background: url("https://www.geeksforgeeks.org/wp-content/uploads/javascript-768x256.png");        }    </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:
Google Chrome 69
Internet Explorer 11
Firefox 68
Safari 11
CSS-Properties
CSS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Design a web page using HTML and CSS
Form validation using jQuery
How to set space between the flexbox ?
Search Bar using HTML, CSS and JavaScript
How to Create Time-Table schedule using HTML ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript