CSS scroll-margin-top Property

Description

The scroll-margin-top CSS property is used to control the spacing between the top of an element and the top of its scroll container when it is scrolled into view. This property allows web developers to specify a buffer zone, preventing content from being visually obscured by the top edge of its scroll container. By setting scroll-margin-top, you can ensure that content is comfortably spaced within the viewport, improving the overall user experience by avoiding abrupt or visually jarring transitions when scrolling. This property is particularly useful when working with elements that have fixed or sticky positioning, ensuring they remain visually separated from the container's edge while scrolling.

Initial value
0
Applies to
all elements
Inherited
no
Computed value
as specified
Animatable
by computed value type
JavaScript syntax
object.style.scrollMarginTop

Interactive Demo

1
2
3
Scroll ยป

Syntax

scroll-margin-top: <length>

Values

  • <length>An outset from the bottom edge of the scroll container.

Example

<div id="container">
<div class="blue"></div>
<div class="green"></div>
<div class="blue"></div>
<div class="green"></div>
<div class="blue"></div>
<div class="green"></div>
<div class="blue"></div>
</div>
#container {
  position: relative;
  width: 80%;
  aspect-ratio: 3/1;
  margin: auto;
  border: solid black 2px;
  overflow-x: hidden;
  overflow-y: scroll;
  scroll-snap-type: y mandatory;
}

.blue {
  background-color: lightblue;
  width: 95%;
  aspect-ratio: 3/1;
}

.green {
  background-color: lightgreen;
  width: 80%;
  aspect-ratio: 4/1;
}

.blue, .green {
  margin: 2px;
  scroll-snap-align: start;
  scroll-margin-top: 20px;
}

Browser Support

The following table will show you the current browser support for the CSS scroll-margin-top property.

Desktop
Edge Chrome Firefox Opera Safari
7969685614.1
Tablets / Mobile
Chrome Firefox Opera Safari Samsung Webview
69684814.51069

Last updated by CSSPortal on: 3rd January 2024