Up to 70% off on hosting for WordPress Websites $2.95 /mo

Csshint recommends hosting
css html

Hamburger menu sidebar

How to create Hamburger menu sidebar using HTML CSS And JS. Designed by Profesor08 a codepen user.

HTML

<div class="nav-container" tabindex="0">
  <div class="nav-toggle"></div>

  <nav class="nav-items">
    <a class="nav-item" href="#">Home</a>
    <a class="nav-item" href="#">About US</a>
    <a class="nav-item" href="#">Product</a>
    <a class="nav-item" href="#">Contact US</a>
  </nav>
</div>

CSS (SCSS)

$toggleSize: 40px;
$toggleMargin: 10px;
$toggleLine: 4px;
$toggleColor: red;

.nav-container {
  position: relative;
  display: inline-block;
  max-width: $toggleSize + $toggleMargin;
  max-height: $toggleSize + $toggleMargin;
  overflow: visible;
  outline: none;
  
  //&:focus-within, &:focus {
  &.is-active {
    .nav-toggle {
      &:before, &:after {
        box-shadow: none;
      }
      
      &:before {
        transform: rotate(-45deg);
      }
      
      &:after {
        transform: rotate(45deg);
      }
    }
    
    .nav-items {
      transform: translate(0, 0);
    }
  }
  
  .nav-toggle {
    $offset: $toggleSize * 0.5;
    
    position: relative;
    width: $toggleSize;
    height: $toggleSize;
    margin: $toggleMargin;
    z-index: 2;
    
    &:hover {
      cursor: pointer;
    }
      
    &:before, &:after {
      content: "";
      position: absolute;
      top: #{$offset - $toggleLine / 2};
      left: 0;
      transform: translate(0, 0);
      width: 100%;
      height: $toggleLine;
      background: $toggleColor;
      transition: transform .3s ease, box-shadow .3s ease;
    }
    
    &:before {
      box-shadow: 0 #{$offset / 1.5} 0 0 $toggleColor;
    }
    
    &:after {
      box-shadow: 0 #{-$offset / 1.5} 0 0 $toggleColor;
    }
  }
  
  .nav-items {
    position: absolute;
    top: 0;
    left: 0;
    min-width: 300px;
    max-width: 50vw;
    width: 100vw;
    height: 100vh;
    z-index: 1;
    padding: 80px 20px 20px 10px;
    transition: transform .3s ease;
    transform: translate(calc(-100% - 50px), 0);
    background: #EFEFEF;
    display: grid;
    grid-template-columns: 1fr;
    grid-gap: 5px 0;
    align-content: start;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.3);
    
    .nav-item {
      background: darken(#EFEFEF, 5%);
      padding: 10px;
      transition: background-color .3s ease;
      
      &:hover {
        cursor: pointer;
        background: darken(#EFEFEF, 10%);
      }
    }
  }
}

JS

const nav = document.querySelector(".nav-container");

if (nav) {
  const toggle = nav.querySelector(".nav-toggle");
  
  if (toggle) {
    toggle.addEventListener("click", () => {
      if (nav.classList.contains("is-active")) {
        nav.classList.remove("is-active");
      }
      else {
        nav.classList.add("is-active");
      }
    });
    
    nav.addEventListener("blur", () => {
      nav.classList.remove("is-active");
    });
  }
}

 

Output

Hamburger menu sidebar

Hamburger menu sidebar