How to create: Fixed menu

Learn how to use CSS to create a 'fixed' menu.

Try it yourself

How to create a fixed top menu

Step 1 - Add HTML:

<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
</div>
<div class="main">
  <p>Some text some text some text some text..</p>
</div>

Step 2 - Add CSS:

To create a fixed top menu, use position:fixed and top:0Please note that the fixed menu will cover your other content. To solve this problem, add an upper external margin (margin-top) equal to or greater than the menu height above the content.

/* Navigation bar */
.navbar {
  overflow: hidden;
  background-color: #333;
  position: fixed; /* Set the navigation bar to a fixed position */
  top: 0; /* Place the navigation bar at the top of the page */
  width: 100%; /* Full width */
}
/* Links within the navigation bar */
.navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
/* Change the background when the mouse hovers */
.navbar a:hover {
  background: #ddd;
  color: black;
}
/* Main content */
.main {
  margin-top: 30px; /* Add the top external margin to avoid content overlap */
}

Try it yourself

How to create a fixed bottom menu

To create a fixed bottom menu, use position:fixed and bottom:0:

/* Navigation bar */
.navbar {
  position: fixed; /* Set the navigation bar to a fixed position */
  bottom: 0; /* Place the navigation bar at the bottom of the page */
  width: 100%; /* Full width */
}
/* Main content */
.main {
  margin-bottom: 30px; /* Add the bottom external margin to avoid content overlap */
}

Try it yourself

Related pages

Tutorial:CSS navigation bar