How to create: a fixed sidebar

Learn how to use CSS to create a fixed side navigation menu.

Full Height:

Try it yourself

Auto Height:

Try it yourself

Create a fixed sidebar

Step 1 - Add HTML:

/* Side navigation */
<div class="sidenav">
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>
/* Page content */
<div class="main">
  ...
</div>

Step 2 - Add CSS:

/* Sidebar menu */
.sidenav {
  height: 100%; /* Full screen height: if you want an 'auto' height, please remove this setting */
  width: 160px; /* Set the width of the sidebar */
  position: fixed; /* Fixed sidebar (stays in place when scrolling) */
  z-index: 1; /* Always stay at the top */
  top: 0; /* Always stay at the top */
  left: 0;
  background-color: #111; /* Black */
  overflow-x: hidden; /* Disable horizontal scrolling */
  padding-top: 20px;
}
/* Navigation menu links */
.sidenav a {
  padding: 6px 8px 6px 16px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
}
/* Change the color when you hover over the navigation link */
.sidenav a:hover {
  color: #f1f1f1;
}
/* Set page content style */
.main {
  margin-left: 160px; /* Same as the width of the sidebar */
  padding: 0px 10px;
}
/* Change the sidebar style on smaller screens with height less than 450 pixels (less padding and smaller font size) */
@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}

Related Pages

Tutorial:CSS Navigation Bar

Tutorial:How to Create Side Navigation Bar