About this snippet

Create a sticky header or navigation bar that stays fixed at the top of the viewport when the user scrolls. This simple CSS snippet uses `position: sticky` for improved site navigation.

Size
1.3 KB
Uses
HTML, CSS
Loads
Nothing external
Views
392
<header class="sticky-header">
  <div class="logo">MySite</div>
  <nav>
    <a href="#section1">Section 1</a>
    <a href="#section2">Section 2</a>
    <a href="#section3">Section 3</a>
  </nav>
</header>
<main class="page-content">
  <section id="section1" style="height: 600px; background-color: #e9f7ef;"><h1>Section 1</h1></section>
  <section id="section2" style="height: 600px; background-color: #fdecea;"><h1>Section 2</h1></section>
  <section id="section3" style="height: 600px; background-color: #eaf2f8;"><h1>Section 3</h1></section>
</main>
body {
  margin: 0;
  font-family: Arial, sans-serif;
  color: #333;
}
.sticky-header {
  position: sticky;
  top: 0;
  background-color: #2c3e50;
  color: white;
  padding: 15px 30px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1000; /* Ensure it stays on top of other content */
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.sticky-header .logo {
  font-size: 1.8em;
  font-weight: bold;
}
.sticky-header nav a {
  color: white;
  text-decoration: none;
  margin-left: 20px;
  font-size: 1em;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}
.sticky-header nav a:hover {
  background-color: #34495e;
}
.page-content section {
  padding: 40px;
  box-sizing: border-box;
}
.page-content section h1 {
  margin-top: 0;
  text-align:center;
}
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save