A sticky header is one of those features that gets approved in a design review in about ten seconds. Keep the navigation visible while the page scrolls, give people a way to move around without scrolling back to the top, done. Then it ships, and a week later someone files a bug that says the page "jumps to the wrong place" when they click a table of contents link.
The bug is real, and it is almost never the page's fault. The browser scrolled exactly where it was told to, and a fixed bar sitting on top of the viewport covered the heading it landed on. That single problem is the tip of a longer list: sticky headers also eat vertical space on small screens, fight with the on-screen keyboard, hide focused elements from keyboard users, and quietly shift layout when they change size. This guide walks through the design decisions that prevent each of those, in the order you are likely to run into them.

Photo by Nicolás Rueda on Pexels
Decide whether the page needs a sticky header at all
The first decision is the one most teams skip. A sticky header earns its place when people move between sections of a long page or an application frequently, and the cost of scrolling back to the top is real. A documentation site, a dashboard with several areas, a long product page with a buy button: all reasonable candidates.
A short marketing page with three sections and one call to action probably does not need it. A header that follows the reader down a page they could finish in one screenful is just permanent clutter. Write down what the sticky header lets someone do that they could not do otherwise. If the answer is vague, the answer is probably "nothing," and the simplest option is a normal header that scrolls away.
There is also a middle path worth knowing about. Many teams use a header that hides while scrolling down and returns when the reader scrolls up, on the theory that scrolling up signals intent to navigate. It recovers screen space and keeps navigation within one gesture. It also adds motion and state, which means more to test, so treat it as an upgrade you choose deliberately rather than the default.
Budget the screen space before you design the bar
Fixed elements subtract from the visible area for the entire visit. On a desktop monitor a 72 pixel header is a rounding error. On a phone held upright, with the browser's own address bar and toolbar already taking their share, the same bar can consume close to a tenth of the usable height, and that is before any cookie notice or promotional strip is stacked underneath it.
Set a budget and defend it. A common working rule is to keep the sticky portion to around 56 pixels on small screens, and to collapse anything secondary. The full-height version with the large logo, the tagline, and the two-row navigation can exist at the top of the page, then shrink to a slim bar once the reader has scrolled past it. People rarely need the tagline at the three-thousandth pixel.
Count every fixed element, not just the header. A sticky header, a fixed chat bubble, a bottom tab bar, and a consent banner can together leave a few hundred pixels of actual content on a small phone. Add them up on a real device, with the browser chrome showing, before you sign off on the design.

Photo by cottonbro studio on Pexels
Fix the anchor link problem with scroll padding, not hacks
Now the bug from the opening. When a link points to #pricing, the browser scrolls so the target element sits at the very top of the viewport. If a fixed header occupies that top strip, the heading ends up underneath it. The reader clicked a link and sees what looks like the middle of a paragraph.
Older fixes tended to be ugly. People added invisible spacer elements, or padded every heading with a negative margin trick, or wrote JavaScript that listened for hash changes and nudged the scroll position after the fact. Those all work until they do not, and the JavaScript version flickers because the page visibly jumps twice.
Modern CSS handles this directly. The scroll-padding-top property tells the scroll container to leave space at the top when it scrolls something into view, and you set it once on the root element. Its sibling scroll-margin-top does the same job on individual targets, which is useful when only some headings need extra room. Using a CSS custom property for the header height keeps the number in one place, so the padding and the header always agree.
:root {
--header-height: 56px;
}
html {
scroll-padding-top: calc(var(--header-height) + 16px);
}
.site-header {
position: sticky;
top: 0;
height: var(--header-height);
}
The extra sixteen pixels is deliberate breathing room, so the heading does not sit pressed against the bar's lower edge. If the header height changes at a breakpoint, change the variable at that breakpoint and everything follows.
Choose between sticky and fixed on purpose
Two positioning values do similar-looking things, and the difference matters. position: fixed removes the element from normal flow and pins it to the viewport, so you must reserve its space yourself, usually with top padding on the body. Forget that step and the first few lines of content slide under the bar from the first frame.
position: sticky keeps the element in the flow until it reaches its threshold, then holds it in place. Because it occupies its own space in the layout, nothing slides underneath it by default, and when it is placed inside a container it stops at the container's end. That makes it the better default for headers, section titles, and table headings. The CSS positioning documentation on MDN covers the full behavior, including the common gotcha that a sticky element silently stops working if any ancestor has overflow: hidden or overflow: auto set.
When a sticky header refuses to stick, check the ancestors before anything else. A wrapper added by a layout library or a "clip the overflowing animation" rule three levels up is the usual culprit, and the symptom is just that the header scrolls away as if the property were not there.
Keep keyboard focus from disappearing behind the bar
Anchor links are the visible symptom. The less visible one affects people who navigate with the keyboard. Tab through a long form with a fixed header and the browser scrolls the focused field into view, sometimes stopping with the field tucked underneath the bar. The user is typing into something they cannot see.
This is a documented accessibility failure. The Web Content Accessibility Guidelines include a criterion on focus not being obscured, which says a focused component should not be entirely hidden by author-created content such as sticky headers. The same scroll-padding-top rule you added for anchor links helps here too, because the browser respects it when it scrolls focus into view. That makes one line of CSS responsible for two fixes.
Test it the plain way. Put the mouse away, tab through the page from top to bottom, then shift-tab back up. Watch for any control that lands behind the header, and watch the bottom of the page for fixed footers doing the same thing in reverse. If you add a skip link, make sure it is the first focusable element and that it is visible when focused, since a skip link that appears under a sticky bar is of no use to anyone.

Photo by Afitab on Pexels
Stop the header from causing layout shift
A header that changes size while the reader scrolls is a layout shift risk. The classic pattern shrinks the bar from 96 pixels to 56 once the page scrolls, and if the header is in normal flow, everything below it jumps up by 40 pixels at that moment. Readers lose their place, and the shift shows up in your performance data.
There are two clean ways to avoid it. One is to keep the header's reserved space constant and animate only what is visible inside it, so the layout box never changes. The other is to switch to position: fixed for the shrunken state with a placeholder of the original height holding the space. Either way, animate transform and opacity rather than height, because those do not force the browser to recalculate layout on every frame.
Google's guidance on Cumulative Layout Shift explains what counts and how it is measured. A shrinking header that causes a one-time shift of a few dozen pixels can be enough to move a page from a good score into a borderline one, and unlike many causes of shift, this one is entirely under your control.
Handle mobile browsers, keyboards, and the safe area
Phones add their own complications. The browser's address bar slides away as the reader scrolls, which changes the size of the viewport underneath your fixed elements. Units like 100vh have historically behaved inconsistently across that change, which is why the newer dvh and svh units exist. If your header or any full-height overlay depends on viewport height, test with the address bar both shown and hidden.
On devices with a notch or rounded corners, a fixed header at the top edge can sit under the status area. The env(safe-area-inset-top) value lets you pad the bar by exactly the amount the hardware needs, provided your viewport meta tag opts in with viewport-fit=cover. It is a small detail that separates a header that looks native from one that looks clipped.
The on-screen keyboard deserves a test too. When a text field gains focus on a phone, the keyboard takes roughly half the screen, and a sticky header plus a sticky footer can leave a sliver of space to see what you are typing. If the page has forms, consider letting the header scroll away on small screens, or at least hide secondary bars while an input is focused.
"Every fixed element is a small tax on the screen, paid by every visitor on every page. The teams that get sticky headers right are the ones who count that tax on a real phone before they decide the header needs to follow the reader." - Dennis Traina, founder of 137Foundry
Keep dropdowns, anchors, and overlays working together
A sticky header rarely lives alone. It usually contains a dropdown menu, a search field, and sometimes a mobile menu that opens as a full-screen overlay. Each of those interacts with the stacking order and the scroll behavior in ways that are easy to get wrong.
Set the header's z-index deliberately and document the scale. A sticky header at z-index: 100, dropdowns at 110, modals at 1000, and toasts above that is a system a team can reason about. Random values like 99999 sprinkled around by different people are how you end up with a date picker that opens behind the navigation bar.
When the mobile menu opens as an overlay, prevent the page behind it from scrolling, return focus to the menu button when it closes, and make sure the Escape key dismisses it. The WAI-ARIA Authoring Practices guide has patterns for disclosure navigation that cover these details, and following them is usually faster than inventing your own and discovering the gaps later.

Photo by Brett Sayles on Pexels
Test it like a user who is not you
Sticky header bugs hide in combinations. A header can behave perfectly in your browser at your window size and fail for someone with a larger default font, a zoomed page, or a narrow split-screen window. A header whose height was set in pixels will not grow when the user raises their text size, so a two-line navigation label can overflow its bar and spill over the content below.
A short checklist covers most of it:
- Click every in-page link and confirm the target heading is fully visible, not clipped by the header.
- Tab through the page and back, watching for focused elements hidden behind the bar.
- Zoom the page to 200 percent and increase the browser's default font size.
- Open the page on a real phone, with the address bar visible and hidden, and with the keyboard open on a form.
- Scroll slowly past the point where the header changes state and watch for jumps in the content below.
- Check that the header does not cover the top of a printed page or a page loaded from a deep link.
Run this once when the header is built and again whenever someone adds a banner, a cookie notice, or a new row of navigation. Those additions are how a header that worked in January stops working by June.
Bringing it together
A sticky header that stays out of the reader's way comes down to a short list of decisions made up front. Confirm the page actually benefits from persistent navigation. Budget the space it takes, especially on small screens. Use scroll-padding-top so anchor links and keyboard focus land in clear view. Prefer position: sticky and watch for overflow on ancestors. Avoid resizing the header in a way that shifts the layout, and test on real devices with real keyboards.
None of this is difficult, and most of it is a handful of lines of CSS. The difficulty is remembering that a header is not only a design object. It is a piece of the page that scrolls, focuses, resizes, and stacks with everything else, and the problems show up in the seams between those behaviors.
If you are rebuilding navigation on a site that has grown beyond its original layout, 137Foundry's web development service can audit how the header, anchors, and overlays behave across real devices and fix the underlying structure. You can also browse our full list of services or read about how we work. For a technical reference on the properties used throughout this guide, MDN's CSS documentation is a dependable starting point, and the homepage at 137foundry.com has more of our writing on building sites that hold up in daily use.