UICheck

LayoutUpdated 12 October 20263 min read

Fixed header covering content: how to fix it

A header with position: fixed floats above the page, so whatever sits under it is hidden: the first heading, and the top of every section you jump to with a link. On phones, where headers take a bigger share of the screen, it is even more noticeable.

What it looks like

The same page on an iPhone 17 (402px wide), with the problem and then the fix.

Before
Before: A position: fixed header sits on top of the page heading, so only its last word shows.
A position: fixed header sits on top of the page heading, so only its last word shows.
After
After: A position: sticky header keeps its own space, so the heading starts below it.
A position: sticky header keeps its own space, so the heading starts below it.

Prefer position: sticky

A sticky header stays in the page flow, so it takes up its own space and still sticks to the top while scrolling. Content never starts underneath it.

header { position: sticky; top: 0; z-index: 50; }
Watch out

A sticky element needs a top value, such as top: 0. Without it, it never sticks.

If it must be fixed, add matching space

Give the page top padding equal to the header height. A CSS variable keeps the two in sync when the header gets taller on phones.

:root { --header-h: 64px; }@media (max-width: 767px) { :root { --header-h: 56px; } }header { position: fixed; inset: 0 0 auto; height: var(--header-h); }body { padding-top: var(--header-h); }

Watch for overflow: hidden

position: sticky silently stops working when an ancestor has overflow: hidden (or auto). If your sticky header or sidebar scrolls away, look for that on a parent, and use overflow: clip or overflow-x: clip instead.

Headers that change height

When the header grows on small screens (a wrapped menu, a promo bar, a cookie notice) a fixed padding value is wrong again. Measure the real height with a small script and keep it in the CSS variable, so padding and scroll-margin always match.

const header = document.querySelector('header');new ResizeObserver(([entry]) => {  document.documentElement.style.setProperty('--header-h', entry.borderBoxSize[0].blockSize + 'px');}).observe(header);

Smooth scrolling and the whole page offset

If every in-page link should stop below the header, set the offset once on the scrolling element instead of on each target. scroll-padding-top works for anchor links, the browser’s find in page and keyboard focus moving down the page.

html { scroll-padding-top: calc(var(--header-h) + 16px); scroll-behavior: smooth; }

Keyboard focus hidden behind the header

When someone tabs through a page, the focused link can scroll under a sticky header and disappear. That fails WCAG 2.2 Focus Not Obscured (2.4.11). The scroll-padding-top rule above also fixes this, because browsers use it when they scroll a focused element into view.

Phones held sideways and small screens

A 64px header is a tenth of a phone held upright but a quarter of one held sideways. Keep the header short on small screens, or hide it while scrolling down and show it again when the visitor scrolls up.

@media (max-height: 500px) { header { position: static; } } /* landscape phones: let it scroll away */

z-index and things that cover the header

A sticky or fixed header needs a z-index higher than the page content, or images, sliders and maps slide over it. Give it a clear value such as 50, and remember that a parent with transform, filter or a lower z-index creates its own layer that the header can’t rise above.

Checklist

Questions

Why is my sticky header not sticking?

Usually an ancestor has overflow: hidden, overflow: auto or a fixed height. Sticky works relative to the nearest scrolling ancestor.

Fixed or sticky: which is better?

Sticky, in most cases: it keeps its space in the layout, so nothing slides underneath it.

How do I stop anchor links hiding under the header?

Add scroll-margin-top equal to the header height (plus a little breathing room) to the elements you link to.

What is the difference between scroll-margin-top and scroll-padding-top?

scroll-margin-top goes on each target element; scroll-padding-top goes once on the scrolling container (usually html) and applies to every target.

Why does my sticky header hide focused links?

The browser scrolls the focused element to the top edge, under the header. Add scroll-padding-top equal to the header height on html.

Find these bugs before your client does

Free account, 5 checks a day, no card. UICheck finds sideways scroll, cut-off text, overlaps and small tap targets on every screen.