Open In App

CSS | Scroll Padding

Last Updated : 23 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Scroll Padding property is an inbuilt property in Scroll Snap module. This property sets the scroll padding longhands. This property acts as a magnet on the top of the sliding element that sticks to the top of the viewport and stops the scrolling(forcefully) in that place. The Scroll Padding property is optional, this property is used when the Scroll Snap type property is set to none. 

Syntax:

scroll-padding: [  length percentage | auto ]

Property Values: This property accept two property mentioned above and described below:

  • length-percentage: This property works same as other padding property contains the length in any specific unit for padding.
  • auto: This property leaves some spaces for padding determined by browsers.

Example: Below example illustrates the Scroll Padding property: 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        CSS Scroll Padding
    </title>
    <style>
        h1 {
            color: green;
        }
        .element{
            border:2px solid black;
        }
         
         
        .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;
                        /* scroll-padding: top right bottom left */
            scroll-padding: 50px 0px 0px 20px;
        }
         
        .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 Padding</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 CSS Scroll Padding are listed below:

  • Google Chrome 69 and above
  • Edge 79 and above
  • Firefox 68 and above
  • Opera 56 and above
  • Safari 14.1 and above


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads