Sticky Header that Shrinks
A header that stays at the top and shrinks with a soft shadow once you scroll, giving content more room.
AI prompt
Design a sticky site header inside a scrollable page. At the top it is 72 pixels tall with a 28 pixel logo mark, the wordmark "Atlas" and three links on the right; after scrolling more than 16 pixels it shrinks to 48 pixels, the logo scales to 20 pixels, and a soft shadow and bottom border appear. The transition takes 200 milliseconds with an ease-out and does not move the page content (content has a constant top offset). Use position sticky, a single scrolled class toggled from a passive scroll listener, links with visible focus outlines, and a prefers-reduced-motion fallback with no transition. The page below contains several placeholder content blocks so scrolling can be tested. Plain HTML, CSS and JavaScript.About this pattern
Definition
A shrinking sticky header stays visible while the page scrolls and becomes more compact after the first few pixels, so navigation is always available without taking too much space.
Purpose & UX principles
- Keeps navigation reachable on long pages while returning vertical space to content.
- Add a subtle shadow or border only after scrolling starts so the header separates from content.
- Avoid layout shifts: shrink with transforms or fixed heights and keep the content offset constant.
Interaction states
- Top of page (tall, no shadow), scrolled (compact, shadow), focus within header.
Motion specification
- Height, logo scale and shadow change over 200ms; the listener uses a passive scroll handler and a single class toggle.
- Reduced motion: the header changes size instantly.
Responsive behaviour
- On phones keep the compact height (48 to 56px) from the start; shrinking matters mostly on desktop.
- Account for the header height when linking to in-page anchors (scroll-margin-top).
Code
<div class="pg" id="pg"><header class="hd" id="hd"><span class="lg"><i></i><b>Atlas</b></span><nav aria-label="Main"><a href="#" onclick="return false">Work</a><a href="#" onclick="return false">About</a><a href="#" onclick="return false">Contact</a></nav></header><main><div class="bl"></div><div class="bl"></div><div class="bl"></div><div class="bl"></div><div class="bl"></div></main></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.pg{width:min(340px,100%);height:230px;overflow:auto;border-radius:12px;background:var(--surface);border:1px solid var(--line)}
.hd{position:sticky;top:0;z-index:2;height:72px;display:flex;align-items:center;justify-content:space-between;padding:0 14px;background:rgba(255,255,255,.92);-webkit-backdrop-filter:blur(8px);backdrop-filter:blur(8px);border-bottom:1px solid transparent;transition:height .2s ease-out,box-shadow .2s,border-color .2s}
.lg{display:flex;align-items:center;gap:8px;transform-origin:0 50%;transition:transform .2s ease-out}.lg i{width:28px;height:28px;border-radius:8px;background:var(--text);display:block}.lg b{font-size:16px}
.hd nav{display:flex;gap:12px}.hd a{font-size:13px;color:var(--text);text-decoration:none;padding:4px 2px}.hd a:hover{text-decoration:underline;text-underline-offset:3px}
.sc .hd{height:48px;border-color:var(--line);box-shadow:0 4px 14px rgba(0,0,0,.08)}.sc .lg{transform:scale(.78)}
main{padding:14px;display:grid;gap:10px}.bl{height:80px;border-radius:10px;background:var(--surface-2);border:1px solid var(--line)}
a:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.hd,.lg{transition:none}}const pg=document.getElementById('pg');let on=false;
pg.addEventListener('scroll',()=>{const s=pg.scrollTop>16;if(s!==on){on=s;pg.classList.toggle('sc',s);}},{passive:true});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sticky Header that Shrinks — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.pg{width:min(340px,100%);height:230px;overflow:auto;border-radius:12px;background:var(--surface);border:1px solid var(--line)}
.hd{position:sticky;top:0;z-index:2;height:72px;display:flex;align-items:center;justify-content:space-between;padding:0 14px;background:rgba(255,255,255,.92);-webkit-backdrop-filter:blur(8px);backdrop-filter:blur(8px);border-bottom:1px solid transparent;transition:height .2s ease-out,box-shadow .2s,border-color .2s}
.lg{display:flex;align-items:center;gap:8px;transform-origin:0 50%;transition:transform .2s ease-out}.lg i{width:28px;height:28px;border-radius:8px;background:var(--text);display:block}.lg b{font-size:16px}
.hd nav{display:flex;gap:12px}.hd a{font-size:13px;color:var(--text);text-decoration:none;padding:4px 2px}.hd a:hover{text-decoration:underline;text-underline-offset:3px}
.sc .hd{height:48px;border-color:var(--line);box-shadow:0 4px 14px rgba(0,0,0,.08)}.sc .lg{transform:scale(.78)}
main{padding:14px;display:grid;gap:10px}.bl{height:80px;border-radius:10px;background:var(--surface-2);border:1px solid var(--line)}
a:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
@media (prefers-reduced-motion:reduce){.hd,.lg{transition:none}}
</style></head>
<body><div class="stage">
<div class="pg" id="pg"><header class="hd" id="hd"><span class="lg"><i></i><b>Atlas</b></span><nav aria-label="Main"><a href="#" onclick="return false">Work</a><a href="#" onclick="return false">About</a><a href="#" onclick="return false">Contact</a></nav></header><main><div class="bl"></div><div class="bl"></div><div class="bl"></div><div class="bl"></div><div class="bl"></div></main></div>
</div>
<script>
const pg=document.getElementById('pg');let on=false;
pg.addEventListener('scroll',()=>{const s=pg.scrollTop>16;if(s!==on){on=s;pg.classList.toggle('sc',s);}},{passive:true});
</script>
</body></html>