CSS - scroll-margin-top



CSS scroll-margin-top property specifies the top margin of the scroll snap area of an element.

Possible Values

  • <length> − A length value is a numeric value, such as px, em.

Applies to

All the HTML elements.

DOM Syntax

object.style.scrollMarginTop = "length";

CSS Scroll Margin Top - Zero Value

The following example demonstrates how to use scroll-margin-top: 0 to remove the top margin around the scrolling sections −

<html>
<head>
<style>
   .scroll-container {
      width: 300px;
      height: 200px;
      overflow-x: hidden;
      overflow-y: scroll;
      scroll-snap-type: y mandatory;
   }
   .scrolling-section1,
   .scrolling-section2,
   .scrolling-section3 {
      width: 300px;
      height: 200px;
      scroll-snap-align: start;
   }
   .scrolling-section1 {
      background-color: rgb(220, 235, 153);
      scroll-margin-top: 0;
   }
   .scrolling-section2 {
      background-color: rgb(230, 173, 218);
      scroll-margin-top: 0;
   }
   .scrolling-section3 {
      background-color: rgb(119, 224, 210);
      scroll-margin-top: 0;
   }
</style>
</head>
<body>
   <h3>Scroll the content using the scrollbar arrows to see the effect.</h3>
   <div class="scroll-container">
      <div class="scrolling-section1">scroll-margin-top: 0</div>
      <div class="scrolling-section2">scroll-margin-top: 0</div>
      <div class="scrolling-section3">scroll-margin-top: 0</div>
   </div>
</body>
</html>

CSS Scroll Margin Top - Length Value

The following example demonstrates how to use the scroll-margin-top property to add a top margin to each section, which affects the scrolling behavior of the content −

<html>
<head>
<style>
   .scroll-container {
      width: 300px;
      height: 200px;
      overflow-x: hidden;
      overflow-y: scroll;
      scroll-snap-type: y mandatory;
   }
   .scrolling-section1,
   .scrolling-section2,
   .scrolling-section3 {
      width: 300px;
      height: 200px;
      scroll-snap-align: start;
   }
   .scrolling-section1 {
      background-color: rgb(220, 235, 153);
      scroll-margin-top: 20px;
   }
   .scrolling-section2 {
      background-color: rgb(230, 173, 218);
      scroll-margin-top: 2em;
   }
   .scrolling-section3 {
      background-color: rgb(119, 224, 210);
      scroll-margin-top: 10px;
   }
</style>
</head>
<body>
   <h3>Scroll the content using the scrollbar arrows to see the effect.</h3>
   <div class="scroll-container">
      <div class="scrolling-section1">scroll-margin-top: 20px</div>
      <div class="scrolling-section2">scroll-margin-top: 2em</div>
      <div class="scrolling-section3">scroll-margin-top: 10px</div>
   </div>
</body>
</html>
Advertisements