CSS scroll-margin Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Scrolling

What You’ll Learn

The scroll-margin property adds breathing room around elements when they become scroll targets. It is especially helpful for anchor links and scrollIntoView() when fixed headers would otherwise hide the destination.

01

Offset

Scroll stop buffer.

02

top

Clear sticky nav.

03

length

px, rem, em.

04

anchors

#section links.

05

JS scroll

scrollIntoView.

06

padding

Companion prop.

Introduction

The scroll-margin property in CSS allows developers to define the margin around an element when it is scrolled into view.

This property is particularly useful for ensuring that elements are not flush against the edge of the viewport when using functions like scrollIntoView(). By adjusting the scroll margin, you can create a more visually pleasing and user-friendly scrolling experience.

Definition and Usage

Apply scroll-margin to the element that will be scrolled to — such as a section with an id, a heading anchor, or a card targeted by JavaScript.

Unlike regular margin, scroll-margin does not change layout spacing between siblings. It only affects the scroll snap area and where the browser positions the element after navigation.

💡
Beginner Tip

If a sticky header covers section titles after clicking nav links, add scroll-margin-top equal to the header height.

📝 Syntax

The scroll-margin property can be applied to any element, and it accepts length values for specifying the margin around the element when it is scrolled into view.

syntax.css
element {
  scroll-margin: value;
}

Basic Example

scroll-margin-top.css
.target {
  scroll-margin-top: 50px;
}

Longhand Properties

  • scroll-margin-top, scroll-margin-right, scroll-margin-bottom, scroll-margin-left
  • scroll-margin-block, scroll-margin-inline
  • scroll-margin-block-start, scroll-margin-block-end, scroll-margin-inline-start, scroll-margin-inline-end

Syntax Rules

  • One to four values work like regular margin shorthand.
  • Length units such as px, rem, and em are common.
  • Percentages are relative to the element’s own dimensions.
  • Set the offset on the target element, not on the scroll container.

🎯 Default Value

The default value of the scroll-margin property is 0, meaning no additional margin is applied.

⚡ Quick Reference

QuestionAnswer
Default value0
Sticky header fixscroll-margin-top: 4rem;
Accepted valuesLengths and percentages
Set onScroll target elements
InheritedNo
AnimatableNo

💎 Property Values

ValueExampleDescription
lengthscroll-margin-top: 50px;Specifies the offset around the element in units such as px, em, rem, etc.
percentagescroll-margin-top: 10%;Specifies the offset as a percentage of the element’s size.
scroll-margin-top 50px 4rem 24px 12px

When to Use scroll-margin

scroll-margin solves common scroll-position problems:

  • Sticky navigation — Keep section headings visible below a fixed or sticky header.
  • Anchor link pages — Improve table-of-contents jumps on docs and landing pages.
  • JavaScript scrolling — Offset targets when calling element.scrollIntoView().
  • Scroll snapping — Fine-tune where snapped items land inside a scroll container.

If you need to inset the scrollable viewport itself, use scroll-padding on the container instead.

👀 Live Preview

Each section below uses scroll-margin-top: 2.5rem so titles stay below the sticky nav when you click a link.

Section A

Scroll target with top offset.

Section B

Heading stays clear of the nav bar.

Section C

Try each link to compare the stop position.

Without scroll-margin-top, the sticky nav would cover the section titles.

Examples Gallery

Start with the reference scroll-margin-top example, fix sticky-header overlap, try shorthand values, and use rem units with a fixed banner.

📜 Scroll Targets

Offset where the browser stops when scrolling to an element — matching the reference example.

Example 1 — scroll-margin-top: 50px

In this example, we’ll add a scroll margin to a section element to ensure it is not flush against the top of the viewport when scrolled into view.

index.html
<style>
  section {
    height: 100vh;
    border: 1px solid black;
  }

  .target {
    scroll-margin-top: 50px;
  }
</style>

<button onclick="document.querySelector('.target').scrollIntoView({ behavior: 'smooth' })">
  Scroll to Section 3
</button>
<section class="target">Section 3</section>
Try It Yourself

How It Works

The browser treats the scroll target as if it has extra space above it when calculating the final scroll position.

Example 2 — Sticky header offset

Match scroll-margin-top to your sticky navigation height so linked headings remain readable.

sticky-nav.css
section[id] {
  scroll-margin-top: 4rem;
}
Try It Yourself

How It Works

Measure the sticky header height and use the same value (or slightly more) for scroll-margin-top.

🗃 Shorthand & Units

Use multiple sides and relative units when building responsive documentation layouts.

Example 3 — scroll-margin shorthand

Set vertical and horizontal offsets with two values: scroll-margin: 24px 12px;.

shorthand.css
#panel-b {
  scroll-margin: 24px 12px;
}
Try It Yourself

How It Works

Shorthand follows the same value order as regular margin.

Example 4 — rem units with a fixed banner

Use rem so the scroll offset scales with root font size: scroll-margin-top: 3.5rem;.

rem-offset.css
#chapter-2 {
  scroll-margin-top: 3.5rem;
}
Try It Yourself

How It Works

rem keeps the offset proportional when users change default text size settings.

scroll-margin with scroll-behavior

Smooth scrolling from scroll-behavior feels better when targets land in a comfortable position. Use both together on documentation pages with sticky navigation.

Remember: scroll-margin goes on the target element; scroll-padding goes on the scroll container when you need viewport insets.

smooth-plus-offset.css
html {
  scroll-behavior: smooth;
}

section {
  scroll-margin-top: 4rem;
}

🧠 How scroll-margin Works

1

A scroll target is chosen

An anchor link, focus move, or scrollIntoView() call selects an element.

Target
2

Scroll snap area expands

The browser includes the element’s scroll-margin when computing the ideal stop position.

Offset
3

Viewport scrolls into place

The element lands with the requested margin instead of touching the viewport edge.

Position
=

Readable destinations

Headings and targets stay visible, even with fixed UI chrome.

Browser Compatibility

The scroll-margin property is supported in most modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, it is always a good practice to test your website across different browsers to ensure compatibility.

Scrolling · Modern support

Reliable scroll-margin support

Current Chrome, Firefox, Safari, Edge, and Opera support scroll-margin on scroll targets.

97% Modern browser support
Google Chrome 69+ · Desktop & Mobile
Full support
Mozilla Firefox 68+ · Desktop & Mobile
Full support
Apple Safari 14.1+ · macOS & iOS
Full support
Microsoft Edge 79+ · Chromium
Full support
Opera 56+ · Modern versions
Full support

Testing tip

Test anchor navigation on mobile Safari and Chrome with your real sticky header height — a few pixels of extra scroll-margin-top often helps.

scroll-margin property 97% supported

Bottom line: scroll-margin is widely available in modern browsers for anchor links and programmatic scrolling.

Conclusion

The scroll-margin property is a useful tool for enhancing the user experience when dealing with scrollable content.

By defining margins around elements that are scrolled into view, you can create a more comfortable and visually appealing navigation experience. Experiment with different values and see how this property can improve the usability of your web projects.

💡 Best Practices

✅ Do

  • Set scroll-margin-top on section targets with sticky headers
  • Match the offset to the real header or banner height
  • Prefer rem for scalable documentation layouts
  • Pair with scroll-behavior: smooth for polished in-page nav
  • Test anchor links on mobile and desktop

❌ Don’t

  • Confuse scroll-margin with regular layout margin
  • Put scroll-margin on the scroll container instead of the target
  • Forget offsets inside nested overflow scroll areas
  • Use huge percentage values without checking small screens
  • Assume scroll-padding and scroll-margin do the same thing

Key Takeaways

Knowledge Unlocked

Five things to remember about scroll-margin

Use these points when fixing scroll target positioning.

5
Core concepts
02

top

Header fix.

Pattern
03

target

Not container.

Scope
04

length

px and rem.

Values
🔄 05

behavior

Smooth pair.

Companion

❓ Frequently Asked Questions

scroll-margin adds an invisible offset around an element when it is scrolled into view. It helps prevent targets from sitting flush against the viewport edge or under fixed headers.
margin affects layout spacing between elements. scroll-margin only changes where the browser stops when scrolling to that element as a target.
scroll-margin is set on the target element being scrolled to. scroll-padding is set on the scroll container to inset the scrollable area.
The default value is 0, meaning no extra offset is applied when the element is scrolled into view.
Use scroll-margin-top on section headings or anchor targets when a sticky or fixed header would otherwise cover the content after navigation.

Practice in the Live Editor

Open the HTML editor and try scroll-margin-top: 4rem with sticky navigation.

HTML Editor →

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.

5 people found this page helpful