Overlay Hamburger Menu Code with Example

Overlay Hamburger Menu Code with Example
Code Snippet:overlay menu - vanilla js
Author: Job Gathu
Published: January 12, 2024
Last Updated: January 22, 2024
Downloads: 624
License: MIT
Edit Code online: View on CodePen
Read More

This code example helps you to create a stylish overlay menu with a hamburger button. The openMenu function toggles the menu’s visibility, triggered by clicking the hamburger icon. The menu appears with smooth animations, providing an elegant user experience.

You can implement this code to enhance your website’s navigation. Moreover, you can customize the menu using additional CSS according to your website’s theme.

How to Create an Overlay Hamburger Menu

1. First of all, load the Font Awesome CSS (for icons) by adding the following CDN link into the head tag of your HTML document.

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

2. Create a basic HTML structure with a container for the menu and the main content. Insert a button with the class btn-menu to trigger the menu.

<div class="flexcontainer">
  <div class="left"></div>
  <div class="right">
      <h1>Overlay Menu - Vanilla Js</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.nulla pariatur.</p>

    <div class="toggle-menu">
      <a class="btn-menu"><i class="fa fa-bars"></i></a>

<div class="overlay">
    <li><a>About Us</a></li>
    <li><a>Tour Packages</a></li>
    <li><a>Explore Kenya</a></li>
    <li><a>Contact Us</a></li>

3. The CSS code provides the styling for the menu, ensuring a visually appealing overlay effect. Customize colors and dimensions according to your website’s design.

body {
	  background: #eee;

	.flexcontainer {
	   display: -webkit-flex;
	   display: flex;
	   -webkit-flex-direction: row;
	   flex-direction: row;
	   -webkit-align-items: flex-start;
	   align-items: flex-start;
	  background-color: #eee;

	.left {
	    background: #0cebeb; /* fallback for old browsers */
  background: -webkit-linear-gradient(to right, #0cebeb, #20e3b2, #29ffc6); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to right, #0cebeb, #20e3b2, #29ffc6);
	  background-size: cover !important;
	  height: 100vh;
	  width: 55%;
	  box-sizing: border-box;
	  animation: motion 6s linear infinite;
	  -webkit-animation: motion 6s linear infinite;

	.right {
	  width: 45%;
	  box-sizing: border-box;
	  margin: auto;

	.right div {
	  width: 500px;
	  margin: auto;
	  padding: 20px;

	.right div h1 {
	  font-size: 3.7rem;
	  font-family: 'Dancing Script', sans-serif;

	.right div p {
	  font-family: 'Montserrat', sans-serif;
	  font-weight: 200;
	  line-height: 25px;

	.btn-menu {
         width: 60px;
         height: 60px;
        box-sizing: border-box;
	  position: absolute;
	  background: #0cebeb;
	  padding: 20px;
	  top: 30px;
	  right: 50px;
	  border-radius: 50%;
	  z-index: 9999;
         font-size: 26px;
          text-align: center;
.btn-menu i{
   vertical-align: text-top;
  .btn-menu:active {
	  background: #29ffc6;
	  cursor: pointer;
    transition: color .5s ease;

	.overlay {
	  background-color: rgba(0,0,0,1);
	  color: #fff;
	  height: 100%;
	  width: 100%;
	  position: fixed;
	  text-align: center;
	  transition: opacity 0.5s ease-in-out;
	  opacity: 0;

	 .open {
	  	position: fixed;
        top: 0;
        right: 0;
        left: 0;
        bottom: 0;
        max-height: 1200px;
        opacity: 1;
        z-index: 100;
        transition: all 0.3s ease;

	.overlay ul {
		list-style-type: none;
    	margin-top: 2em;
    	font-size: 3rem;
	.overlay ul li a{
		position: relative;
    display: inline-block;
    font-size: 35px;
    font-weight: 300;
    color: #fff;
    -webkit-transition: color 0.5s ease;
    transition: color 0.5s ease;
    padding: 3px 0;
    margin-bottom: 40px;

	.overlay ul li a:hover,
	.overlay ul li a:active {
		color: #787878;
		cursor: pointer;
		transition: all 0.3s linear;

.overlay ul li a:hover::after,
.overlay ul li a:hover::before {
  width: 100%;
  left: 0;

.overlay ul li a::after,
.overlay ul li a::before {
  content: "";
  position: absolute;
  width: 0;
  right: 0;
  height: 2px;
  top: calc(100% + 5px);

.overlay ul li a::before {
  -webkit-transition: width 0.4s cubic-bezier(0.51, 0.18, 0, 0.88) 0.1s;
    transition: width 0.4s cubic-bezier(0.51, 0.18, 0, 0.88) 0.1s;
    background: #2196f3;

.overlay ul li a::before {
 -webkit-transition: width 0.5s cubic-bezier(0.29, 0.18, 0.26, 0.83);
    transition: width 0.5s cubic-bezier(0.29, 0.18, 0.26, 0.83);
    background: #20e3b2;

4. Finally, add the following JavaScript code to your project. It defines a function openMenu that toggles the menu’s visibility. When the .btn-menu is clicked, it triggers the openMenu function, showcasing or hiding the overlay menu.

function openMenu() {

document.querySelector('.btn-menu').onclick = openMenu;

That’s all! hopefully, you have successfully integrated this code example into your project to create an overlay hamburger menu. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link