CSS Shrinking Header
A sticky header that shrinks, gains a background and casts a shadow over the first 120px of scrolling, in pure CSS.
<header class="shrink-head">
<a class="shrink-head-logo" href="#">Acme</a>
<nav aria-label="Main">
<a href="#">Docs</a>
<a href="#">Blog</a>
</nav>
</header>
.shrink-head {
position: sticky;
top: 0;
z-index: 10;
display: flex;
justify-content: space-between;
align-items: center;
padding: 22px 20px;
color: #0f172a;
font-family: system-ui, sans-serif;
}
.shrink-head-logo {
color: inherit;
font-size: 24px;
font-weight: 800;
text-decoration: none;
}
.shrink-head nav {
display: flex;
gap: 16px;
}
.shrink-head nav a {
color: #475569;
font-size: 14px;
font-weight: 600;
text-decoration: none;
}
.shrink-head a:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 3px;
border-radius: 2px;
}
@supports (animation-timeline: scroll()) {
.shrink-head {
animation: shrink-head-pad linear both;
animation-timeline: scroll();
animation-range: 0 120px;
}
.shrink-head-logo {
animation: shrink-head-logo linear both;
animation-timeline: scroll();
animation-range: 0 120px;
}
}
@keyframes shrink-head-pad {
to {
padding-block: 10px;
background: rgba(255, 255, 255, 0.9);
box-shadow: 0 4px 16px -8px rgba(15, 23, 42, 0.3);
}
}
@keyframes shrink-head-logo {
to {
font-size: 18px;
}
}
@media (prefers-reduced-motion: reduce) {
.shrink-head,
.shrink-head-logo {
animation: none;
}
}
About this CSS Shrinking Header
This CSS Shrinking Header is 71 lines of pure CSS (1.2 KB) with no JavaScript and no dependencies. It uses scroll-driven animations, keyframe animations, box shadows and Flexbox. It relies on scroll-driven animations, which not every browser supports yet, so check the browser notes below. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
- scroll-driven animationsChrome and Edge 115+ and Safari 26+. Firefox support is very recent; browsers without it skip the animation and show the content as is.
- keyframe animations
- box shadows
- Flexbox
- sticky positioning
- keyboard focus styles
- reduced-motion support
Customize it
The colors in this snippet. Search and replace them to match your brand:
#0f172a#475569#2563eb
How to use this shrinking header
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.shrink-headclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.