skip to Main Content

I’m working on a travel agency website and I’m trying to implement a responsive navigation menu using HTML, CSS, and JavaScript.

I want the menu to be fully functional on both desktop and mobile devices.

Markup:

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Destinations</a></li>
    <li><a href="#">Book Now</a></li>
  </ul>
</nav>

Question: how can I make this navigation menu responsive, so that it collapses into a hamburger menu on mobile devices and expands on desktop devices?

I’ve tried using CSS media queries, but I’m not sure how to implement the hamburger menu icon and toggle functionality. Any help or guidance would be appreciated!

3

Answers


  1. There are multiple ways to achieve this functionality. But I have tried to use HTML, CSS, and JavaScript as simple as I could.

    I also have added Fontawesome CDN to make it simple instead of using CSS properties to make a hamburger and times icon.

    I also added main body content to check if it works perfect. You can keep your current code and navbar style and adjust this code into it to make it perfectly functional.

    Here is the updated code:

    document.addEventListener("DOMContentLoaded", function () {
        const menuIcon = document.querySelector(".menu-icon");
        const hamburger = document.getElementById("hamburger");
        const cross = document.getElementById("cross");
        const navLinks = document.getElementById("nav-links");
    
        menuIcon.addEventListener("click", function () {
            hamburger.style.display =
            hamburger.style.display === "none" ? "block" : "none";
            cross.style.display =
            cross.style.display === "none" ? "block" : "none";
    
            navLinks.classList.toggle("show");
        });
    });
    body {
    margin: 0;
    padding: 0;
    }
    
    main {
    padding: 8px;
    }
    
    nav {
    background-color: #333;
    color: #fff;
    padding: 15px;
    }
    
    nav ul {
    list-style: none;
    display: flex;
    justify-content: center;
    }
    
    nav ul li {
    margin: 0 15px;
    }
    
    nav ul li a {
    color: #fff;
    text-decoration: none;
    font-size: 18px;
    }
    
    .menu-icon {
    display: none;
    cursor: pointer;
    position: relative;
    font-size: 24px;
    }
    
    @media (max-width: 768px) {
    nav ul {
        display: none;
        flex-direction: column;
        width: 100%;
        text-align: center;
        position: absolute;
        top: 52px;
        left: 0;
        background-color: #333;
        margin: 0;
        padding: 0;
    }
    
    nav ul.show {
        display: flex;
    }
    
    nav ul li {
        margin: 15px 0;
    }
    
    .menu-icon {
        display: block;
    }
    }
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Responsive Navbar</title>
    
        <link
          rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css"
          integrity="sha512-<hash>"
          crossorigin="anonymous"
          referrerpolicy="no-referrer"
        />
    <header>
    </head>
      <body>
        <nav>
        <div class="menu-icon">
            <i class="fas fa-bars" id="hamburger"></i>
            <i class="fas fa-times" id="cross" style="display: none"></i>
        </div>
    
        <ul id="nav-links">
            <li><a href="#">Home</a></li>
            <li><a href="#">Destinations</a></li>
            <li><a href="#">Book Now</a></li>
        </ul>
        </nav>
    </header>
    
    <main>
        <h1>Main content</h1>
        <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Repellendus
        temporibus porro pariatur eum molestias, iure nisi assumenda quidem
        harum vitae, odio unde! Molestiae consequatur omnis rem saepe laboriosam
        iusto corporis.
        </p>
    
        <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Repellendus
        temporibus porro pariatur eum molestias, iure nisi assumenda quidem
        harum vitae, odio unde! Molestiae consequatur omnis rem saepe laboriosam
        iusto corporis.
        </p>
    
        <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Repellendus
        temporibus porro pariatur eum molestias, iure nisi assumenda quidem
        harum vitae, odio unde! Molestiae consequatur omnis rem saepe laboriosam
        iusto corporis.
        </p>
    
        <p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Repellendus
        temporibus porro pariatur eum molestias, iure nisi assumenda quidem
        harum vitae, odio unde! Molestiae consequatur omnis rem saepe laboriosam
        iusto corporis.
        </p>
    </main>
     </body>
    </html>
    Login or Signup to reply.
  2. Similar to your expectations

    I came across this hover animation, hope if it could help !!

    <div id="menu">
        <div class="hamburger">
            <div class="line"></div>
            <div class="line"></div>
            <div class="line"></div>
        </div>
        <div class="menu-inner">
            
            <ul>
                <li>Menu Item</li>
                <li>Menu Item</li>
                <li>Menu Item</li>
                <li>Menu Item</li>
                <li>Menu Item</li>
                <li>Menu Item</li>
            </ul>
        </div>
    
    
        
        <svg version="1.1" id="blob"xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
            <path id="blob-path" d="M60,500H0V0h60c0,0,20,172,20,250S60,900,60,500z"/>
        </svg>
    </div>
    

    The CSS file

            body, html {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            background-color:#26394E ;
        }
    
        #menu {
            height: 100%;
            position: fixed;
            background-color: #FED057;
            width: 300px;
            transition: 1000ms all cubic-bezier(0.19, 1, 0.22, 1);
            transform: translateX(-100%);
            left: 60px;
        }
    
        #menu.expanded {
            transform: translateX(0%);
            left: 0px;
        }
    
        .menu-inner {
            width: 100%;
            height: 100%;
            position: relative;
        }
    
        #blob {
            top: 0;
            z-index: -1;
            right: 60px;
            transform: translateX(100%);
            height: 100%;
            position: absolute;
        }
    
        #blob-path {
            height: 100%;
            fill:  #FED057;
        }
    
        .hamburger {
            right: 20px;
            position: absolute;
            width: 20px;
            height: 20px;
            margin-top: -10px;  
        }
    
        .hamburger .line {
            width: 100%;
            height: 4px;
            background-color: #fff;
            position: absolute;
        }
    
        .hamburger .line:nth-child(2) {
            top: 50%;
            margin-top: -2px;
        }
    
        .hamburger .line:nth-child(3) {
            bottom: 0;
        }
    
        h1 {
            position: fixed;
            right: 0;
            margin: 0;
        }
    
        ul {
            padding: 0;
            list-style: none;
            width: 80%;
            margin-left: 10%;
            position: absolute;
            top: 10px;
        }
    
        ul li {
            color: #fff;
            font-family: sans-serif;
            padding: 20px 0;
        }
    
        h2 {
            position: absolute;
           left: 50%;
      color: #fff;
            margin: 0;
      font-size: 16px;
      font-family: sans-serif;
      font-weight: 100;
        }
    

    The JS File

    $(window).load(function(){
    var height = window.innerHeight, x= 0, y= height/2,
    curveX = 10,
    curveY = 0,
    targetX = 0,
    xitteration = 0,
    yitteration = 0,
    menuExpanded = false;
    
    blob = $('#blob'),
    blobPath = $('#blob-path'),
    
    hamburger = $('.hamburger');
    
    $(this).on('mousemove', function(e){
        x = e.pageX;
        
        y = e.pageY;
    });
    
    $('.hamburger, .menu-inner').on('mouseenter', function(){
        $(this).parent().addClass('expanded');
        menuExpanded = true;
    });
    
    $('.menu-inner').on('mouseleave', function(){
        menuExpanded = false;
        $(this).parent().removeClass('expanded');
    });
    
    function easeOutExpo(currentIteration, startValue, changeInValue, totalIterations) {
        return changeInValue * (-Math.pow(2, -10 * currentIteration / totalIterations) + 1) + startValue;
    }
    
    var hoverZone = 150;
    var expandAmount = 20;
    
    function svgCurve() {
        if ((curveX > x-1) && (curveX < x+1)) {
            xitteration = 0;
        } else {
            if (menuExpanded) {
                targetX = 0;
            } else {
                xitteration = 0;
                if (x > hoverZone) {
                    targetX = 0;
                } else {
                    targetX = -(((60+expandAmount)/100)*(x-hoverZone));
                }           
            }
            xitteration++;
        }
    
        if ((curveY > y-1) && (curveY < y+1)) {
            yitteration = 0;
        } else {
            yitteration = 0;
            yitteration++;  
        }
    
        curveX = easeOutExpo(xitteration, curveX, targetX-curveX, 100);
        curveY = easeOutExpo(yitteration, curveY, y-curveY, 100);
    
        var anchorDistance = 200;
        var curviness = anchorDistance - 40;
    
        var newCurve2 = "M60,"+height+"H0V0h60v"+(curveY-anchorDistance)+"c0,"+curviness+","+curveX+","+curviness+","+curveX+","+anchorDistance+"S60,"+(curveY)+",60,"+(curveY+(anchorDistance*2))+"V"+height+"z";
    
        blobPath.attr('d', newCurve2);
    
        blob.width(curveX+60);
    
        hamburger.css('transform', 'translate('+curveX+'px, '+curveY+'px)');
    
    $('h2').css('transform', 'translateY('+curveY+'px)');
        window.requestAnimationFrame(svgCurve);
    }
    
    window.requestAnimationFrame(svgCurve);});
    
    Login or Signup to reply.
  3. First, answer your question about the hamburger menu. You can use a combination of HTML, CSS, and JavaScript or jQuery to make a navigation menu responsive. It will expand on desktop devices and become a hamburger menu on mobile devices.

    Below is a basic example of how to use the hamburger menu.

    $(".hamburger ").on("click", function() {
        $(this).toggleClass('active');
        $(".main-menu").toggleClass('active');
    });
    body {
        margin: 0;
    }
    
    header {
        display: flex;
        align-items: center;
        padding: 20px 40px;
        background: lightgray;
        font-size: 18px;
    }
    
    .hamburger {
        display: none;
    }
    
    .main-menu ul {
        display: flex;
        column-gap: 30px;
        padding-left: 0;
        list-style: none;
        margin: 0;
    }
    
    .main-menu ul li a {
        color: #000000;
        text-decoration: none;
    }
    
    @media only screen and (max-width: 768px) {
        .hamburger {
            display: block;
            line-height: 1;
            cursor: pointer;
        }
    
        .main-menu {
            display: none;
            position: absolute;
            top: 60px;
            width: 100%;
            left: 0;
            background: lightgray;
        }
    
        .main-menu ul {
            flex-direction: column;
            row-gap: 20px;
            padding: 20px 40px;
        }
    
        .hamburger .close {
            display: none;
        }
    
        .hamburger.active .bar {
            display: none;
        }
    
        .hamburger.active .close {
            display: block;
        }
    
        .main-menu.active {
            display: block;
            height: 100%;
        }
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" integrity="sha512-<hash>" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <header>
        <div class="hamburger">
            <i class="fa-solid fa-bars bar"></i>
            <i class="fa-solid fa-times close"></i>
        </div>
        <nav class="main-menu">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Destinations</a></li>
                <li><a href="#">Book Now</a></li>
            </ul>
        </nav>
    </header>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search