CSS Sidebar Menus

This article demonstrates how to create several CSS sidebar menus for your webpage. Included are code walkthroughs and other sidebar examples.

What Is a Sidebar Menu in CSS?

The sidebar menu phrase refers to an area in a webpage that provides access to top-level collections of links and enables quick app navigation to the content in your app, game, or article. In most cases, a sidebar is a column or pillar that can include typography and icons that appear at the side of the main content – either on the left or on the right in a side pane of a split view, depending on a website layout and structure. A sidebar in a web application can be a creative, unique, and valuable HTML component of website navigation design.

HTML Sidebar Menu Diagram
HTML Sidebar Menu Diagram

What Is a Sidebar Layout?

With sidebar navigation, you have several options: You can have a navigation pane always displayed to the left of the page content; Use collapsible and responsive side navigation; Have a navigation pane that slides over the left part of the page content; Or even a navigation pane that opens to cover a whole page.

Where Is a Sidebar Menu Located?

A sidebar is a form of an auxiliary menu that appears at the edge of a web page or application. In simple words, a sidebar is usually a vertical column placed on either the left or right of a web page’s primary content.

Creating a Sidebar Menu

To demonstrate several different types of sidebar menus, we need to create a web page with HTML and CSS to create an example we can edit. First, we must generate HTML with links, a toggle button, and a close button. Then we will add some CSS.

Let’s go over how a side navigation menu is created. We won’t use JavaScript to add or remove class names to elements to create a functional menu; instead, we will use label elements mixed with a hidden checkbox input. The input and labels allow us to use a CSS :has() pseudo-class to check if a body element has an input element with a value set to check. Once an input element is in a check state a CSS selector can select any element within the webpage because all the HTML elements are contained within the body.

Down below, we have created an HTML structure for you.

HTML

<!-- Use any element to open the sidenav -->
<nav>
  <label for="menu-toggle" class="menu-button">Menu</label>
  <input type="checkbox" id="menu-toggle" name="menu" value="">
  <div id="mySidenav" class="sidenav">
    <label class="closebtn" for="menu-toggle">&times;</label>
    <a href="#">Home</a>
    <a href="#">Resources</a>
    <a href="#">Guides</a>
    <a href="#">Examples</a>
  </div>
</nav>

<!-- Add all page content inside this div if you want the side nav to push page content to the right (not used if you only want the sidenav to sit on top of the page -->
<div id="main">
  Main Content
</div>

Here we provide the CSS code that decorates the webpage and gives the sidebar menu a simple visual look. The following four examples will use this same CSS to demonstrate different ways you can implement a sidebar menu.

CSS

html,
body {
  margin: 0;
  font-family: Arial;
}

html {
  margin-top: 50px;
}

.sidebar {
  transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
}

/* The side navigation menu */
.sidenav {
  height: 100%; /* 100% Full-height */
  width: 0; /* 0 width - change this with JavaScript */
  position: fixed; /* Stay in place */
  z-index: 1; /* Stay on top */
  top: 50px; /* Stay at the top */
  left: 0;
  background: #634d8d; /* Purple */
  overflow-x: hidden; /* Disable horizontal scroll */
  padding-top: 60px; /* Place content 60px from the top */
}

/* The navigation menu links */
.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #fff;
  display: block;
  transition: 0.3s;
}

/* When you mouse over the navigation links, change their color */
.sidenav a:hover {
  color: #f1f1f1;
}

/* Position and style the close button (top right corner) */
.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  color: white;
  cursor: pointer;
  height: 50px;
  line-height: 55px;
}

/* Style page content - use this if you want to push the page content to the right when you open the side navigation */
#main {
  transition: margin-left 0.5s;
  padding: 20px;
}

/* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
@media screen and (max-height: 450px) {
  .sidenav a {
    font-size: 18px;
  }
}

nav {
  height: 50px;
  background: #c1c1c1;
}

.menu-button {
  background: #4646cc;
  height: 40px;
  margin: 5px;
  float: left;
  line-height: 40px;
  width: 100px;
  text-align: center;
  color: white;
  border-radius: 4px;
  cursor: pointer;
}

#menu-toggle {
  display: none;
}

/* Move the menu if the button is clicked */
body:has(#menu-toggle:checked) #mySidenav {
  width: 250px !important;
}

Animating The Sidebar Menu

Webpage With Animated Sidebar Menu
Webpage With Animated Sidebar Menu

Animated sidebar menus are where a menu is transitioned or moved animatedly. For example, a user can click a menu icon, and a side section of a page containing links fades in from the left side.

Let’s add a property to animate a menu when a user interacts with a page. All that is required is adding a transition property to the element with a .sidebar class.

CSS

.sidenav {
  transition: 0.5s; /* 0.5 second transition effect to slide in the sidenav */
}

Making the Sidebar Fixed

Fixed Sidebar Menu
Fixed Sidebar Menu

A fixed sidebar menu is helpful whenever you want some links and other buttons in a menu constantly to be accessible to a user. In this case, we need to change the width property from 0 to 250px.

CSS

/* Replace 0 width value with a value of 250px */
*/
.sidenav {
  width: 0;
} 
*/

/* The new width value forces the sidebar to be fixed */
.sidenav {
  width: 250px;
}

Make The Sidebar Push Content

Sidebar Menu That Pushes Content
Sidebar Menu That Pushes Content

Content pushed or nudged to a side when a side menu is opened is also possible. To achieve this effect, we can add another :has() pseudo-element class that targets the body element and applies a margin equal to the size of the .sidebar class. In addition to adding the second :has() pseudo-element, we must give the .sidebar and body the same transition time, so there is a smooth animation.

CSS

/* Move the body if the button is clicked */
body:has(#menu-toggle:checked) {
  margin-left: 250px;
}

.sidebar, body {
  transition: 0.5s; /* 0.5 second transition effect to slide the side navigation and body */
}

Making the Sidebar Expand to Full-Width

Expanding Full-Width Sidebar Menu
Expanding Full-Width Sidebar Menu

Another sidebar we can create is a menu that expands over an entire page. To do this, we can set a width value to 100% in the .sidebar class instead of an absolute or relative value. The above image shows that the side section expands across a whole page when the menu is opened.

CSS

/* Move the menu if the button is clicked */
body:has(#menu-toggle:checked) #mySidenav {
  width: 100% !important;
}

Other Examples

This collection shows CSS sidebar menus from across the web. Included are links with examples and code for use on your website.

Fixed Nav Hover Effect

Fixed Nav Hover Effect

About Project

Author

  • Terence Devine
  • codepen.io
Double Explorer Sidebar Nav

Double Explorer Sidebar Nav

About Project

Author

  • Jon Ambas
  • codepen.io
Sidebar Menu Hover Show Hide CSS

Sidebar Menu Hover Show Hide CSS

About Project

Author

  • JFarrow
  • codepen.io
Pure CSS3 Mega Dropdown Menu With Animation (Vertical)

Pure CSS3 Mega Dropdown Menu With Animation (Vertical)

About Project

Author

  • Rizky Kurniawan Ritonga
  • codepen.io
3D Rotating Navigation

3D Rotating Navigation

About Project

Author

  • Arjan Jassal
  • codepen.io
Side Sliding Menu CSS

Side Sliding Menu CSS

About Project

Author

  • Eduard L.
  • codepen.io
Fully Responsive CSS3 Menu

Fully Responsive CSS3 Menu

About Project

Author

  • Claudio Holanda
  • codepen.io
Fixed Hover Navigation

Fixed Hover Navigation

About Project

Author

  • Vince Brown
  • codepen.io
Pure CSS Fly In Sidebar Nav

Pure CSS Fly In Sidebar Nav

About Project

Author

  • Stephen Scaff
  • codepen.io
Simple Purple Sidebar Menu

Simple Purple Sidebar Menu

About Project

Author

  • Shawn Reisner
  • codepen.io
Navbar and Sidebar

Navbar and Sidebar

About Project

Author

  • Milica
  • codepen.io
Front End Day 94 CSS Only Mirror Like Nav

Front End Day 94 CSS Only Mirror Like Nav

About Project

Author

  • sean_codes
  • codepen.io
Reverse Text Color Menu Effects

Reverse Text Color Menu Effects

About Project

Author

  • Comehope
  • codepen.io
Pure CSS Single Page Application

Pure CSS Single Page Application

About Project

Author

  • Cassandra Rossall
  • codepen.io
Menu Hover Slide Fill

Menu Hover Slide Fill

About Project

Author

  • alphardex
  • codepen.io
CodePen Home App Admin Menus + Light_Dark Modes - With Tailwind CSSl

CodePen Home App Admin Menus + Light_Dark Modes – With Tailwind CSSl

About Project

Author

  • Rob Stinson
  • codepen.io
Vue Sidebar Menu

Vue Sidebar Menu

About Project

Author

  • MuthuKumar
  • codepen.io
Custom Bootstrap4 Sidebar Menu

Custom Bootstrap4 Sidebar Menu

About Project

Author

  • Prince Sargbah
  • codepen.io
Sidebar Menu With Icons

Sidebar Menu With Icons

About Project

Author

  • ivan
  • codepen.io
Arrowed Sidebar Menu

Arrowed Sidebar Menu

About Project

Author

  • Pierre Baron
  • codepen.io
Simple Accordion Menu

Simple Accordion Menu

About Project

Author

  • Corey Roth
  • codepen.io
Sidebar AdminLTE

Sidebar AdminLTE

About Project

Author

  • Jasp402
  • codepen.io
Sticky Sidebar Menu

Sticky Sidebar Menu

About Project

Author

  • Jake’s Tuts
  • codepen.io
Sidebar Pastel

Sidebar Pastel

About Project

Author

  • Yuhomyan
  • codepen.io
Nexus Sidebar

Nexus Sidebar

About Project

Author

  • illnino
  • codepen.io
Semantic-UI Sidebar

Semantic-UI Sidebar

About Project

Author

  • Ravi Shekhar
  • codepen.io
Modern Sidebar Menu

Modern Sidebar Menu

About Project

Author

  • FlorinCornea
  • codepen.io
Simple Bootstrap SideBar

Simple Bootstrap SideBar

About Project

Author

  • goodywebs
  • codepen.io
Hidden Sidebar Menu

Hidden Sidebar Menu

About Project

Author

  • Michael Ambrozaitis
  • codepen.io
Triangular Sidebar Menu

Triangular Sidebar Menu

About Project

Author

  • Nikolay Talanov
  • codepen.io
Material Design – Sidebar (Profile Menu)

Material Design – Sidebar (Profile Menu)

About Project

Author

  • Sergey Kupletsky
  • codepen.io

Recommended Articles

Other Articles