Top Navigation
A sticky header that compacts and gains a border as the page scrolls.
AI prompt
Create a sticky top navigation inside a scrollable container. The bar shows a brand mark on the left, four links (Product, Docs, Pricing, Blog) and a primary button on the right. At scroll position 0 it is 72px tall and transparent. After the user scrolls more than 8px it shrinks to 52px over 250ms, gets a translucent background (rgba of the surface colour at 80%) with backdrop-filter: blur(12px) and a 1px bottom border. Links get an underline that scales in from the left on hover. Mark the current page with aria-current="page". Below 640px, hide the links and show a menu button that toggles a dropdown panel with aria-expanded and aria-controls; close it on Escape and when a link is chosen. Use a scroll listener with requestAnimationFrame throttling. Honour prefers-reduced-motion by disabling transitions.About this pattern
Definition
A horizontal bar at the top of the page holding the brand, the main destinations and one or two actions. In this version it shrinks and gains a blurred background once the content scrolls beneath it.
Purpose & UX principles
- Gives every page a stable anchor for orientation and for getting back home.
- Shrinking on scroll returns screen space to the content while keeping navigation one tap away.
- Keep the visible links to six or fewer. Move extras into a menu.
Interaction states
- At top: transparent, 72px tall.
- Scrolled: 52px tall, translucent surface with backdrop blur and a bottom border.
- Link hover: text brightens and an underline grows from the left.
- Current page: accent colour and
aria-current="page". - Menu open (mobile): button shows a close icon and
aria-expanded="true".
Motion specification
- Height and padding: 250ms ease.
- Background and border: 250ms linear.
- Underline: scaleX from 0 to 1, 220ms, origin left.
- Mobile panel: opacity and translateY(-8px), 200ms.
- Reduced motion: no transitions; state changes are instant.
Responsive behaviour
- Wide screens: links are shown inline.
- Under 640px: the links collapse into a menu button that opens a panel under the bar.
- The bar height changes from 72px to 52px, so reserve the larger height for the page's top padding to avoid layout jumps.
Code
<div class="frame" id="scroller">
<header class="topnav" id="bar">
<a class="brand" href="#">◐ Orbit</a>
<nav id="links" aria-label="Main"><a href="#" aria-current="page">Product</a><a href="#">Docs</a><a href="#">Pricing</a><a href="#">Blog</a></nav>
<button class="cta">Sign up</button>
<button class="burger" id="burger" aria-expanded="false" aria-controls="links" aria-label="Menu">☰</button>
</header>
<div class="content"><h2>Scroll this panel</h2><p>The bar above compacts once you scroll.</p><p>Keep scrolling…</p><p>…the border and blur appear.</p><p>Content continues here.</p><p>More content.</p><p>More content.</p><p>More content.</p></div>
</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}
.frame{width:min(520px,100%);height:420px;overflow:auto;border:1px solid var(--line);border-radius:18px;background:var(--surface);position:relative}
.topnav{position:sticky;top:0;z-index:5;display:flex;align-items:center;gap:20px;height:72px;padding:0 20px;border-bottom:1px solid transparent;transition:height .25s ease,background .25s linear,border-color .25s linear}
.topnav.scrolled{height:52px;background:rgba(255,255,255,.85);backdrop-filter:blur(12px);border-color:var(--line)}
.brand{font-weight:700;color:var(--text);text-decoration:none}
#links{display:flex;gap:18px;margin-left:auto}
#links a{position:relative;color:var(--muted);text-decoration:none;font-size:14px;padding:6px 0}
#links a::after{content:"";position:absolute;left:0;right:0;bottom:0;height:2px;background:var(--accent);transform:scaleX(0);transform-origin:left;transition:transform .22s}
#links a:hover,#links a[aria-current=page]{color:var(--text)}
#links a:hover::after,#links a[aria-current=page]::after{transform:scaleX(1)}
.cta{background:var(--accent);color:var(--accent-ink);border:0;border-radius:99px;padding:8px 16px;font:inherit;font-size:13px;font-weight:600;cursor:pointer}
.burger{display:none;background:none;border:0;color:var(--text);font-size:20px;cursor:pointer}
.content{padding:20px}.content p{color:var(--muted);margin:0 0 70px}
@media (max-width:540px){.burger{display:block;margin-left:auto}.cta{display:none}
#links{display:none;position:absolute;top:100%;left:12px;right:12px;flex-direction:column;gap:4px;margin:0;padding:12px;background:var(--surface-2);border:1px solid var(--line);border-radius:12px;opacity:0;transform:translateY(-8px)}
#links.open{display:flex;opacity:1;transform:none;transition:opacity .2s,transform .2s}}
@media (prefers-reduced-motion:reduce){.topnav,#links a::after{transition:none}}const sc=document.getElementById('scroller'),bar=document.getElementById('bar'),burger=document.getElementById('burger'),links=document.getElementById('links');
let ticking=false;
sc.addEventListener('scroll',()=>{if(ticking)return;ticking=true;requestAnimationFrame(()=>{bar.classList.toggle('scrolled',sc.scrollTop>8);ticking=false;});});
burger.addEventListener('click',()=>{const o=links.classList.toggle('open');burger.setAttribute('aria-expanded',o);});
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&links.classList.contains('open')){links.classList.remove('open');burger.setAttribute('aria-expanded','false');burger.focus();}});
links.addEventListener('click',e=>{if(e.target.matches('a')){e.preventDefault();links.classList.remove('open');burger.setAttribute('aria-expanded','false');}});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>Top Navigation — 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}}
.frame{width:min(520px,100%);height:420px;overflow:auto;border:1px solid var(--line);border-radius:18px;background:var(--surface);position:relative}
.topnav{position:sticky;top:0;z-index:5;display:flex;align-items:center;gap:20px;height:72px;padding:0 20px;border-bottom:1px solid transparent;transition:height .25s ease,background .25s linear,border-color .25s linear}
.topnav.scrolled{height:52px;background:rgba(255,255,255,.85);backdrop-filter:blur(12px);border-color:var(--line)}
.brand{font-weight:700;color:var(--text);text-decoration:none}
#links{display:flex;gap:18px;margin-left:auto}
#links a{position:relative;color:var(--muted);text-decoration:none;font-size:14px;padding:6px 0}
#links a::after{content:"";position:absolute;left:0;right:0;bottom:0;height:2px;background:var(--accent);transform:scaleX(0);transform-origin:left;transition:transform .22s}
#links a:hover,#links a[aria-current=page]{color:var(--text)}
#links a:hover::after,#links a[aria-current=page]::after{transform:scaleX(1)}
.cta{background:var(--accent);color:var(--accent-ink);border:0;border-radius:99px;padding:8px 16px;font:inherit;font-size:13px;font-weight:600;cursor:pointer}
.burger{display:none;background:none;border:0;color:var(--text);font-size:20px;cursor:pointer}
.content{padding:20px}.content p{color:var(--muted);margin:0 0 70px}
@media (max-width:540px){.burger{display:block;margin-left:auto}.cta{display:none}
#links{display:none;position:absolute;top:100%;left:12px;right:12px;flex-direction:column;gap:4px;margin:0;padding:12px;background:var(--surface-2);border:1px solid var(--line);border-radius:12px;opacity:0;transform:translateY(-8px)}
#links.open{display:flex;opacity:1;transform:none;transition:opacity .2s,transform .2s}}
@media (prefers-reduced-motion:reduce){.topnav,#links a::after{transition:none}}
</style></head>
<body><div class="stage">
<div class="frame" id="scroller">
<header class="topnav" id="bar">
<a class="brand" href="#">◐ Orbit</a>
<nav id="links" aria-label="Main"><a href="#" aria-current="page">Product</a><a href="#">Docs</a><a href="#">Pricing</a><a href="#">Blog</a></nav>
<button class="cta">Sign up</button>
<button class="burger" id="burger" aria-expanded="false" aria-controls="links" aria-label="Menu">☰</button>
</header>
<div class="content"><h2>Scroll this panel</h2><p>The bar above compacts once you scroll.</p><p>Keep scrolling…</p><p>…the border and blur appear.</p><p>Content continues here.</p><p>More content.</p><p>More content.</p><p>More content.</p></div>
</div>
</div>
<script>
const sc=document.getElementById('scroller'),bar=document.getElementById('bar'),burger=document.getElementById('burger'),links=document.getElementById('links');
let ticking=false;
sc.addEventListener('scroll',()=>{if(ticking)return;ticking=true;requestAnimationFrame(()=>{bar.classList.toggle('scrolled',sc.scrollTop>8);ticking=false;});});
burger.addEventListener('click',()=>{const o=links.classList.toggle('open');burger.setAttribute('aria-expanded',o);});
document.addEventListener('keydown',e=>{if(e.key==='Escape'&&links.classList.contains('open')){links.classList.remove('open');burger.setAttribute('aria-expanded','false');burger.focus();}});
links.addEventListener('click',e=>{if(e.target.matches('a')){e.preventDefault();links.classList.remove('open');burger.setAttribute('aria-expanded','false');}});
</script>
</body></html>