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.
Fundamentals
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.
Foundation
📝 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.
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.
Cheat Sheet
⚡ Quick Reference
Question
Answer
Default value
0
Common use
Fixed headers and anchor links
Part of
scroll-margin shorthand
Accepted values
Lengths and percentages
Inherited
No
Animatable
No
Reference
💎 Property Values
Value
Example
Description
length
scroll-margin-top: 20px;
Specifies the margin as a length value in units like px, em, rem, or %. For example, 20px, 2em, 10%.
percentage
scroll-margin-top: 10%;
A percentage of the containing block’s width, such as 10%.
20px2rem10%
Context
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.
Preview
👀 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.
Hands-On
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.
/* These are equivalent when only top matters */.a{scroll-margin-top:2rem;}.b{scroll-margin:2rem000;}
🧠 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.
Compatibility
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 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 links and snap panels on mobile browsers to confirm the top offset clears your header.
scroll-margin-top property97% supported
Bottom line:scroll-margin-top is safe to use in modern projects for scroll targets that need top-side offset.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about scroll-margin-top
Use these points when offsetting top scroll targets.
5
Core concepts
★01
0 Default
No offset.
Default
⚙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.