All effectsScroll & reveal

Scroll Collapsing Header

滚动折叠标题栏

A tall heading gradually condenses into a compact sticky bar while secondary text fades out.

Scroll inside the example

Where to use it

Give a report or product page a strong opening while retaining section identity below the fold. Carry the page title or another stable identifier into the collapsed bar. Keep interactive elements reachable during both states.

Practical example

In an event schedule, the opening header shows the conference date, venue, and title. As attendees scroll into the itinerary, it condenses into a slim bar that still displays the conference name.

AI prompt

Add Scroll Collapsing Header to [target]. Use a sticky header and map a short fixed scroll interval to the title scale and secondary content height. Keep the actual heading in the DOM and use a discrete compact state for reduced motion. Keep the controls keyboard accessible and provide a reduced-motion state.

Example code

Save the standalone HTML as a file and open it in your browser. It uses the same code as the preview.

View HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Scroll Collapsing Header</title>
  <style>
    :root {
      color-scheme: dark;
      --demo-bg: #171629;
      --demo-ink: #f8f5ff;
      --demo-accent: #b9a6ff;
      --demo-muted: #bfbbd3;
      font-family: system-ui, sans-serif;
      color: var(--demo-ink);
      background: var(--demo-bg);
    }

    *, *::before, *::after { box-sizing: border-box; }
    body { margin: 0; min-width: 0; }
    button, input { font: inherit; }

    .demo {
      display: grid;
      place-items: center;
      align-content: center;
      min-height: 100vh;
      padding: 14px;
      overflow: hidden;
      background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
    }

    .demo-button {
      border: 1px solid #ffffff3d;
      border-radius: 7px;
      background: #ffffff12;
      color: var(--demo-ink);
      padding: 6px 11px;
      font-size: 12px;
      cursor: pointer;
    }

    :focus-visible {
      outline: 2px solid var(--demo-accent);
      outline-offset: 3px;
    }

    .scroll-port {
      --frame: clamp(112px, 75vh, 242px);
      --p: 0;
      width: min(100%, 290px); height: var(--frame);
      overflow-y:auto; overflow-x:hidden; scroll-behavior:auto;
      overscroll-behavior:contain; scrollbar-width:thin;
      scrollbar-color:#a79bd5 #20223d; border-radius:12px;
      border:1px solid #a79bd567; background:#20233c; color:#f5f2ff;
    }
    .scroll-port:focus-visible { outline-offset: 3px; }
    .scroll-track { position:relative; min-height:calc(var(--frame) + 235px); }
    .scroll-pin { position:sticky; top:0; height:var(--frame); overflow:hidden; }
    .scroll-pin .tag { position:absolute; z-index:4; left:8px; bottom:9px; font-size:10px; color:#ecfcff; padding:4px 6px; background:#17152ce8; border-radius:4px; }
    .scroll-panel { min-height:62px; padding:9px; border:1px solid #adb2e345; border-radius:8px; background:linear-gradient(120deg,#314861,#3e346a); }


    .condensing { position:sticky; top:0; z-index:3; color:#fbf9ff; background:linear-gradient(125deg,#315777,#422b67); border-bottom:1px solid #a1afda99; padding:7px 9px; }
    .condensing strong { display:block; width:max-content; max-width:75%; font-size:17px; transform-origin:left center; transform:scale(calc(1.14 - .28 * var(--compact,0))); }
    .condensing span { display:block; color:#d7d7f6; font-size:10px; max-height:calc(18px * (1 - var(--compact,0))); overflow:hidden; opacity:calc(1 - var(--compact,0)); }
    .condensing i { position:absolute; right:10px; top:7px; transform:rotate(calc(var(--compact,0) * 70deg)); color:#97f1e1; }
    .condensing-pages { display:grid; gap:7px; padding:7px; }
    .condensing-pages p { min-height:46px; margin:0; padding:7px; border:1px solid #abb3e250; background:linear-gradient(100deg,#243f4e,#2f385b); border-radius:7px; font-size:12px; }
    @media (prefers-reduced-motion:reduce) { .condensing i { transform:none; } }

    @media (prefers-reduced-motion: reduce) {
      .lab, .lab *, .lab::before, .lab::after, .lab *::before, .lab *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
      }
    }
  </style>
</head>
<body>
  <main class="demo">
    <div class="lab scroll-port" role="region" tabindex="0" aria-label="Scroll to condense the header"><header class="condensing"><strong>Field Notes</strong><span>Northern coastline</span><i aria-hidden="true">✦</i></header>
    <div class="condensing-pages"><p>North inlet coordinates</p><p>Lighthouse drawings</p><p>Tidal markers</p><p>Offshore route</p><p>Return</p></div></div>
  </main>
  <script>
    const port = document.querySelector('.scroll-port');
    const reduced = matchMedia('(prefers-reduced-motion: reduce)');
    const listeners = [];
    let scheduled = 0;
    function sample() {
      const max = Math.max(1,port.scrollHeight - port.clientHeight);
      return Math.max(0,Math.min(1,port.scrollTop / max));
    }
    function update() {
      scheduled = 0;
      const progress = sample();
      port.style.setProperty('--p', progress.toFixed(4));
      for (const callback of listeners) callback(progress);
    }
    function schedule() { if (!scheduled) scheduled = requestAnimationFrame(update); }
    function onScroll(callback) { listeners.push(callback); callback(sample()); }
    port.addEventListener('scroll', schedule, {passive:true});
    new ResizeObserver(schedule).observe(port);
    reduced.addEventListener('change', schedule);
    update();

    let compactFrame = 0;
    function condense() {
      compactFrame = 0;
      const phase = Math.max(0,Math.min(1,port.scrollTop / 75));
      port.style.setProperty('--compact', reduced.matches ? Number(phase >= .5) : phase.toFixed(3));
    }
    port.addEventListener('scroll',() => { if (!compactFrame) compactFrame = requestAnimationFrame(condense); },{passive:true});
    reduced.addEventListener('change',condense);
    condense();
  </script>
</body>
</html>

Original example by UI Skills

Further examples