CSS scroll-margin-top Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Physical Properties

What You’ll Learn

The scroll-margin-top property offsets a scroll target at the top edge. It is useful when you want space between the top of the viewport or scroll container and the element being scrolled into view.

01

top

Physical top edge.

02

offset

Scroll stop space.

03

length

px, rem, %.

04

target

On the element.

05

header

Fixed nav fix.

06

snap

Vertical panels.

Introduction

The scroll-margin-top property in CSS is used to set the top margin of an element when it is scrolled into view.

This property is particularly useful when working with CSS scroll snapping and fixed headers, as it allows you to adjust the scroll position to avoid overlapping content at the top of the viewport.

Definition and Usage

Apply scroll-margin-top to the element being scrolled to — not to the scroll container itself. It works with anchor links, scrollIntoView(), focus navigation, and scroll snapping.

It is one longhand of the scroll-margin shorthand. In normal horizontal writing modes it often behaves like scroll-margin-block-start, but it always targets the physical top edge.

💡
Beginner Tip

scroll-margin-top: 20px tells the browser to leave 20px of space above the target when it becomes the scroll destination — helpful under a fixed header.

📝 Syntax

The syntax for the scroll-margin-top property is straightforward. You can specify a length value using units such as px, em, rem, %, and more.

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

Here, value can be any valid CSS length value.

Basic Example

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

Related Properties

  • scroll-margin-bottom — offset at the bottom edge
  • scroll-margin — shorthand for all four sides
  • scroll-margin-block-start — logical block-start offset

🎯 Default Value

The default value of the scroll-margin-top property is 0. This means there is no additional margin applied when the element is scrolled into view by default.

⚡ Quick Reference

QuestionAnswer
Default value0
Common useFixed headers and anchor links
Part ofscroll-margin shorthand
Accepted valuesLengths and percentages
InheritedNo
AnimatableNo

💎 Property Values

ValueExampleDescription
lengthscroll-margin-top: 20px;Specifies the margin as a length value in units like px, em, rem, or %. For example, 20px, 2em, 10%.
percentagescroll-margin-top: 10%;A percentage of the containing block’s width, such as 10%.
20px 2rem 10%

When to Use scroll-margin-top

scroll-margin-top helps when the top side of a target needs breathing room:

  • Fixed headers — Keep section titles visible below a fixed navigation bar.
  • Anchor links — Improve in-page jumps so headings are not hidden under sticky UI.
  • Vertical snap panels — Add space above snapped sections in a scroll container.
  • scrollIntoView() — Offset programmatic scroll targets the same way as hash links.

For vertical writing modes or international layouts, consider scroll-margin-block-start instead of hard-coded top rules.

👀 Live Preview

Scroll inside the demo. Each panel uses scroll-snap-align: start and scroll-margin-top: 1.25rem.

Panel 1
Panel 2
Panel 3

Top scroll margin creates breathing room above each snapped panel.

Examples Gallery

Start with the reference fixed-header example, fix sticky nav anchor jumps, try vertical snap spacing, and match header height with rem units.

📜 Top Offset

Add space at the top edge when an element is scrolled into view — matching the reference example.

Example 1 — scroll-margin-top with a fixed header

In this example, we’ll set a top scroll margin of 20px for section elements. This is useful when you want to avoid overlapping content, like a fixed header.

index.html
<style>
  body {
    height: 200vh;
    scroll-snap-type: y mandatory;
  }

  section {
    height: 100vh;
    scroll-snap-align: start;
    scroll-margin-top: 20px;
  }

  header {
    position: fixed;
    top: 0;
    width: 100%;
    background: #334155;
    color: #fff;
    padding: 10px;
    text-align: center;
  }
</style>

<header>Fixed Header</header>
<section style="background:lightcoral">Section 1</section>
<section style="background:lightblue">Section 2</section>
Try It Yourself

How It Works

The browser expands the scroll snap area above the section before settling the scroll position.

Example 2 — Sticky navigation anchor links

Add scroll-margin-top to sections so headings stay visible below a sticky nav when users click in-page links.

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

How It Works

Match the offset to the sticky header height so linked headings are not covered.

📄 Vertical Scroll Layouts

Improve full-page and in-container vertical scroll snapping.

Example 3 — Vertical snap panel spacing

Use scroll-margin-top on snapped panels inside a vertical scroll container.

snap-panel.css
.panel {
  scroll-snap-align: start;
  scroll-margin-top: 24px;
}
Try It Yourself

How It Works

Pair with scroll-snap-type: y mandatory for polished vertical panel navigation.

Example 4 — rem units matching header height

Use scroll-margin-top: 3.5rem; when the fixed header height is defined in rem.

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

How It Works

Using rem keeps the scroll offset proportional when users change root font size.

scroll-margin-top in the family

scroll-margin-top is the top longhand of scroll-margin. For the opposite side, see scroll-margin-bottom.

shorthand-equivalent.css
/* These are equivalent when only top matters */
.a { scroll-margin-top: 2rem; }
.b { scroll-margin: 2rem 0 0 0; }

🧠 How scroll-margin-top Works

1

Scroll target is selected

A link, script, or snap rule focuses on an element with scroll-margin-top.

Target
2

Top snap area grows

The browser adds the specified offset beyond the element’s top edge.

Offset
3

Scroll position settles

The element lands with the extra top-side space visible in the scrollport.

Position
=

Comfortable top stops

Section titles and snap targets no longer hide under fixed or sticky headers.

Browser Compatibility

The scroll-margin-top 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.

Physical scroll margins · Modern support

Reliable top-side support

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

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 links and snap panels on mobile browsers to confirm the top offset clears your header.

scroll-margin-top property 97% supported

Bottom line: scroll-margin-top is safe to use in modern projects for scroll targets that need top-side offset.

Conclusion

The scroll-margin-top property is a valuable tool for web developers aiming to create smooth and user-friendly scrolling experiences.

By adjusting the top margin of elements during scroll snapping and anchor navigation, you can prevent overlapping content and ensure that important elements remain visible. Experiment with different values to see how this property can improve the usability of your web projects.

💡 Best Practices

✅ Do

  • Apply scroll-margin-top on section and heading scroll targets
  • Match the offset to fixed or sticky header height
  • Use rem units when the header size is defined in rem
  • Test anchor links and scrollIntoView() behavior
  • Combine with scroll-behavior: smooth for page jumps

❌ Don’t

  • Set it on the scroll container expecting container padding
  • Confuse it with margin-top layout spacing
  • Assume top offset is always correct in vertical writing modes
  • Use huge percentages without testing on small screens
  • Forget scroll-margin-bottom when the bottom edge also needs space

Key Takeaways

Knowledge Unlocked

Five things to remember about scroll-margin-top

Use these points when offsetting top scroll targets.

5
Core concepts
02

top

Physical edge.

Axis
03

longhand

One side.

Detail
04

header

Fixed nav.

Use case
🔄 05

scroll-margin

Four-side shorthand.

Companion

❓ Frequently Asked Questions

scroll-margin-top adds scroll offset at the top edge of an element when it is scrolled into view. It helps prevent targets from sitting flush against the top of the viewport or scroll container.
When users follow anchor links, the browser scrolls the target to the top of the viewport. scroll-margin-top adds extra space so section titles are not hidden under a fixed or sticky header.
scroll-margin-top is a physical property that always targets the top edge. scroll-margin-block-start is logical and follows the block axis, which may map differently in vertical writing modes.
The default value is 0, meaning no extra top offset is applied.
Use it for anchor links, scroll snapping, sticky navigation bars, fixed headers, and any scroll target that should stop below the top edge.

Practice in the Live Editor

Open the HTML editor and try scroll-margin-top: 20px on a section scroll target.

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