An Astro sticky header needs one CSS rule: position: sticky; top: 0 on the <header>, plus a z-index and a background so content doesn’t show through. Add scroll-padding-top on html so anchor links don’t land under it. A small optional script adds a shadow once the page scrolls, or hides the header while scrolling down.
Key Takeaways
position: stickykeeps the header in the document flow, so there’s no layout jump.- A parent with
overflow: hiddenoroverflow: autobreaks sticky; check ancestors first.scroll-padding-topstops in-page links from hiding headings under the header.- Effects like a scroll shadow need only an
IntersectionObserver, not a scroll listener.
The CSS
---
// src/components/Header.astro
---
<header class="site-header">
<a href="/" class="logo">Example</a>
<nav aria-label="Main">
<a href="/blog/">Blog</a>
<a href="/about/">About</a>
</nav>
</header>
<style>
.site-header {
position: sticky;
top: 0;
z-index: 40;
display: flex;
align-items: center;
justify-content: space-between;
height: 4rem;
padding-inline: 1.5rem;
background: rgb(255 255 255 / 0.85);
backdrop-filter: blur(8px);
border-bottom: 1px solid transparent;
transition: border-color 0.2s, box-shadow 0.2s, transform 0.25s;
}
</style>
/* src/styles/global.css */
html { scroll-padding-top: 5rem; }
position: sticky keeps the header in normal flow until it reaches top: 0, then pins it. Unlike position: fixed, the content below doesn’t need padding to avoid sliding under it. scroll-padding-top (a bit more than the header height) applies to every #anchor jump, including heading anchor links and table of contents links.
Why sticky isn’t sticking
The usual causes:
- An ancestor has
overflowset to anything other thanvisible. Sticky positions relative to the nearest scrolling ancestor, sooverflow: hiddenon a wrapper (often added to stop horizontal scroll) breaks it. Useoverflow-x: clipinstead, which doesn’t create a scroll container. - The header’s parent is only as tall as the header. Sticky can’t move outside its parent. Put the header directly inside
<body>or a full-height wrapper. - No
topvalue. Sticky needstop(orbottom) to know where to stick.
Add a shadow once the page scrolls
Put an empty sentinel at the very top of the page and watch it. When it leaves the viewport, the page has scrolled:
<!-- src/layouts/BaseLayout.astro (body excerpt) -->
<div id="top-sentinel" aria-hidden="true" style="position:absolute; top:0; height:1px"></div>
<Header />
<slot />
<script>
const header = document.querySelector('.site-header');
const sentinel = document.getElementById('top-sentinel');
if (header && sentinel) {
new IntersectionObserver(([entry]) => {
header.classList.toggle('is-scrolled', !entry.isIntersecting);
}).observe(sentinel);
}
</script>
<style is:global>
.site-header.is-scrolled {
border-bottom-color: rgb(0 0 0 / 0.08);
box-shadow: 0 4px 16px rgb(0 0 0 / 0.06);
}
</style>
An observer only fires when the state changes, so it costs nothing while scrolling, unlike a scroll event handler that runs on every frame.
Hide on scroll down, show on scroll up
This pattern frees screen space on phones. It does need scroll position, so throttle it with requestAnimationFrame:
// inside the same <script>
let lastY = window.scrollY;
let ticking = false;
window.addEventListener('scroll', () => {
if (ticking) return;
ticking = true;
requestAnimationFrame(() => {
const y = window.scrollY;
header?.classList.toggle('is-hidden', y > lastY && y > 200);
lastY = y;
ticking = false;
});
}, { passive: true });
.site-header.is-hidden { transform: translateY(-100%); }
.site-header:focus-within { transform: none; }
@media (prefers-reduced-motion: reduce) { .site-header { transition: none; } }
:focus-within brings the header back when a keyboard user tabs into it. Use this pattern sparingly; a header that moves can be disorienting, and on desktop it rarely saves much space.
With view transitions
If you use <ClientRouter />, add transition:persist to the header to keep it mounted between pages, and register listeners inside an astro:page-load handler so they’re set up once per navigation. Our view transitions guide covers both. For the menu inside the header, see the mobile nav component, and the styling docs explain where global styles live in a project. If you’re customising one of the Astro themes, check its header component for an existing sticky class first.
FAQ
How do I make the header sticky in Astro?
Add position: sticky; top: 0; with a z-index and a solid or translucent background to your header’s styles. Make sure no ancestor element has overflow: hidden or overflow: auto, which stops sticky positioning.
Why does my anchor link scroll under the sticky header?
The browser scrolls the target to the very top, behind the header. Set scroll-padding-top on html to a little more than the header’s height, or scroll-margin-top on the target headings.
Should I use position fixed or sticky for a header?
Use sticky in most cases. It stays in the document flow, so you don’t need to add top padding to the page, and it only pins once it reaches the top. Fixed is useful when the header must overlay a full-screen hero from the first pixel.