SmartCodingTips

Sticky Header & Footer

A sticky header or footer remains visible as the user scrolls through the page. This is useful for navigation menus, page titles, or footers with important links.

Sticky Header Example


<style>
  header {
    position: sticky;
    top: 0;
    background-color: #333;
    color: white;
    padding: 15px;
    text-align: center;
    z-index: 1000;
  }
</style>

<header>
  This is a sticky header!
</header>

<main>
  <p>Scroll down to see the header stay in place...</p>
  <div style="height: 1000px;"></div>
</main>
            

Sticky Footer Example

To keep the footer always visible at the bottom of the viewport:


<style>
  body, html {
    margin: 0;
    height: 100%;
  }

  .wrapper {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
  }

  footer {
    background: #222;
    color: white;
    text-align: center;
    padding: 10px;
    position: sticky;
    bottom: 0;
  }

  main {
    flex: 1;
    padding: 20px;
  }
</style>

<div class="wrapper">
  <main>
    <p>Content goes here...</p>
  </main>
  <footer>
    Sticky Footer Content
  </footer>
</div>
            

When to Use Sticky Elements

  • Navigation bars for quick access
  • Important page actions (e.g. "Save" or "Next")
  • Persistent toolbars or messages
  • Footer disclaimers or contact buttons