Responsive Navigation Menu
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
- Create a horizontal menu with at least 5 items for desktop view.
- Implement a hamburger icon that appears on screens smaller than 768px.
- When the hamburger icon is clicked, the menu should appear as a vertical list.
- Use CSS transitions for smooth open/close animations.
Steps
- Create the HTML structure for the navigation menu.
- Style the menu for desktop view using CSS.
- Add media queries for mobile devices.
- Create a hamburger icon using CSS.
- 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:
- 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>
- 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;
}
}
- 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:
-
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. -
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