Open In App

CSS scroll-margin-top property

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

The scroll-margin-top property is used to set all the scroll margins to the top of an element at once. The value specified for the scroll-margin-top determines how much of the page that is primarily outside the support should remain visible.

Hence, the scroll-margin-top values represent outsets that define the scroll snap area that is used for snapping this box to the support.

Syntax:

scroll-margin-top: length
/* Or */
scroll-margin-top: Global_Values

Property values: This property accepts two-properties mentioned above and described below:

  • length: This property refers to the values defined with length units: px, em, rem, vh, etc.
  • Global_Values: This property refers to the global values like initial, inherit, unset, etc.

Note: scroll-margin-top doesn’t accept percentage value as the length.

Example: In this example, you can see the effect of scroll-margin-top by scrolling to a point partway between two of the “interfaces” of the example’s content.

HTML




<!DOCTYPE html>
<html>
<head>
    <style>
        .page {
            width: 278px;
            height: 296px;
            color: white;
            font-size: 50px;
            display: flex;
            box-sizing: border-box;
            align-items: center;
            justify-content: center;
            scroll-snap-align: end none;
        }
 
        .Container {
            width: 300px;
            height: 300px;
            overflow-x: hidden;
            overflow-y: auto;
            white-space: nowrap;
            scroll-snap-type: y mandatory;
        }
    </style>
</head>
<body>
    <div class="Container">
        <div class="page"
            style="background-color: rgb(20, 240, 38);
                scroll-margin-top: 0px;">
            Geeks
        </div>
        <div class="page"
            style="background-color: green;
                scroll-margin-top: 20px;">
            for
        </div>
        <div class="page" style="color: black;
            scroll-margin-top: 40px;">
            Geeks
        </div>
        <div class="page"
            style="background-color: rgb(10, 207, 43);
            scroll-margin-top: 30px;">
            for
        </div>
    </div>
</body>
</html>


Output:

Supported Browsers:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads