Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created September 11, 2023 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/0ca47c16b0afcfd9d805df7e9b59ce1b to your computer and use it in GitHub Desktop.
Save code-boxx/0ca47c16b0afcfd9d805df7e9b59ce1b to your computer and use it in GitHub Desktop.
Sticky Headers In HTML CSS

STICKY HTML CSS HEADER

https://code-boxx.com/sticky-headers-html-css/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (A) STICKY HEADER */
#page-head { position: sticky; top: 0; z-index: 9; }
/* (X) DOES NOT MATTER - COSMETICS */
* { font-family: arial, sans-serif; box-sizing: border-box; }
body { padding: 0; margin: 0; }
#page-nav, #page-head, #page-main { padding: 20px; }
#page-nav { background: #f5ddff; }
#page-head { color: #fff; background: #464646; }
#page-main { height: 3000px; } /* extra height for testing */
<!DOCTYPE html>
<html>
<head>
<title>Sticky Header Demo</title>
<link rel="stylesheet" type="text/css" href="1-sticky.css">
</head>
<body>
<!-- (A) NAV BAR -->
<nav id="page-nav">TOP NAV</nav>
<!-- (B) STICKY HEADER -->
<header id="page-head">HEADER</header>
<!-- (C) CONTENTS -->
<main id="page-main">CONTENTS</main>
</body>
</html>
/* (A) HEADER */
#page-head {
position: fixed; top: 0; left: 0; z-index: 9; /* fixed at top */
width: 100%; height: 60px; padding: 10px; /* dimensions */
display: flex; align-items: center; /* alignment */
}
/* (B) CONTENTS */
/* add top spacing to prevent being covered by header */
#page-main { margin-top: 80px; padding: 10px; }
/* (X) COSMETICS - DOES NOT MATTER */
* { font-family: arial, sans-serif; box-sizing: border-box; }
body { padding: 0; margin: 0; }
#page-head { color: #fff; background: #464646; }
#page-main { height: 3000px; } /* extra height for testing */
<!DOCTYPE html>
<html>
<head>
<title>Fixed Header Demo</title>
<link rel="stylesheet" type="text/css" href="2-fixed.css">
</head>
<body>
<!-- (A) STICKY HEADER -->
<header id="page-head">HEADER</header>
<!-- (B) CONTENTS -->
<main id="page-main">CONTENTS</main>
</body>
</html>
/* (A) STICKY HEADER */
#page-head { position: sticky; top: -1px; z-index: 9; }
#page-head.stuck { background: red; }
/* (X) DOES NOT MATTER - COSMETICS */
* { font-family: arial, sans-serif; box-sizing: border-box; }
body { padding: 0; margin: 0; }
#page-nav, #page-head, #page-main { padding: 20px; }
#page-nav { background: #f5ddff; }
#page-head { color: #fff; background: #464646; }
#page-main { height: 3000px; } /* extra height for testing */
<!DOCTYPE html>
<html>
<head>
<title>Sticky Header Demo</title>
<link rel="stylesheet" type="text/css" href="3-stuck.css">
<script src="3-stuck.js"></script>
</head>
<body>
<!-- (A) NAV BAR -->
<nav id="page-nav">TOP NAV</nav>
<!-- (B) STICKY HEADER -->
<header id="page-head">HEADER</header>
<!-- (C) CONTENTS -->
<main id="page-main">CONTENTS</main>
</body>
</html>
// CREDITS : https://davidwalsh.name/detect-sticky
window.addEventListener("load", () => {
const observer = new IntersectionObserver(
([e]) => e.target.classList.toggle("stuck", e.intersectionRatio < 1),
{ threshold: [1] }
);
observer.observe(document.getElementById("#page-head"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment