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
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 | |||||
79 | 69 | 68 | 56 | 14.1 |
Tablets / Mobile | |||||
69 | 68 | 48 | 14.5 | 10 | 69 |
Last updated by CSSPortal on: 3rd January 2024