CSS scroll-margin Property

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The scroll-margin shorthand sets outsets around an element’s scroll snap area. When that element is snapped to or scrolled into view, the browser leaves that much space from the edge of the scrollport.

It is a shorthand for scroll-margin-top, scroll-margin-right, scroll-margin-bottom, and scroll-margin-left. Set it on the target, not the scroll container. It does not change layout like margin. The longhands start at 0. It is not inherited.

Baseline · Widely available.

Not deprecated. Older drafts used the name scroll-snap-margin; prefer scroll-margin in new CSS.

CSS Syntax

syntax.css
scroll-margin: <length>{1,4};

Same 1–4 value pattern as margin. Example: scroll-margin: 4rem 0 0; Percentages are invalid.

Property Values

ValueDescriptionTry it
One lengthSame outset on all four sidesTry it
Two lengthsBlock (top/bottom), then inline (left/right)Try it
Four lengthsTop, right, bottom, leftTry it
0 / initialNo extra offset (default)Try it
inheritTakes the parent’s scroll-marginTry it

Common beginner pattern: scroll-margin-top: 4rem; (or scroll-margin: 4rem 0 0;) so a sticky header does not cover the target.

How to Use scroll-margin

Put the offset on the element you scroll to, sized to clear any fixed or sticky chrome.

  1. Identify the scroll target (heading, section, snap item).
  2. Measure how much sticky/fixed UI covers the top (or other edge).
  3. Set scroll-margin (or a longhand like scroll-margin-top) on that target.
  4. Optional: use scroll-padding on the container instead when every target needs the same inset.
PieceRequired?Role
Scroll target elementYesWhere you set scroll-margin
Length value(s)YesHow far to inset from the scrollport edge
Scroll containerUsuallyViewport, or a box with overflow that scrolls

Live Preview

A sticky bar covers the top of the scroll box. Change scroll-margin-top on the targets, then click a link.

With 0, section titles hide under the sticky bar. Larger values leave them visible.

Scroll box
Section A
Section B
Section C

scroll-margin-top: 48px

CSS Examples

Each snippet focuses on scroll-margin. Extra styles stay minimal for study. Use Try It Yourself for a live editor.

Example 1 — Clear a Sticky Header

Offset the top of each section so the title is not covered.

CSS
<style>
  .box {
    height: 160px;
    overflow: auto;
  }
  header {
    position: sticky;
    top: 0;
    height: 40px;
  }
  section {
    height: 160px;
    scroll-margin-top: 40px;
  }
</style>

<p><a href="#b">Go to B</a></p>
<div class="box">
  <header>Menu</header>
  <section id="a">A</section>
  <section id="b">B</section>
</div>
Try It Yourself

How It Works

  1. Sticky covers the top. The header stays visible while the box scrolls.
  2. Margin on the target. scroll-margin-top: 40px matches the header height.
  3. Scroll stops lower. Section B lands below the sticky bar, not under it.
  4. Layout unchanged. Normal margin between sections does not change.

Example 2 — One Length for All Sides

A single value sets the same outset on every side.

CSS
<style>
  .box {
    height: 160px;
    overflow: auto;
  }
  section {
    height: 160px;
    scroll-margin: 24px;
  }
</style>

<p><a href="#b">Go to B</a></p>
<div class="box">
  <section id="a">A</section>
  <section id="b">B</section>
</div>
Try It Yourself

How It Works

  1. One value. scroll-margin: 24px applies 24px on top, right, bottom, and left.
  2. Breathing room. The target stops 24px away from the scrollport edges.
  3. Still on the target. The rule lives on section, not on .box.
  4. Shorthand. Same idea as writing all four longhands.

Example 3 — Two Values

First value is top/bottom; second is left/right.

CSS
<style>
  .box {
    height: 160px;
    overflow: auto;
  }
  section {
    height: 160px;
    scroll-margin: 40px 8px;
  }
</style>

<p><a href="#b">Go to B</a></p>
<div class="box">
  <section id="a">A</section>
  <section id="b">B</section>
</div>
Try It Yourself

How It Works

  1. Two values. Like margin: first = top and bottom, second = left and right.
  2. Larger vertical offset. 40px clears more space on the block axis.
  3. Smaller sides. 8px is a light inset on the left and right.
  4. Four values would be top, right, bottom, left in that order.

Edge Cases & Pitfalls

If the target still hides under a header, one of these is usually why.

target

Set it on the target, not the container

Put scroll-margin on the element you scroll to. For a container-wide inset, use scroll-padding instead.

layout

Not the same as margin

It does not push siblings apart. Neighbors keep their normal layout positions.

%

Percentages are invalid

Use lengths such as px, rem, or em. A percentage value is dropped.

end

Near the end of the scroll range

If there is not enough room to honor the offset, the browser stops as close as it can.

alias

Old name: scroll-snap-margin

Prefer scroll-margin. Some engines still recognize the older name as an alias.

Browser Support

scroll-margin is a browser CSS property for scroll targets. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

scroll-margin

Chrome 69+, Edge 79+, Firefox 90+, Safari 14.1+ / iOS 14.5+. Not supported in Internet Explorer.

97% Global support
Google Chrome 69+
Full support
Mozilla Firefox 90+
Full support
Apple Safari 14.1+
Full support
Microsoft Edge 79+
Full support
Opera 56+
Full support
Samsung Internet 10.1+
Full support
Internet Explorer No support
No support
scroll-margin Widely supported

Bottom line: Safe for modern sites. Prefer scroll-margin over the older scroll-snap-margin name. This is not a JavaScript runtime API.

Key Takeaways

  • Role: offset where a scroll target stops relative to the scrollport.
  • On the target. Use lengths only; percentages are invalid.
  • vs scroll-padding: margin on the item; padding on the container.
  • Sticky headers: match scroll-margin-top to the header height.

One line: scroll-margin keeps scroll targets out from under sticky UI — without changing layout.

Frequently Asked Questions

It adds an invisible outset around an element’s scroll snap area. When that element is scrolled into view or snapped to, the browser stops with that extra offset — useful under sticky headers.
margin changes layout spacing between boxes. scroll-margin does not move neighbors; it only changes where scrolling stops when the element is a scroll target.
scroll-margin is set on the target being scrolled to. scroll-padding is set on the scroll container to inset the scrollport.
Each longhand starts at 0, so the shorthand’s initial effect is no extra offset.
No. scroll-margin accepts length values only. Percentages are invalid.
It is Baseline Widely available: Chrome 69+, Edge 79+, Firefox 90+, Safari 14.1+. It is a browser CSS property, not a Node.js feature. Internet Explorer does not support it.

Next: scroll-margin-block

Set block-start and block-end scroll margins with logical properties.

scroll-margin-block tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful