The CSS scroll-margin
property is a shorthand that defines the outset margin of an element which is used as the scroll snap area when scrolling. This allows you to create a visual separation between the edge of the scroll container and the snap position of the element, preventing content from being obscured by elements like fixed headers.
Example 1: scroll-margin-top
/* Target the element that will be snapped to */
.snap-target {
scroll-margin-top: 50px; /* Creates a 50px top margin for scrolling */
scroll-snap-align: start;
}
/* A container to enable scroll snapping */
.scroll-container {
height: 100vh;
overflow-y: scroll;
scroll-snap-type: y mandatory;
}
/* Example of a fixed header that might obscure the content */
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 50px;
background-color: white;
border-bottom: 1px solid #ccc;
}
Explanation
This code adds a 50px margin to the top of the .snap-target
element's scroll snap area. When you scroll and snap to this element, it will stop 50px below the top of the scroll container, preventing the .fixed-header
from covering it.
Example 2: scroll-margin shorthand (all sides)
/* Apply a scroll margin to all four sides of the element */
.snap-element {
scroll-margin: 2rem; /* Sets a 2rem scroll margin on top, right, bottom, and left */
scroll-snap-align: center;
}
/* A scroll container for context */
.scroll-container {
scroll-snap-type: both mandatory;
overflow: scroll;
width: 100vw;
height: 100vh;
}
Explanation
The scroll-margin
shorthand with a single value applies the specified outset to all four sides. Here, a 2rem
gap is created around the .snap-element
for scroll snapping purposes on both the vertical and horizontal axes.
Example 3: scroll-margin shorthand (vertical & horizontal)
/* Apply different scroll margins for vertical and horizontal axes */
.card {
scroll-margin: 10px 20px; /* 10px for top/bottom, 20px for left/right */
scroll-snap-align: start;
}
/* A horizontal scroll container */
.horizontal-scroller {
display: flex;
overflow-x: scroll;
scroll-snap-type: x mandatory;
}
Explanation
When using two values, the first value sets scroll-margin-top
and scroll-margin-bottom
, while the second sets scroll-margin-left
and scroll-margin-right
. This is useful for creating different spacing for vertical and horizontal scrolling.
Example 4: scroll-margin-left
/* Target an element within a horizontal scroll container */
.list-item {
scroll-margin-left: 15px; /* Adds a 15px left margin for scroll snapping */
scroll-snap-align: start;
}
/* A container with horizontal scrolling */
.horizontal-list {
display: flex;
overflow-x: scroll;
scroll-snap-type: x mandatory;
}
Explanation
The scroll-margin-left
property specifically defines the outset on the left side of the element. This is ideal for horizontal scrolling interfaces where you need to ensure snapped items don't sit flush against the container's starting edge.
Example 5: Using viewport units (vh)
/* Use viewport height units for a responsive scroll margin */
.section {
scroll-margin-top: 10vh; /* Sets the top scroll margin to 10% of the viewport height */
scroll-snap-align: start;
}
.scrolling-wrapper {
height: 100vh;
overflow-y: scroll;
scroll-snap-type: y mandatory;
}
Explanation
Using relative units like vh
(viewport height) for scroll-margin
allows the space to be responsive to the user's screen size. This ensures the offset is proportional, which is great for accommodating navigation bars or headers of varying heights.
Example 6: scroll-margin with four values
/* Apply a different scroll margin to each side of the element */
.grid-item {
/* top: 10px, right: 20px, bottom: 30px, left: 40px */
scroll-margin: 10px 20px 30px 40px;
scroll-snap-align: center;
}
.grid-container {
display: grid;
overflow: scroll;
scroll-snap-type: both mandatory;
}
Explanation
The four-value shorthand follows the standard CSS box model order: top, right, bottom, and left. This provides granular control over the scroll snap area on each individual side of the element.
Example 7: scroll-margin-bottom
/* Define a scroll margin at the bottom of a snap target */
.footer-snap {
scroll-margin-bottom: 25px; /* Ensures a 25px space below when snapping */
scroll-snap-align: end;
}
.full-page-scroller {
height: 100vh;
overflow-y: scroll;
scroll-snap-type: y mandatory;
}
Explanation
scroll-margin-bottom
creates an outset at the bottom of the element. When combined with scroll-snap-align: end
, this ensures that when you snap to the end of the element, there is still space visible below it, preventing it from touching the edge of the viewport.