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

position: fixed header sits on top of the page heading, so only its last word shows.
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; }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); }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; }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.