Scroll Margin Top: The CSS Solution for Sticky Header Navigation

Stop anchor links from scrolling behind fixed headers. Learn how scroll-margin-top creates seamless, user-friendly navigation experiences.

What is Scroll Margin Top?

Modern websites frequently use sticky headers and fixed navigation bars to improve user experience by keeping important navigation elements always visible. However, this well-intentioned design pattern introduces a frustrating usability problem: when users click anchor links that navigate to page sections, the target content often scrolls behind the sticky header, obscuring section titles and key content.

The scroll-margin-top CSS property provides an elegant, standards-based solution to this common UX challenge. When implemented correctly as part of a comprehensive web development strategy, scroll-margin-top ensures smooth, intuitive navigation that keeps users engaged with your content.

How Scroll Margin Top Works

The scroll-margin-top property defines the top margin of the scroll snap area used for snapping elements to the snapport. This creates an outset that expands the scroll snap area beyond the element's border box.

Key Concepts

  • Scroll Snap Area: Determined by taking the transformed border box of an element
  • Snapport: The visible area of the scroll container where content is viewed
  • Outset: The scroll-margin-top creates space between the element and the viewport edge during scroll settlement

When scrolling settles, the browser aligns the scroll snap area within the snapport, accounting for the margin. This ensures the element ends up positioned with the specified offset from the viewport edge.

scroll-margin-top Syntax
1/* Basic syntax */2scroll-margin-top: <length>;3 4/* Practical examples */5.hero-section {6 scroll-margin-top: 64px;7}8 9.content-section {10 scroll-margin-top: 4rem;11}12 13/* Using CSS custom properties */14:root {15 --header-height: 64px;16 --scroll-offset: calc(var(--header-height) + 20px);17}18 19.anchor-target {20 scroll-margin-top: var(--scroll-offset);21}

Browser Support

scroll-margin-top is part of the CSS Scroll Snap Module Level 1 specification and enjoys excellent browser support across all modern browsers:

  • Chrome/Edge: Version 69+ (September 2018)
  • Firefox: Version 68+ (July 2019)
  • Safari: Version 15+ (September 2021)

This means scroll-margin-top works across virtually all modern browsers with no fallbacks needed. The property is safe to use in production without vendor prefixes.

The Sticky Header Problem

When a page has a fixed or sticky navigation header at the top, clicking an anchor link causes the browser to scroll the target element to the very top of the viewport. This means the sticky header covers the first portion of the target section, potentially hiding:

  • Section titles and headings
  • Navigation micro-copy
  • Key calls to action
  • Initial paragraphs of content

The scroll-margin-top Solution

The scroll-margin-top property adds spacing between the element and the viewport edge when it's the target of anchor navigation. When applied to section elements that serve as anchor targets, the browser accounts for this margin during scroll settlement, positioning the element with the specified offset from the top of the viewport.

This ensures the sticky header no longer obscures the content while maintaining clean, uncluttered CSS architecture.

Practical Implementation

Basic Anchor Link Offset

/* Apply to each section that serves as an anchor target */
section[id] {
 scroll-margin-top: 4rem;
}

This single rule ensures all sections with ID attributes (commonly used as anchor targets) will have 4rem of offset when navigated to via anchor links.

Sticky Header with Anchor Navigation

/* Sticky navigation header */
.site-header {
 position: sticky;
 top: 0;
 height: 64px;
 background: white;
 z-index: 100;
}

/* Anchor sections with offset */
.content-section {
 scroll-margin-top: 80px; /* Header height + extra breathing room */
}

scroll-margin-top vs scroll-padding-top

Both properties solve similar problems but approach them differently:

scroll-padding-top

  • Applied to the scroll container (typically html or the scrolling parent)
  • Affects ALL anchor navigation within that container
  • Global solution for the entire page
  • Best when you want consistent offset for all anchor targets
/* Applied to scroll container - affects all anchor targets */
html {
 scroll-padding-top: 4rem;
}

scroll-margin-top

  • Applied to individual elements (the anchor targets)
  • Allows per-element control of scroll offset
  • Granular solution for specific sections
  • Best when different sections need different offsets

When to Use Each

Use scroll-padding-top when:

  • You have consistent header height across all sections
  • You want a single, maintainable rule for the entire page
  • Your navigation is relatively simple

Use scroll-margin-top when:

  • Different sections require different offsets
  • You have multiple sticky elements (header plus floating CTA)
  • You need fine-grained control over scroll behavior per section
Best Practices for User Experience

Guidelines for implementing scroll-margin-top effectively

Choose Appropriate Offset Values

The offset should be at least equal to the sticky header height. Adding 10-30px of extra space improves visual clarity and helps users distinguish section boundaries.

Responsive Considerations

Different header heights may be needed for mobile vs. desktop layouts. Use media queries to adjust scroll-margin-top breakpoints.

Accessibility Benefits

scroll-margin-top improves accessibility by ensuring content is fully visible when navigated to via anchor links. Screen reader users benefit from consistent behavior.

Performance Efficiency

CSS-only solution with minimal performance impact. No JavaScript required for basic functionality. Works with native browser scrolling.

Real-World Use Cases

Single-Page Application Navigation

Long scrolling pages with section navigation benefit greatly from scroll-margin-top. When users click "Features" in the navigation, they should see the Features section title immediately, not have it hidden behind the header.

Documentation Sites

Technical documentation often uses anchor links within long pages for quick navigation. scroll-margin-top ensures that when a user follows a deep link to a specific heading, that heading is fully visible and readable.

Product Pages

E-commerce product pages frequently include anchor links to Reviews, Specifications, and Related Products. Using scroll-margin-top ensures each section is presented clearly when users navigate between them.

Landing Pages with CTAs

Marketing landing pages often have sticky headers with call-to-action buttons. scroll-margin-top ensures that when users navigate to key conversion sections, those sections are presented without being obscured by the header.

Frequently Asked Questions

Troubleshooting Common Issues

Scroll Margin Not Working

  1. Element has a unique ID that's the target of the anchor link
  2. Element is actually an anchor target (check href attribute matches the ID)
  3. No conflicting CSS on the scroll container
  4. Browser supports the property (should not be an issue in modern browsers)

Scroll Offset is Wrong

  • Measure actual header height including any border or shadow
  • Consider if content has any existing padding or margin
  • Check for other sticky or fixed elements on the page
  • Verify responsive breakpoints are set correctly

Smooth Scroll Behavior Conflicts

If smooth scrolling (scroll-behavior: smooth) is causing issues:

  • Both properties work together; no conflict should occur
  • Test without smooth scroll to isolate the issue
  • Ensure no JavaScript is intercepting scroll events

Advanced Techniques

Dynamic Header Heights

For dynamic header heights (collapsible headers, responsive nav):

const header = document.querySelector('.site-header');
const headerHeight = header.offsetHeight;

document.documentElement.style.setProperty(
 '--scroll-offset',
 `${headerHeight + 20}px`
);

Multiple Nested Sticky Elements

When page has both a sticky header and a sticky sidebar:

.content-section {
 scroll-margin-top: calc(var(--header-height) + var(--sidebar-height) + 20px);
}

Need Help Implementing Scroll Margin Top or Other CSS Solutions?

Our UI/UX team specializes in creating seamless, user-friendly navigation experiences that convert visitors into customers.