CSS scroll-margin-top Property
The CSS scroll-margin-top property sets the scroll margin at the physical top edge of an element.
It adjusts the area used to align an element when scrolling it into view without changing its normal layout spacing. This property is particularly useful for anchor navigation, sticky headers, scroll snapping, and JavaScript scrolling.
scroll-margin-top Syntax
The scroll-margin-top property accepts one CSS length value or a CSS-wide keyword.
Basic Syntax
selector {
scroll-margin-top: 20px;
}
Available Length Values
.target {
scroll-margin-top: 0;
}
.target {
scroll-margin-top: 20px;
}
.target {
scroll-margin-top: 2rem;
}
.target {
scroll-margin-top: 1.5em;
}
.target {
scroll-margin-top: -10px;
}
.target {
scroll-margin-top: calc(3rem + 10px);
}
CSS-Wide Keywords
.target {
scroll-margin-top: initial;
}
.target {
scroll-margin-top: inherit;
}
.target {
scroll-margin-top: unset;
}
.target {
scroll-margin-top: revert;
}
.target {
scroll-margin-top: revert-layer;
}
Positive values expand the scroll alignment area above the element. Negative values contract it. Percentages and auto are invalid.
Property Values
| Value | Description |
|---|---|
<length> |
Sets the top scroll margin using a CSS length such as px, rem, or em. |
0 |
Applies no additional top scroll margin. This is the initial value. |
| Positive length | Expands the scroll alignment area above the element. |
| Negative length | Contracts the scroll alignment area at the top edge. |
calc() |
Calculates a valid length from a CSS mathematical expression. |
initial |
Resets the property to 0. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to 0 because it is 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 |
| Inherited | No |
| Applies to | All elements |
| Computed value | Absolute length |
| Animation type | By computed value, as a length |
| Percentages | Not accepted |
| Physical edge | Top |
Valid Values
.target {
scroll-margin-top: 80px;
}
.target {
scroll-margin-top: 4rem;
}
.target {
scroll-margin-top: 0;
}
.target {
scroll-margin-top: -5px;
}
.target {
scroll-margin-top: calc(3rem + 12px);
}
Invalid Values
.target {
scroll-margin-top: auto;
}
.target {
scroll-margin-top: 10%;
}
.target {
scroll-margin-top: 10px 20px;
}
.target {
scroll-margin-top: smooth;
}
.target {
scroll-margin-top: 2s;
}
This longhand property accepts a single length, not percentages, multiple values, or scrolling behavior keywords.
How It Works
The scroll-margin-top property expands or contracts the scroll snap area surrounding an element at its physical top edge.
When the browser aligns a target during scrolling, it can use this adjusted area rather than aligning the element's border box directly with the edge of the scrollport.
Default Top Scroll Margin
.target {
scroll-margin-top: 0;
}
No additional top scroll margin is applied.
Positive Top Scroll Margin
.target {
scroll-margin-top: 80px;
}
The target's scroll alignment area extends 80 pixels above its top edge. When aligned at the top of a scroll container, this can leave room above the element.
Negative Top Scroll Margin
.target {
scroll-margin-top: -10px;
}
The alignment area contracts by 10 pixels at the top edge. This can cause the target to align closer to, or beyond, the top of the visible scrollport.
Using Relative Lengths
.target {
scroll-margin-top: 5rem;
}
Relative units allow the scroll offset to scale with the root font size, which can be useful when interface dimensions use rem units.
Scroll Margin Does Not Affect Layout
Unlike margin-top, the scroll-margin-top property does not move the element, change its dimensions, or add spacing between neighboring elements.
Available Scroll Range
The browser cannot always achieve the requested offset. For example, a target near the beginning or end of a scroll container may be limited by the available scroll range.
Anchor Navigation and Sticky Headers
One of the most common uses of scroll-margin-top is preventing anchor-linked headings from appearing underneath a sticky or fixed header.
The Problem With Sticky Headers
When a visitor clicks a link such as href="#contact", the browser may scroll the target heading near the top of the viewport. If a header covers that area, the heading can become partially or completely hidden.
Adding a Top Scroll Margin
section[id] {
scroll-margin-top: 90px;
}
Each targeted section requests 90 pixels of space above its top edge when scrolled into alignment.
HTML Anchor Navigation
<nav aria-label="Page sections">
<a href="#overview">Overview</a>
<a href="#features">Features</a>
<a href="#contact">Contact</a>
</nav>
<section id="overview">
<h2>Overview</h2>
</section>
<section id="features">
<h2>Features</h2>
</section>
<section id="contact">
<h2>Contact</h2>
</section>
Applying the Offset to Headings
h2[id],
h3[id] {
scroll-margin-top: 6rem;
}
This approach is useful when headings themselves contain the fragment identifiers used by navigation links.
Using a Custom Property
:root {
--header-height: 80px;
}
section[id] {
scroll-margin-top: calc(var(--header-height) + 16px);
}
The calculated value accounts for the header height and adds 16 pixels of extra space.
Important Consideration
Apply the property to the element identified by the anchor link. If the fragment identifier belongs to a heading, setting the property only on its parent section may not produce the intended result.
Using scroll-margin-top With Scroll Snap
The scroll-margin-top property can adjust how items align inside a vertically scrolling container that uses CSS Scroll Snap.
Vertical Scroll Snap Example
.scroll-container {
height: 300px;
overflow-y: auto;
scroll-snap-type: y mandatory;
}
.scroll-container .item {
min-height: 200px;
scroll-snap-align: start;
scroll-margin-top: 30px;
}
Each item's scroll snap area extends 30 pixels above its border box. When the browser aligns an item at the top, this can provide additional space above it.
Different Offsets for Individual Items
.scroll-container .item {
scroll-margin-top: 20px;
}
.scroll-container .item.featured {
scroll-margin-top: 60px;
}
The featured item receives a larger top scroll margin than the other items.
Combining With Scroll Padding
.scroll-container {
scroll-padding-top: 20px;
}
.scroll-container .item {
scroll-margin-top: 30px;
}
Scroll padding establishes an inset within the container, while scroll margin adjusts the alignment area around each item.
Important Consideration
The property does not enable scroll snapping. The scroll container needs scroll-snap-type, and the targets need scroll-snap-align.
Programmatic Scrolling
JavaScript scrolling methods can use scroll-margin-top when bringing a target into view.
Using scrollIntoView()
.target {
scroll-margin-top: 80px;
}
document.querySelector(".target").scrollIntoView({
behavior: "smooth",
block: "start"
});
The target requests approximately 80 pixels of space above its top edge, assuming sufficient scroll range and no additional alignment offsets.
Using Nearest Alignment
document.querySelector(".target").scrollIntoView({
behavior: "smooth",
block: "nearest"
});
Nearest alignment attempts to minimize scrolling. The target may not be positioned at the top of the scrollport, so the visible effect of the top scroll margin can differ.
Respecting Reduced Motion
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
document.querySelector(".target").scrollIntoView({
behavior: reduceMotion ? "instant" : "smooth",
block: "start"
});
Consider reduced-motion preferences when using animated scrolling.
Important Consideration
The scroll-margin-top property does not control scroll animation. It only adjusts the target's scroll alignment area.
Using the scroll-margin Shorthand
The scroll-margin shorthand sets scroll margins for all four physical edges.
One-Value Shorthand
.target {
scroll-margin: 20px;
}
All four scroll margins receive 20 pixels.
Two-Value Shorthand
.target {
scroll-margin: 10px 30px;
}
Top and bottom receive 10 pixels, while right and left receive 30 pixels.
Three-Value Shorthand
.target {
scroll-margin: 10px 20px 30px;
}
Top receives 10 pixels, right and left receive 20 pixels, and bottom receives 30 pixels.
Four-Value Shorthand
.target {
scroll-margin: 10px 20px 30px 40px;
}
The values apply clockwise: top, right, bottom, and left.
Equivalent Longhand Declarations
.target {
scroll-margin-top: 10px;
scroll-margin-right: 20px;
scroll-margin-bottom: 30px;
scroll-margin-left: 40px;
}
Overriding the Top Margin
.target {
scroll-margin: 20px;
scroll-margin-top: 80px;
}
The top scroll margin becomes 80 pixels, while the other three edges remain at 20 pixels.
Declaration Order
.target {
scroll-margin-top: 80px;
scroll-margin: 20px;
}
The later shorthand resets the top scroll margin to 20 pixels along with the other three edges.
Physical Versus Logical Scroll Margins
The scroll-margin-top property always refers to the physical top edge. Logical scroll margins adapt to writing mode and text direction.
| Property | Edge |
|---|---|
scroll-margin-top |
Physical top edge |
scroll-margin-bottom |
Physical bottom edge |
scroll-margin-block-start |
Logical block-start edge |
scroll-margin-block-end |
Logical block-end edge |
scroll-margin-inline-start |
Logical inline-start edge |
Horizontal Writing Mode
.target {
writing-mode: horizontal-tb;
scroll-margin-block-start: 80px;
}
In horizontal writing, block-start corresponds to the physical top edge.
Vertical Writing Mode
.target {
writing-mode: vertical-rl;
direction: ltr;
scroll-margin-inline-start: 80px;
}
In this vertical writing mode, inline-start corresponds to the physical top edge.
Physical Top Scroll Margin
.target {
scroll-margin-top: 80px;
}
The top edge remains the same regardless of writing mode or direction.
Choosing Between Physical and Logical Properties
Use scroll-margin-top when the offset must remain at the physical top. Use logical scroll margins when the offset should follow the writing mode or text direction.
Overlapping Declarations
Physical and logical scroll margin properties can affect the same edge. The CSS cascade determines which declaration applies.
scroll-margin-top Versus scroll-padding-top
Both properties influence vertical scroll alignment, but they are applied to different elements.
| Property | Applied To | Purpose |
|---|---|---|
scroll-margin-top |
Scroll target | Expands or contracts the target's scroll alignment area at its top edge. |
scroll-padding-top |
Scroll container | Defines an inset at the top of the container's optimal viewing region. |
Target-Side Offset
.section-heading {
scroll-margin-top: 80px;
}
Use this approach when specific headings or sections need additional alignment space.
Container-Side Offset
.scroll-container {
scroll-padding-top: 80px;
}
Use this approach when the scroll container needs a consistent top alignment inset.
Combining Both Properties
.scroll-container {
scroll-padding-top: 20px;
}
.scroll-container .target {
scroll-margin-top: 30px;
}
For top alignment, these values can provide a combined 50-pixel offset between the target's top border edge and the container's top scrollport edge.
When to Use Each Property
Use scroll-padding-top for a consistent inset across the scroll container. Use scroll-margin-top when individual targets need their own offsets.
scroll-margin-top Example
This interactive example compares two vertically scrolling panels. The second panel applies a 50-pixel top scroll margin to its target. Click each button to compare the alignment.
HTML Example
<div class="scroll-top-demo">
<div class="scroll-example">
<h3>Without Scroll Margin</h3>
<div class="scroll-panel">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item target">Target</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
<div class="item">Item 6</div>
</div>
<button type="button">Scroll to Target</button>
</div>
<div class="scroll-example with-margin">
<h3>With Top Scroll Margin</h3>
<div class="scroll-panel">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item target">Target</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
<div class="item">Item 6</div>
</div>
<button type="button">Scroll to Target</button>
</div>
</div>
CSS Example
.scroll-top-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
gap: 20px;
}
.scroll-top-demo .scroll-panel {
height: 180px;
overflow-y: auto;
padding: 10px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.scroll-top-demo .item {
padding: 22px 10px;
margin-bottom: 10px;
text-align: center;
background-color: #e2e8f0;
}
.scroll-top-demo .target {
background-color: #bfdbfe;
border: 2px solid #2563eb;
}
.scroll-top-demo .with-margin .target {
scroll-margin-top: 50px;
}
.scroll-top-demo button {
margin-top: 10px;
padding: 8px 12px;
cursor: pointer;
}
JavaScript Example
document.querySelectorAll(".scroll-top-demo .scroll-example").forEach((example) => {
const button = example.querySelector("button");
const target = example.querySelector(".target");
button.addEventListener("click", () => {
target.scrollIntoView({
behavior: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "instant" : "smooth",
block: "start",
inline: "nearest"
});
});
});
Without Scroll Margin
With Top Scroll Margin
Click each button to bring the target toward the top of its panel. The second panel requests an additional 50 pixels of space above the target. The available scroll range determines whether the full offset can be applied.
Increasing the Top Offset
.scroll-top-demo .with-margin .target {
scroll-margin-top: 80px;
}
The target requests a larger top alignment offset.
Removing the Top Offset
.scroll-top-demo .with-margin .target {
scroll-margin-top: 0;
}
The additional top scroll margin is removed.
Browser Compatibility Note
The final scroll position can vary depending on the scrolling operation and available scroll range. The target may not reach its requested alignment when the scroll container is near its boundaries.
Accessibility Considerations
Top scroll margins can improve navigation by preventing headings and other targets from being hidden beneath sticky or fixed headers.
Keep Anchor Targets Visible
Apply an appropriate offset to headings or sections targeted by fragment links so their content remains readable after navigation.
Maintain Keyboard Accessibility
Links and buttons that trigger scrolling should remain keyboard accessible and display visible focus indicators.
Consider Focus Management
Calling scrollIntoView() does not automatically move keyboard focus to the target. Scripted navigation may require additional focus handling.
Respect Reduced Motion Preferences
The property does not animate scrolling. When smooth scrolling is used, respect prefers-reduced-motion.
Account for Responsive Headers
Sticky header heights may change across screen sizes. Consider responsive values or CSS custom properties when defining scroll offsets.
Test Zoom and Text Scaling
Verify that target headings remain visible when users increase browser zoom or text size.
Common Mistakes
Using Percentages
.target {
scroll-margin-top: 10%;
}
Percentages are invalid. Use a CSS length.
Using auto
.target {
scroll-margin-top: auto;
}
The property does not accept auto.
Providing Multiple Values
.target {
scroll-margin-top: 10px 20px;
}
This longhand property accepts only one length.
Confusing Scroll Margin With Normal Margin
scroll-margin-top adjusts scroll alignment, while margin-top changes normal layout spacing.
Applying the Property to the Wrong Element
Apply scroll-margin-top to the scroll target. For anchor navigation, this is generally the element whose id matches the fragment link.
Applying It Only to the Scroll Container
Use scroll-margin-top on target elements. Use scroll-padding-top on the scroll container for a container-side inset.
Expecting Automatic Scroll Snapping
The property does not enable scroll snapping by itself.
Ignoring Available Scroll Range
Targets near the beginning or end of a scroll container may not reach their requested top alignment.
Ignoring Declaration Order
A later scroll-margin shorthand can override an earlier scroll-margin-top declaration.
Confusing Scroll Margin With Smooth Scrolling
Scroll margin adjusts alignment. The scroll-behavior property and scrolling API options control scrolling animation.
Important Considerations
Keep the following points in mind when using scroll-margin-top:
- It is a physical scroll margin longhand property.
- Its initial value is
0, and it is not inherited. - It accepts one CSS length, including negative and calculated lengths.
- Percentages and
autoare invalid. - It always affects the physical top edge, regardless of writing mode or direction.
- It adjusts scroll alignment without changing normal layout spacing.
- It is useful for anchor navigation and sticky header offsets.
- It can influence scroll snapping and programmatic scrolling.
- It does not create a scroll container, enable scroll snapping, or control smooth scrolling.
- The
scroll-marginshorthand controls all four physical scroll margins. - The
scroll-margin-block-startproperty follows the logical block-start edge. - The
scroll-padding-topproperty provides container-side alignment spacing. - Physical and logical scroll margin declarations can affect the same edge.
- Available scroll range may limit the final alignment position.
- Responsive sticky headers may require different offsets at different screen sizes.
- Test anchor navigation, keyboard access, zoom, and reduced-motion behavior.
Browser Support
The CSS scroll-margin-top property is Baseline Widely available and is supported in current versions of major browsers.
Older browsers may have limited support, and some older browser versions have had inconsistencies when applying scroll margins to fragment navigation.
For current compatibility information, visit Can I Use? and search for scroll-margin-top.
When unsupported, scrolling generally remains functional, but the additional top alignment offset may not be applied.
Specifications
The scroll-margin-top property is defined in CSS Scroll Snap Module Level 1.
CSS Scroll Snap Module Level 1
The specification defines how scroll margins expand or contract the scroll snap area surrounding a target.
For additional documentation and examples, consult MDN Web Docs.
