CSS scroll-margin Property
The CSS scroll-margin property defines extra spacing around an element when it is used as a scroll target.
It is a shorthand property for setting the scroll margins on all four sides of an element. Scroll margins affect scroll snapping and target alignment during operations such as anchor navigation and scrollIntoView(), without changing the element's actual layout margins.
scroll-margin Syntax
The scroll-margin property accepts one to four length values, following the same ordering pattern as the CSS margin shorthand.
Basic Syntax
selector {
scroll-margin: 20px;
}
Available Values
.target {
scroll-margin: 20px;
}
.target {
scroll-margin: 10px 20px;
}
.target {
scroll-margin: 10px 20px 30px;
}
.target {
scroll-margin: 10px 20px 30px 40px;
}
.target {
scroll-margin: 1rem;
}
.target {
scroll-margin: -10px;
}
.target {
scroll-margin: calc(2rem + 10px);
}
CSS-Wide Keywords
.target {
scroll-margin: initial;
}
.target {
scroll-margin: inherit;
}
.target {
scroll-margin: unset;
}
.target {
scroll-margin: revert;
}
.target {
scroll-margin: revert-layer;
}
The values represent physical top, right, bottom, and left scroll margins. Unlike ordinary margin, scroll-margin does not accept auto or percentage values.
Property Values
| Value | Description |
|---|---|
<length> |
Specifies the scroll margin using an absolute or relative CSS length, such as 20px, 1rem, or 2em. |
| One length | Applies the same scroll margin to all four sides. |
| Two lengths | The first sets top and bottom; the second sets right and left. |
| Three lengths | Sets top, right and left, and bottom, respectively. |
| Four lengths | Sets top, right, bottom, and left, respectively. |
| Negative lengths | Reduce the scroll target's expanded alignment area along the specified sides. |
calc() |
Calculates a length using a valid CSS math expression. |
initial |
Resets all four scroll margins to 0. |
inherit |
Uses the computed scroll margin values from the parent element. |
unset |
Resets all four scroll margins to 0 because the properties are not inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 for each longhand property |
| Inherited | No |
| Applies to | All elements |
| Computed value | Computed length values for the four longhand properties |
| Animation type | By computed value, as lengths |
| Percentages | Not accepted |
Valid Values
.target {
scroll-margin: 24px;
}
.target {
scroll-margin: 1rem 2rem;
}
.target {
scroll-margin: 10px 15px 20px 25px;
}
.target {
scroll-margin: -5px;
}
.target {
scroll-margin: calc(1rem + 8px);
}
Invalid Values
.target {
scroll-margin: auto;
}
.target {
scroll-margin: 10%;
}
.target {
scroll-margin: 10px, 20px;
}
.target {
scroll-margin: 1s;
}
.target {
scroll-margin: 10px 20px 30px 40px 50px;
}
Scroll margins require length values. Percentages, auto, comma-separated values, and more than four values are invalid.
How It Works
When a browser determines where to position an element during scrolling, it can use an alignment area surrounding the element rather than only the element's border box.
The scroll-margin property expands or contracts that alignment area by the specified distances.
Default Scroll Margin
.target {
scroll-margin: 0;
}
The scroll target has no additional scroll margin around its transformed border box.
Adding Scroll Margin
.target {
scroll-margin: 30px;
}
The target's scroll alignment area extends 30 pixels beyond each side of its transformed border box.
Adding Only Top Spacing
.target {
scroll-margin-top: 80px;
}
When the target is aligned near the top of a scrolling container, the additional top scroll margin can leave room above the element.
Scroll Target Alignment
The effect of scroll margin depends on the scrolling operation and the alignment requested. It is especially useful with scroll snapping, anchor navigation, and scrollIntoView().
Layout Remains Unchanged
Unlike regular margins, scroll margins do not add space between neighboring elements in normal document flow.
Important Distinction
The property changes the area used to calculate scrolling alignment. It does not directly move an element, create scrolling overflow, or determine how quickly scrolling occurs.
One to Four Values
The scroll-margin shorthand follows the familiar clockwise ordering used by CSS margin and padding properties.
One Value
.target {
scroll-margin: 20px;
}
All four sides receive a scroll margin of 20 pixels.
Two Values
.target {
scroll-margin: 10px 20px;
}
The top and bottom receive 10 pixels, while the right and left receive 20 pixels.
Three Values
.target {
scroll-margin: 10px 20px 30px;
}
The top receives 10 pixels, the right and left receive 20 pixels, and the bottom receives 30 pixels.
Four Values
.target {
scroll-margin: 10px 20px 30px 40px;
}
The values apply to the top, right, bottom, and left sides in that order.
Shorthand Value Reference
| Number of Values | Assignment |
|---|---|
| 1 | All four sides |
| 2 | Top/bottom, right/left |
| 3 | Top, right/left, bottom |
| 4 | Top, right, bottom, left |
Important Consideration
The shorthand sets all four physical scroll margins. Any omitted side receives a value according to the shorthand rules rather than retaining a previously declared value.
Longhand Properties
The scroll-margin shorthand sets four physical longhand properties.
| Property | Description |
|---|---|
scroll-margin-top |
Sets the scroll margin on the top side. |
scroll-margin-right |
Sets the scroll margin on the right side. |
scroll-margin-bottom |
Sets the scroll margin on the bottom side. |
scroll-margin-left |
Sets the scroll margin on the left side. |
Shorthand Declaration
.target {
scroll-margin: 10px 20px 30px 40px;
}
Equivalent Longhand Declarations
.target {
scroll-margin-top: 10px;
scroll-margin-right: 20px;
scroll-margin-bottom: 30px;
scroll-margin-left: 40px;
}
Overriding One Side
.target {
scroll-margin: 20px;
scroll-margin-top: 80px;
}
The top scroll margin becomes 80 pixels while the other three sides remain at 20 pixels.
Declaration Order
.target {
scroll-margin-top: 80px;
scroll-margin: 20px;
}
In this example, the later shorthand resets all four sides to 20 pixels, including the top.
Important Consideration
Use the shorthand when several sides share related values. Use a longhand property when only one side needs adjustment.
Anchor Navigation and Sticky Headers
A common use of scroll-margin is preventing headings from being hidden behind a fixed or sticky header when navigating to anchor links.
HTML Example
<nav aria-label="Page sections">
<a href="#overview">Overview</a>
<a href="#features">Features</a>
<a href="#examples">Examples</a>
</nav>
<section id="overview">
<h2>Overview</h2>
</section>
<section id="features">
<h2>Features</h2>
</section>
<section id="examples">
<h2>Examples</h2>
</section>
CSS Example
section[id] {
scroll-margin-top: 90px;
}
When a section is scrolled into view near the top of the viewport, the 90-pixel scroll margin can keep it below the sticky header.
Using the Shorthand
section[id] {
scroll-margin: 90px 0 0;
}
This applies 90 pixels to the top scroll margin and zero to the other sides.
Combining With Smooth Scrolling
html {
scroll-behavior: smooth;
}
section[id] {
scroll-margin-top: 90px;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}
The browser requests smooth scrolling while the target retains additional space above it. Users who prefer reduced motion receive normal scrolling.
Important Consideration
Choose a scroll margin that accommodates the actual sticky header height. If the header changes size at different viewport widths, the offset may need to change as well.
Scroll Snapping
The scroll-margin property is part of CSS Scroll Snap. It expands or contracts the area used to align a snap target with a scrolling container's snapport.
Basic Scroll Snap Example
.gallery {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.gallery .slide {
flex: 0 0 250px;
scroll-snap-align: start;
scroll-margin: 20px;
}
Each slide has an expanded scroll snap area extending 20 pixels beyond its border box.
Horizontal Scroll Margin
.gallery .slide {
scroll-margin: 0 20px;
}
The top and bottom margins are zero, while the right and left scroll margins are 20 pixels.
Using scroll-snap-align
.gallery {
scroll-snap-type: x mandatory;
}
.gallery .slide {
scroll-snap-align: center;
scroll-margin: 15px;
}
The browser aligns the expanded snap area according to the specified snap alignment.
Combining With scroll-padding
.gallery {
scroll-snap-type: x mandatory;
scroll-padding-inline: 20px;
}
.gallery .slide {
scroll-snap-align: start;
scroll-margin-inline: 10px;
}
Scroll padding adjusts the container's optimal viewing region, while scroll margin adjusts the alignment area around each slide.
Important Consideration
Scroll margin does not enable snapping by itself. The container needs an appropriate scroll-snap-type declaration, and its targets need snap alignment settings.
scroll-margin Versus margin
Although the two properties use similar shorthand syntax, they serve different purposes.
| Property | Purpose |
|---|---|
margin |
Creates space outside an element's border box and affects normal layout. |
scroll-margin |
Adjusts the element's scroll alignment area without adding layout spacing. |
Regular Margin
.target {
margin-top: 40px;
}
The element receives 40 pixels of top margin in the document layout.
Scroll Margin
.target {
scroll-margin-top: 40px;
}
The element's scroll alignment area extends 40 pixels above its border box, without adding layout space.
Using Both Properties
.target {
margin-top: 20px;
scroll-margin-top: 80px;
}
The regular margin affects layout spacing, while the scroll margin affects scrolling alignment.
Important Distinction
Use regular margins to control spacing between elements. Use scroll margins to control how targets are positioned during scrolling.
scroll-margin Versus scroll-padding
The scroll-margin and scroll-padding properties work together but apply to different parts of the scrolling relationship.
| Property | Applied To | Purpose |
|---|---|---|
scroll-margin |
Scroll target | Expands or contracts the target's scroll alignment area. |
scroll-padding |
Scroll container | Defines offsets within the container's optimal viewing region. |
Using scroll-margin
.target {
scroll-margin-top: 80px;
}
The target requests additional space above itself during scroll alignment.
Using scroll-padding
.scroll-container {
scroll-padding-top: 80px;
}
The container defines an 80-pixel inset from its top edge for scroll alignment.
Combining Both Properties
.scroll-container {
scroll-padding-top: 40px;
}
.scroll-container .target {
scroll-margin-top: 20px;
}
Both offsets can contribute to the final alignment position.
When to Use Each Property
Use scroll-padding when the container needs a consistent inset for its targets. Use scroll-margin when individual targets need additional spacing.
Important Consideration
Using both properties can produce a larger offset than intended. Check the resulting alignment before combining them.
Logical Scroll Margins
CSS also provides logical scroll margin properties that follow the writing mode and text direction rather than fixed physical sides.
| Property | Description |
|---|---|
scroll-margin-block |
Shorthand for the block-start and block-end scroll margins. |
scroll-margin-block-start |
Sets the scroll margin on the block-start side. |
scroll-margin-block-end |
Sets the scroll margin on the block-end side. |
scroll-margin-inline |
Shorthand for the inline-start and inline-end scroll margins. |
scroll-margin-inline-start |
Sets the scroll margin on the inline-start side. |
scroll-margin-inline-end |
Sets the scroll margin on the inline-end side. |
Block-Axis Scroll Margins
.target {
scroll-margin-block: 20px 40px;
}
The first value sets the block-start margin, while the second sets the block-end margin.
Inline-Axis Scroll Margins
.target {
scroll-margin-inline: 10px 30px;
}
The values apply to the inline-start and inline-end sides according to the writing mode and direction.
Combining Logical Properties
.target {
scroll-margin-block: 30px;
scroll-margin-inline: 15px;
}
The target receives 30 pixels on both block-axis sides and 15 pixels on both inline-axis sides.
Important Consideration
Logical scroll margin properties are useful for multilingual layouts and writing modes where the block and inline directions may not correspond to the physical top, right, bottom, and left sides.
scroll-margin Example
The following example demonstrates how scroll-margin-top creates space above a target when navigating within a scrollable container.
HTML Example
<div class="scroll-margin-demo">
<nav aria-label="Scroll margin example">
<a href="#scroll-demo-one">Section One</a>
<a href="#scroll-demo-two">Section Two</a>
<a href="#scroll-demo-three">Section Three</a>
</nav>
<div class="scroll-panel">
<section id="scroll-demo-one">
<h3>Section One</h3>
<p>The first scroll target.</p>
</section>
<section id="scroll-demo-two">
<h3>Section Two</h3>
<p>The second scroll target.</p>
</section>
<section id="scroll-demo-three">
<h3>Section Three</h3>
<p>The third scroll target.</p>
</section>
</div>
</div>
CSS Example
.scroll-margin-demo {
max-width: 600px;
}
.scroll-margin-demo nav {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 15px;
}
.scroll-margin-demo nav a {
padding: 8px 12px;
background-color: #2563eb;
color: #ffffff;
text-decoration: none;
}
.scroll-margin-demo .scroll-panel {
height: 220px;
overflow-y: auto;
scroll-behavior: smooth;
border: 1px solid #cbd5e1;
}
.scroll-margin-demo .scroll-panel section {
min-height: 220px;
padding: 20px;
box-sizing: border-box;
scroll-margin-top: 40px;
}
.scroll-margin-demo .scroll-panel section:nth-child(odd) {
background-color: #eff6ff;
}
.scroll-margin-demo .scroll-panel section:nth-child(even) {
background-color: #dbeafe;
}
@media (prefers-reduced-motion: reduce) {
.scroll-margin-demo .scroll-panel {
scroll-behavior: auto;
}
}
Section One
The first scroll target.
Use the links above to navigate between sections.
Section Two
The second scroll target.
Notice the additional space above this section.
Section Three
The third scroll target.
Scroll margin changes alignment without adding layout spacing.
Click Section Two or Section Three. The browser requests smooth scrolling and uses the 40-pixel top scroll margin when positioning the target within the scroll panel.
Removing the Scroll Margin
.scroll-margin-demo .scroll-panel section {
scroll-margin-top: 0;
}
The additional scroll alignment offset is removed.
Increasing the Scroll Margin
.scroll-margin-demo .scroll-panel section {
scroll-margin-top: 80px;
}
The target requests 80 pixels of additional space above itself during scrolling alignment.
Using the Shorthand
.scroll-margin-demo .scroll-panel section {
scroll-margin: 40px 0 0;
}
This produces the same top scroll margin while explicitly setting the other sides to zero.
Browser Compatibility Note
Scroll alignment can vary according to the scrolling operation, available scroll range, and browser implementation. A target near the end of a container may not be able to reach the requested position if insufficient scrollable space remains.
Accessibility Considerations
Scroll margins can improve navigation by keeping headings and other targets visible when users follow links or move between interface sections.
Keep Anchor Targets Visible
Use scroll margins to prevent important headings from being hidden behind sticky navigation bars.
Preserve Semantic Navigation
Use standard anchor links and meaningful target IDs so navigation works without JavaScript.
Maintain Keyboard Accessibility
Ensure links remain keyboard accessible and that focus indicators are visible after navigation.
Consider Focus Management
Changing scroll alignment does not automatically solve focus management requirements in scripted interfaces.
Avoid Excessive Scroll Offsets
Very large scroll margins can leave excessive empty space or make the destination appear disconnected from the user's intended target.
Respect Reduced Motion Preferences
Scroll margin does not animate scrolling, but it is often combined with scroll-behavior: smooth. Disable unnecessary smooth scrolling when users request reduced motion.
Support Different Viewport Sizes
Check that offsets remain appropriate when headers change height on mobile devices or when users zoom the page.
Important Consideration
The scroll-margin property affects visual scroll alignment, not the document's semantic structure or reading order.
Common Mistakes
Using Percentage Values
.target {
scroll-margin: 10%;
}
Percentages are not valid for scroll-margin. Use length values instead.
Using auto
.target {
scroll-margin: auto;
}
Unlike the regular margin property, scroll-margin does not accept auto.
Using Commas Between Values
.target {
scroll-margin: 10px, 20px;
}
Separate shorthand values with spaces, not commas.
Expecting Layout Spacing
Scroll margin does not create visible space between neighboring elements in normal layout.
Applying It to the Scroll Container Instead of the Target
For target-specific offsets, apply scroll-margin to the element being scrolled into view. Use scroll-padding on the container for container-wide offsets.
Expecting scroll-margin to Enable Scroll Snapping
Scroll snapping requires appropriate snap properties. Scroll margin only adjusts the target alignment area.
Confusing Scroll Margin With Smooth Scrolling
Scroll margin controls alignment offsets. The scroll-behavior property controls whether supported scrolling operations use smooth movement.
Using an Excessive Offset
A large scroll margin can create an unexpected target position, particularly in short scroll containers.
Ignoring Writing Modes
The physical shorthand uses top, right, bottom, and left. Consider logical scroll margin properties when supporting vertical writing or multiple text directions.
Important Considerations
Keep the following points in mind when using scroll-margin:
- It is a shorthand property for four physical scroll margin longhands.
- The initial value of each longhand is
0. - The property is not inherited.
- It applies to all elements.
- It accepts one to four length values.
- One value applies to all four sides.
- Two values set top/bottom and right/left.
- Three values set top, right/left, and bottom.
- Four values set top, right, bottom, and left.
- Negative length values are permitted.
- Percentage values are not accepted.
- The
autokeyword is invalid. - Scroll margins adjust the target's scroll alignment area.
- They do not create spacing in normal document layout.
- They do not directly move the element.
- They can affect scroll snapping and target alignment.
- They are useful for anchor navigation with sticky headers.
- The property does not create a scroll container.
- The property does not enable scroll snapping by itself.
- The
scroll-snap-alignproperty determines target snap alignment. - The
scroll-snap-typeproperty configures snapping behavior on the container. - The
scroll-paddingproperty defines container-side alignment offsets. - The
scroll-behaviorproperty controls smooth or normal scrolling. - Logical scroll margin properties follow writing modes and text directions.
- Later shorthand declarations can override earlier longhand declarations.
- The available scroll range can limit the final target position.
- Offsets should accommodate responsive header sizes.
- Scroll margins do not replace appropriate focus management.
- Test scroll alignment across browsers, viewport sizes, and input methods.
Browser Support
The CSS scroll-margin property is Baseline Widely available and is supported in current versions of major browsers.
Scroll margin behavior can vary according to the scrolling operation, browser implementation, and available scroll range. Some older browsers may not apply scroll margins consistently to anchor navigation or programmatic scrolling.
For current compatibility information, visit Can I Use? and search for scroll-margin.
When scroll margin is unsupported, anchor navigation and scrolling generally remain functional, but the additional alignment offset may not be applied.
Specifications
The scroll-margin property is defined in CSS Scroll Snap Module Level 1.
CSS Scroll Snap Module Level 1
The specification defines the scroll margin shorthand, its longhand properties, and how scroll margins affect the scroll snap area around a target.
For additional documentation and examples, consult MDN Web Docs.
