JDDST Design Mistakes,SEO Strategy,Website Design Why Sticky Website Headers Hide Anchor Links and How to Fix Them

Why Sticky Website Headers Hide Anchor Links and How to Fix Them

You click “Pricing” in a contents list, the page jumps down, and the heading sits hidden under the menu bar. That’s the sticky header anchor link issue, and one line of CSS fixes it by leaving a gap the height of your header.

Visitors think the link is broken, so they scroll back up and lose their place. Here at JDDST, we build and look after websites, and this fix takes minutes on any site with a pinned menu.

You’ll learn why browsers tuck anchors under fixed headers and how to set the offset with a custom property. We also cover what WordPress sites need on top. Read on for the fix.

Why Do Sticky Headers Hide Anchor Links in the First Place?

Developer sitting at a desk in afternoon light

Sticky headers hide anchor links because the browser doesn’t know the header is there. Click a link ending in #pricing and the browser scrolls until that element sits flush against the top of the viewport. The viewport is the visible part of the page.

Fixed headers break that behaviour. A header with fixed or sticky positioning stays pinned while the page scrolls beneath it. So an 80-pixel header hides the first 80 pixels of wherever the link appears. That strip often holds the very heading the reader clicked (usually the one line they came to read).

Keyboard users hit the same bug. When someone tabs to a link and presses Enter, the heading slides under the menu and they can’t see where they landed. It’s on our list of common web design mistakes because it hides content from anyone who can’t easily scroll back with a mouse.

On top of that, skip-to-content links and table-of-contents links miss by the same amount on every jump. If you’ve ever clicked through an FAQ on your phone and nudged the screen up after each one, you’ve seen the problem firsthand.

How CSS Scroll Margin Top Fixes a Fixed Header Scroll Offset

The cleanest fix for a fixed header scroll offset is scroll-margin-top. This CSS property adds invisible space above an element, but only when the browser scrolls to it. Nothing moves on the page, so your layout stays exactly as you designed it.

Work through the four steps below in order, since each one builds on the measurement from the first.

1. Measure Your Header Height Before You Write Any CSS

A guessed value leaves either a sliver of hidden text or an awkward gap above the heading. So open your browser’s developer tools, select the header element and read its height in pixels from the box model panel.

Then check it again at mobile width, where many headers grow or shrink once the menu collapses. Write both numbers down before you touch any code (a sticky note on the monitor works fine).

2. Add scroll-margin-top to Every Anchor Target

With the height in hand, add the property to the elements your links point at. A rule like h2, h3 { scroll-margin-top: 90px; } covers headings. The extra 10 pixels over an 80-pixel header gives the text some breathing room.

That said, IDs often sit on sections or divs rather than headings. If your anchor targets live on wrappers, use the [id] selector so every element with an ID gets the same offset. Click each link once afterwards to confirm the heading lands just below the menu.

3. Store the Offset in a Custom Property So It Follows the Header

Hard-coding 90 pixels works until someone redesigns the header. A safer approach stores the height in a custom property, such as --header-height: 80px on :root. Then, set scroll-margin-top: calc(var(--header-height) 10px) to keep anchor links clear of the header.

That way, one media query can swap the value for smaller screens without you hunting down every rule. In our experience maintaining client sites, a header redesign then needs one edit instead of a search through every stylesheet.

4. Switch to scroll-padding for Older Browsers

If your visitors still use older Safari versions, swap methods instead of stacking them. Current major browsers all support scroll-margin-top, but some older Safari versions only applied it to scroll snapping. For those sites, set scroll-padding-top: var(--header-height) on the html html element and remove scroll-margin-top from your targets.

The MDN reference for scroll-padding-top says the property lets authors exclude areas “obscured by other content (such as fixed-positioned toolbars or sidebars)”. Since the two properties stack when used together, running both doubles the gap above every heading.

Focused developer at a clean desk in bright morning light

Choosing Between Scroll Margin, Scroll Padding and a JavaScript Offset

Each method offsets the jump from a different spot in your code. Your pick depends on whether one header height applies everywhere and who edits the site. The table below compares where each method lives and what can go wrong:

MethodWhere it goesBest forWatch out for
scroll-margin-topEach anchor targetDifferent offsets per elementNeeds a selector covering every target
scroll-padding-topThe html elementOne header height site-wideAlso shifts keyboard page-down scrolling
JavaScript offsetA click handler scriptHeaders that change height while scrollingBreaks if the script fails to load

Where several people add pages, scroll-padding-top on the html element is the simplest start. One declaration covers every new link without anyone touching the CSS. Keep JavaScript for headers that resize mid-scroll, where no fixed number fits.

WordPress and Page Builder Headers Need Their Own Tweaks

WordPress websites add layers that a plain CSS fix doesn’t account for. The admin toolbar, theme scripts and page builders can each change how far a link needs to stop.

Before finishing, check these five areas of your WordPress website:

  • Admin Toolbar: Logged-in users see an extra toolbar strip that grows taller on narrow screens. The offset can look wrong to you while visitors see it correctly. Scope an extra rule to the body .admin-bar class so both views match.

  • Shrinking Headers: If you set up a shrink-on-scroll header in Elementor or Bricks, it will sit shorter mid-page than at the top. Measure that smaller height and use it for your offset.

  • Scroll Scripts: Some WordPress websites use JavaScript to control smooth scrolling. Match the script’s offset to your CSS so back-to-top and skip-to-content links land correctly.

  • Builder Anchors: Anchor widgets and add-ons can place the ID on a separate element instead of the heading. If your CSS misses that element, move the ID onto the heading or widen the selector.

  • Cached Styles: Caching plugins can keep serving your old CSS after you save changes. Clear the cache, then test the page again in a private browser window.

Once the changes are live, test your menu, skip-to-content link and back-to-top button on desktop and mobile. If you manage several WordPress websites, repeat the test on any layout with a custom header. When a heading still hides, compare the scroll script’s offset with your CSS before changing other design settings.

Developer at a desk with multiple printed CSS documentation sheets

Put the Fix Live and Test Every Anchor on Your Site

Hidden headings come from browsers ignoring a header they can’t see, and scroll-margin-top or scroll-padding-top closes that gap. A measured height stored in one custom property keeps the offset accurate after any redesign. Add the rule today, then click every jump link on desktop and mobile in a logged-out browser.

JDDST handles web development and ongoing maintenance, including front-end fixes like this one. Contact our team to sort out stubborn sticky header problems and keep every anchor link landing where it should.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post