Back To Tutorials

Responsive Navigation Menu

Intermediatechallenge

Create a responsive navigation menu that transforms into a hamburger menu on mobile devices.

Objective

Build a horizontal navigation menu on the desktop and collapse it into a hamburger menu on mobile devices.

Requirements

  1. Create a horizontal menu with at least 5 items for desktop view.
  2. Implement a hamburger icon that appears on screens smaller than 768px.
  3. When the hamburger icon is clicked, the menu should appear as a vertical list.
  4. Use CSS transitions for smooth open/close animations.

Steps

  1. Create the HTML structure for the navigation menu.
  2. Style the menu for desktop view using CSS.
  3. Add media queries for mobile devices.
  4. Create a hamburger icon using CSS.
  5. Use JavaScript to toggle the mobile menu.

Bonus

  • Add a dropdown submenu for one of the menu items.
  • Implement an overlay that appears behind the mobile menu when it's open.

Resources

Solution

Here's a step-by-step solution to create a responsive navigation menu:

  1. HTML Structure:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Responsive Navigation Menu</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <nav class="navbar">
        <div class="brand">Logo</div>
        <div class="hamburger">
            <div class="line"></div>
            <div class="line"></div>
            <div class="line"></div>
        </div>
        <ul class="nav-links">
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Portfolio</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
    </nav>
    <script src="script.js"></script>
</body>
</html>
  1. CSS Styling (styles.css):
body {
    margin: 0;
    font-family: Arial, sans-serif;
}

.navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #333;
    padding: 10px 20px;
}

.brand {
    color: white;
    font-size: 1.5rem;
}

.nav-links {
    display: flex;
    list-style: none;
    margin: 0;
    padding: 0;
}

.nav-links li {
    margin-left: 20px;
}

.nav-links a {
    color: white;
    text-decoration: none;
}

.hamburger {
    display: none;
    flex-direction: column;
    cursor: pointer;
}

.hamburger .line {
    width: 25px;
    height: 3px;
    background-color: white;
    margin: 3px 0;
}

@media screen and (max-width: 768px) {
    .nav-links {
        display: none;
        flex-direction: column;
        width: 100%;
        position: absolute;
        top: 60px;
        left: 0;
        background-color: #333;
        padding: 20px 0;
    }

    .nav-links.active {
        display: flex;
    }

    .nav-links li {
        margin: 10px 20px;
    }

    .hamburger {
        display: flex;
    }
}
  1. JavaScript for toggle functionality (script.js):
document.addEventListener('DOMContentLoaded', function() {
    const hamburger = document.querySelector('.hamburger');
    const navLinks = document.querySelector('.nav-links');

    hamburger.addEventListener('click', function() {
        navLinks.classList.toggle('active');
    });
});

This solution creates a responsive navigation menu that displays horizontally on desktop and collapses into a hamburger menu on mobile devices (screens smaller than 768px). The menu items are hidden on mobile until the hamburger icon is clicked, at this point, they appear as a vertical list.

To add the bonus features:

  1. For a dropdown submenu, you would add nested <ul> elements within the desired <li> in the HTML, and add CSS to handle the dropdown behavior.

  2. For an overlay, you would add a new <div> element just inside the <body> tag and use CSS to style it as a full-screen, semi-transparent overlay that appears when the mobile menu is