user21213
4
Q:

hamburger menu html and css

/* Link to Codepen in source for PoC */

<style>
.container {
  background: dodgerblue;
  padding: 20px;
  height: 70px;
}

#hamburger {
  width: 40px;
  height: 40px;
  display: block;
  position: relative;
  float: right;
  transform: rotate(0deg);
  transition: .5s ease-in-out;
  cursor: pointer;
}
#hamburger span {
  display: block;
  position: absolute;
  height: 4px;
  width: 100%;
  background: white;
  border-radius: 9px;
  opacity: 1;
  left: 0;
  transform: rotate(0deg);
  transition: .25s ease-in-out;
}
#hamburger span:nth-child(1) {
  top: 0px;
}
#hamburger span:nth-child(2) {
  top: 12px;
}
#hamburger span:nth-child(3) {
  top: 24px;
}
#hamburger.open span:nth-child(1) {
  top: 14px;
  transform: rotate(135deg);
}
#hamburger.open span:nth-child(2) {
  opacity: 0;
  left: -60px;
}
#hamburger.open span:nth-child(3) {
  top: 14px;
  transform: rotate(-135deg);
}

</style>
<body>
  <div class="container">
       <div id="hamburger">
          <span></span>
          <span></span>
          <span></span>
      </div> 
  </div>
</body>
1

  /* 

	Step-2

Style the navigation menu */
.topnav {
  overflow: hidden;
  
  background-color: #333;
  position: relative;
}

/* Hide the 
  links inside the navigation menu (except for logo/home) */
.topnav #myLinks {
  
  display: none;
}

/* Style navigation menu links */
.topnav a {
  color: white;
  
  padding: 14px 16px;
  text-decoration: none;
  font-size: 
  17px;
  display: block;
}

/* Style the hamburger menu */

  .topnav a.icon {
  
  background: black;
  display: block;
  position: absolute;
  
  right: 0;
  top: 0;
}

/* Add a grey background color on 
  mouse-over */
.topnav a:hover {
  
  background-color: #ddd;
  color: black;
}

/* Style the 
  active link (or home/logo) */
.active {
  background-color: #4CAF50;
  
  color: white;
} 
1

  <!--
	Step-1

Load an icon library to show a hamburger menu (bars) on small screens -->

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

  
<!-- Top Navigation Menu -->
<div class="topnav">
  <a href="#home" 
  class="active">Logo</a>
  <!-- Navigation links (hidden by default) 
  -->
  <div id="myLinks">
    <a href="#news">News</a>
    
  <a href="#contact">Contact</a>
    <a href="#about">About</a>
  
  </div>
  <!-- "Hamburger menu" / "Bar icon" to toggle the navigation 
  links -->
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
    
  <i class="fa fa-bars"></i>
  </a>
</div> 
1

/* 
Step-3

Toggle between showing and hiding the navigation menu links when 
the user clicks on the hamburger menu / bar icon */
function myFunction() {
  var x = 
  document.getElementById("myLinks");
  if (x.style.display === "block") 
  {
    x.style.display = "none";
  } else {
    
  x.style.display = "block";
  }
} 
1

New to Communities?

Join the community