collapsing-header.html 1.0 KB

12345678910111213141516171819202122
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Collapsing header</title>
  7. <style>
  8. body { margin: 0; font: 16px/1.5 system-ui, sans-serif; color: #111; background: #fff; }
  9. header { position: sticky; top: 0; height: 96px; padding: 0 48px; display: flex; align-items: center; background: #f6f5f2; border-bottom: 1px solid #ddd; }
  10. header.compact { height: 56px; }
  11. section { min-height: 100vh; padding: 32px 48px; box-sizing: border-box; border-bottom: 1px solid #ddd; }
  12. </style>
  13. </head>
  14. <body>
  15. <header id="top"><strong>Docs</strong></header>
  16. <main><section><h1>Collapsing header</h1><p>The header sits in the flow and shrinks by 40px once the page leaves the top, so the page is shorter while scrolled.</p></section><section><p>Second screen.</p></section><section><p>Third screen.</p></section></main>
  17. <script>
  18. const bar = document.getElementById("top");
  19. addEventListener("scroll", () => bar.classList.toggle("compact", scrollY > 0));
  20. </script>
  21. </body>
  22. </html>