Responsive Mega Menu Using JavaScript

mega menu responsive design

Responsive Mega Menu Using JavaScript

In this article, You will get to know how to create a responsive mega menu using HTML, CSS and JavaScript.

Mega menus are a great way to increase navigation options on your website, and this article walks you through the process step-by-step.

By the end of this article, you'll be able to create a responsive mega menu using HTML, CSS and JavaScript!


Watch the Tutorial on Youtube
https://youtu.be/wgzwI-MW6zU



Live Preview.!



HTML Code

<nav>
  <div class="navbar">
      <div class="logo">
          <a href="#">Web<span>Tuttorials</span></a>
      </div>
      <div class="mega-menu">
          <div class="menu-bar"></div>
          <ul class="main-menu">
              <li><a href="#">Home</a></li>
              <li><a href="#">About</a></li>
              <li><a href="#">Products</a></li>
              <li><a href="#">Contact</a></li>
              <li class="mega-menu-dropdown">
                  <span>Mega Menu</span>
                  <ul class="mega-menu-list">
                      <li>
                          <img src="https://images.unsplash.com/photo-1599785209707-a456fc1337bb?w=300&q=60" />
                          <a href="#">Vanila Cupcake</a>
                      </li>
                      <li>
                          <img src="https://images.unsplash.com/photo-1603532648955-039310d9ed75?w=300&q=60" />
                          <a href="#">Chocolate Cupcake</a>
                      </li>
                      <li>
                          <img src="https://images.unsplash.com/photo-1620442513427-814e90d384e9?w=300&q=60" />
                          <a href="#">Strawberry Cupcake</a>
                      </li>
                      <li>
                          <img src="https://images.unsplash.com/photo-1587668178277-295251f900ce?w=300&q=60" />
                          <a href="#">Dark Chocolate Cupcake</a>
                      </li>
                  </ul>
              </li>
          </ul>
      </div>
  </div>
</nav>

CSS Code

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Roboto', sans-serif;
}
nav{
    width: 100%;
    height: 60px;
    background-color: #fff;
    border: 2px solid #EDF2F4;
}
.navbar{
    width: 100%;
    max-width: 1200px;
    height: 100%;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.navbar .logo a{
    color: #2B2D42;
    font-size: 24px;
    font-weight: 600;
    text-decoration: none;
}
.navbar .logo a span{
    color: #D90429;
}
.mega-menu ul{
    list-style: none;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.mega-menu ul li,
.mega-menu ul li a {
    color: #D90429;
    font-weight: 600;
    text-decoration: none;
    transition: 0.3s ease;
}
.mega-menu ul > li{
    padding: 24px 0;
}
.mega-menu ul > li a{
    padding: 0 15px;
}
.mega-menu ul li:hover,
.mega-menu ul li a:hover {
    color: #2B2D42;
}
.mega-menu li.mega-menu-dropdown span{
    cursor: pointer;
    position: relative;
}
.mega-menu li.mega-menu-dropdown span:after{
    content: '';
    position: absolute;
    top: 25%;
    right: -15px;
    border-top: 5px solid #D90429;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
}
.mega-menu li.mega-menu-dropdown:hover span:after{
    border-top-color: #2B2D42;
}
.mega-menu ul ul.mega-menu-list{
    width: 90%;
    height: auto;
    padding: 20px;
    background-color: #fff;
    border-bottom: 2px solid #D90429;
    position: absolute;
    top: 60px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 5;
    display: none;
    grid-template-columns: repeat(4, 1fr);
    column-gap: 20px;
    box-shadow: 0 3px 4px rgba(0,0,0,0.2);
}
.mega-menu ul ul.mega-menu-list li{
    padding: 0;
}
.mega-menu ul ul.mega-menu-list li img{
    width: 100%;
    height: 320px;
    object-fit: cover;
}
.mega-menu ul ul.mega-menu-list li a{
    display: block;
    margin-top: 15px;
    text-align: center;
}
.mega-menu li.mega-menu-dropdown:hover ul.mega-menu-list{
    display: grid;
}
@media screen and (max-width: 1024px) {
    .navbar{
        width: 100%;
        padding: 0 30px;
    }
    .mega-menu ul ul.mega-menu-list li img{
        height: 250px;
    }
    .mega-menu ul ul.mega-menu-list li a{
        font-size: 14px;
    }
}
@media screen and (max-width: 768px) {
    .navbar .logo a{
        font-size: 20px;
    }
    .mega-menu ul li a,
    .mega-menu ul li span{
        font-size: 14px;
    }
    .mega-menu ul ul.mega-menu-list{
        width: 97%;
        padding: 15;
        column-gap: 15px;
    }
    .mega-menu ul ul.mega-menu-list li img{
        height: 220px;
    }
    .mega-menu ul ul.mega-menu-list li a{
        font-size: 11px;
    }
}
@media screen and (max-width: 580px) {
    .mega-menu ul{
        display: none;
    }
    .mega-menu .menu-bar{
        width: 30px;
        height: 9px;
        background-color: #fff;
        position: relative;
        cursor: pointer;
    }
    .mega-menu .menu-bar:before{
        content: '';
        width: 100%;
        height: 3px;
        background-color: #D90429;
        position: absolute;
        top: 0;
        left: 0;
    }
    .mega-menu .menu-bar:after{
        content: '';
        width: 100%;
        height: 3px;
        background-color: #D90429;
        position: absolute;
        bottom: 0;
        left: 0;
    }
    .mega-menu ul.main-menu.open{
        width: 100%;
        padding: 20px;
        background-color: #fff;
        display: flex;
        flex-direction: column;
        position: absolute;
        top: 60px;
        left: 0;
    }
    .mega-menu ul.main-menu.open ul.mega-menu-list{
        width: 68%;
        grid-template-columns: 1fr;
        grid-row-gap: 20px;
    }
}

JavaScript Code

const menuBar = document.querySelector('.menu-bar');
const mainMenu = document.querySelector('.main-menu');
window.addEventListener('resize', function(){
    if(mainMenu.classList.contains('open')){
        mainMenu.classList.remove('open');
    }
})
menuBar.addEventListener('click', function(){
    if(mainMenu.classList.contains('open')){
        mainMenu.classList.remove('open');
    }else{
        mainMenu.classList.add('open');
    }
})

Post a Comment

0 Comments