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.
Fundamentals
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.
Foundation
📝 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.
Chapter 2 clears the fixed banner when opened from the table of contents.
How It Works
rem keeps the offset proportional when users change default text size settings.
Companion
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.
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.
Compatibility
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 Chrome69+ · Desktop & Mobile
Full support
Mozilla Firefox68+ · Desktop & Mobile
Full support
Apple Safari14.1+ · macOS & iOS
Full support
Microsoft Edge79+ · Chromium
Full support
Opera56+ · 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 property97% supported
Bottom line:scroll-margin is widely available in modern browsers for anchor links and programmatic scrolling.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about scroll-margin
Use these points when fixing scroll target positioning.
5
Core concepts
★01
0 Default
No offset.
Default
⚙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.