Very Simple Hidden Side Menu In HTML CSS (Free Download)

Welcome to a quick tutorial on how to create a simple hidden side menu in HTML and CSS. I am sure there are a ton of “sidebar plugins” on the Internet, but they are bloated and hard to customize. So let us do something different in this guide – Only the raw basics, so you can customize however you want. Read on!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TLDR – QUICK SLIDES

Fullscreen Mode – Click Here

 

TABLE OF CONTENTS

 

DOWNLOAD & DEMO

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

HIDDEN SIDEBAR DEMO

 

 

HIDDEN SIDEBAR MENU

All right, let us now get into the details of how to create a hidden side menu.

 

PART 1) THE HTML

sidebar.html
<!-- (A) SIDEBAR -->
<nav id="page-side">
  <a href="#">First</a>
  <a href="#">Second</a>
  <a href="#">Third</a>
</nav>

<!-- (B) MAIN -->
<main id="page-main">
  <!-- (B1) TOGGLE SIDEBAR -->
  <button id="side-button" onclick="sidebar()">
    &#9776;
  </button>
 
  <!-- (B2) CONTENTS -->
  <p>Contents</p>
</main>

The HTML is straightforward as can be:

  1. <nav id="page-side"> Captain Obvious, the sidebar. Just sandwich all your <a> menu items inside.
  2. <main id="page-main"> Self-explanatory, the main contents area. Just take note that we are using <button id="side-button"> to toggle the sidebar.

 

 

PART 2) THE JAVASCRIPT

sidebar.js
function sidebar () {
  // (A) GET SIDEBAR + BUTTON
  let side = document.getElementById("page-side"),
      btn = document.getElementById("side-button");
 
   // (B) TOGGLE SHOW/HIDE
  if (side.classList.contains("show")) {
    side.classList.remove("show");
    btn.innerHTML = "&#9776;";
  } else {
    side.classList.add("show");
    btn.innerHTML = "X";
  }
}

Unfortunately, we still need a little bit of Javascript to drive the sidebar. Not to worry, we are just doing 2 things here:

  • Toggling a .show CSS class on <nav id="page-side">.
  • Changing the icon of the toggle button <button id="side-button">.

 

PART 3) THE ESSENTIAL CSS

sidebar.css
/* (A) MAIN MECHANICS */
/* (A1) FLEX LAYOUT BODY */
body {
  display: flex;
  min-height: 100vh;
  padding: 0; margin: 0;
}
 
/* (A2) TOGGLE SIDEBAR SHOW/HIDE */
#page-side {
  width: 0;
  visibility: hidden;
  overflow: hidden;
  transition: width 0.4s;
}
#page-side.show {
  padding: 20px 10px;
  width: 150px;
  visibility: visible;
}
 
/* (A3) FLEX GROW MAIN CONTENTS */
#page-main {
  flex-grow: 1;
  padding: 15px;
}

No need to panic, there are only a few “core CSS mechanisms” to drive the hidden side menu.

  • (A1) Layout of the entire page.
    • body { display: flex } enables the “left sidebar, right contents” magic layout.
    • min-height: 100vh is kind of optional, but it ensures that the sidebar covers the height of the screen; Prevents a funky “broken looking layout”.
  • (A2) Toggle show/hide for the sidebar.
    • By default, we use width: 0 and visibility: hidden to hide the sidebar.
    • Then, applying width: 150px and visibility: visible will turn it visible.
    • transition: width 0.4s automatically does the animation magic, no Javascript required.
  • (A3) By default, display: flex will layout all containers in a horizontal manner. By adding #page-main { flex-grow: 1 }, we are simply allowing the contents area to automatically fill up the remaining width of the screen.

 

 

PART 4) COSMETIC CSS

sidebar.css
/* (B) COSMETICS */
/* (B1) FONT + SIZING */
* {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}
 
/* (B2) MENU ITEMS */
#page-side { background: #242424; }
#page-side a {
  display: block;
  width: 100%;
  margin-bottom: 15px;
  color: #ababab;
  text-decoration: none;
  cursor: pointer;
}
#page-side a:hover { color: #fff; }
 
/* (B3) SIDEBAR TOGGLE BUTTON */
#side-button {
  font-weight: 700;
  border: 0;
  padding: 10px;
  color: #fff;
  background: #f00;
  cursor: pointer;
}

Finally, a whole bunch of cosmetics to make things look better.

 

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Simple Hidden Sidebar (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *