Simple Toggle Menu using HTML CSS and JavaScript

In this tutorial we will see how to create a Simple Toggle Menu using HTML CSS and JavaScript. JavaScript is used to display the menu and hide it while CSS is used to design it.

HTML Code

Take a look at the HTML code given below.

    <ul class="menu">
     <span id="icon" onclick="menu(this)">+</span>
     <div id="list">
        <li><a href='#'>Home</a></li>
        <li><a href='#'>Contact</a></li>
        <li><a href='#'>About</a></li>
     </div>    
    </ul>   

CSS Code

CSS code is given below.

<style>
body{
    font-family: sans-serif;
    background-color: #5fc7ea;
}
.menu{
      list-style: none;
      color: #fff;
      width: 150px;
      text-align: center;
}
#icon{
      cursor: pointer;
      display: block;
      background-color: #2B3033;
      width: 50px;
      padding: 8px 0px;
      font-size: 30px;
      margin: auto;
      border-radius: 25px;
      transition: all 0.3s;    
}
#list{
      background-color: #2B3033;
      padding: 25px 0px;
      border-radius: 14px;
      margin-top: 20px;
      transition: all 0.3s;
      transform: scale(0);
}
#list li{
      padding: 15px 0px;
}
#list li a{
      text-decoration: none;
      color: #fff;
      font-size: 14px;
}
.animate{
      transform: scale(1) !important;
}
</style>           

JavaScript Code

JavaScript code is given below.

<script>
var x=0;
var element = document.getElementById("list");    
function menu(icon){
if(x==0)
{
icon.style.transform = "rotate(45deg)";     
element.classList.add("animate");
x=1;
}
else{
icon.style.transform = "rotate(0deg)";    
element.classList.remove("animate");
x=0;    
}    
} 
</script>           

Video Tutorial

Watch our video tutorial on Simple Toggle Menu using HTML CSS and JavaScript.

Social Media Menu with Popup Tooltip using HTML and CSS Horizontal Navigation Menu with HTML and CSS Responsive Sidebar Menu with CSS and JavaScript Cool CSS Button with Hover Animation Pure CSS Custom Radio Buttons Custom Emoji Radio Buttons with CSS Color Picker Design with HTML CSS and JavaScript Custom Radio Buttons with CSS Engraved HTML Buttons With CSS Neumorphic Buttons Design with CSS Cool Profile Card Design with HTML CSS Neumorphic Search Bar Design with HTML and CSS Simple Login Form with Placeholder Animation Pure CSS Button with Folded Corner Background Color Change Slider with HTML CSS and JavaScript 404 Page Not Found Template Design with HTML and CSS Movies Blog Card Design with HTML and CSS Simple CSS Border Animation with before and after Selector Custom Social Select Dropdown Menu with CSS and JavaScript Facebook Offline and Online Notification with CSS and JS New Year 2022 Text Animation with CSS and JS Change Placeholder Color using CSS Custom Select Tag DropDown with CSS and JavaScript Simple Price Range Slider With HTML CSS and JavaScript Profile Card With Hover Effect using HTML and CSS Circle With Two Colors using CSS Simple FAQ Accordion With HTML CSS and JavaScript