Creating a responsive mobile menu that appears only on screens smaller than 768px can be achieved using HTML, CSS, and a small amount of JavaScript. Below is a complete example that includes:
- HTML: Structure of the navigation menu.
- CSS: Styling and media queries to handle responsiveness.
- JavaScript: Toggle functionality for the mobile menu.
1. HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Your Website</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header class="header">
<div class="logo">MySite</div>
<button class="menu-toggle" id="mobile-menu" aria-label="Toggle navigation">
☰ <!-- Hamburger icon -->
</button>
<nav class="nav" id="nav-menu">
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
<!-- Main content of your website -->
<main>
<section id="home">
<h1>Welcome to MySite</h1>
<!-- Your content here -->
</section>
<!-- Add more sections as needed -->
</main>
<script src="script.js"></script>
</body>
</html>
2. CSS (styles.css)
/* Basic Reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Header Styling */
.header {
display: flex;
justify-content: space-between;
align-items: center;
background-color: #333;
padding: 10px 20px;
color: #fff;
position: relative;
}
.logo {
font-size: 1.5em;
}
/* Navigation Styling */
.nav ul {
list-style: none;
display: flex;
}
.nav ul li {
margin-left: 20px;
}
.nav ul li a {
color: #fff;
text-decoration: none;
font-size: 1em;
}
.nav ul li a:hover {
text-decoration: underline;
}
/* Menu Toggle Button (Hidden by default on larger screens) */
.menu-toggle {
display: none;
font-size: 2em;
background: none;
border: none;
color: #fff;
cursor: pointer;
}
/* Media Queries for Mobile */
@media (max-width: 768px) {
/* Hide the navigation links by default */
.nav {
display: none;
width: 100%;
background-color: #333;
position: absolute;
top: 60px; /* Height of the header */
left: 0;
}
.nav ul {
flex-direction: column;
padding: 10px 0;
}
.nav ul li {
margin: 10px 0;
text-align: center;
}
/* Show the menu toggle button */
.menu-toggle {
display: block;
}
/* Show the navigation when active */
.nav.active {
display: block;
}
}
3. JavaScript (script.js)
document.addEventListener('DOMContentLoaded', function() {
const menuToggle = document.getElementById('mobile-menu');
const navMenu = document.getElementById('nav-menu');
menuToggle.addEventListener('click', function() {
navMenu.classList.toggle('active');
});
// Optional: Close the menu when clicking outside
document.addEventListener('click', function(event) {
if (!menuToggle.contains(event.target) && !navMenu.contains(event.target)) {
navMenu.classList.remove('active');
}
});
// Optional: Handle window resize to ensure proper menu display
window.addEventListener('resize', function() {
if (window.innerWidth > 768) {
navMenu.classList.remove('active');
}
});
});
Explanation
-
HTML Structure:
- Header: Contains the site logo, the hamburger menu button, and the navigation links.
- Button (
.menu-toggle): Visible only on mobile devices (screen width < 768px). Clicking this toggles the visibility of the navigation menu.
- Navigation (
.nav): Contains the list of navigation links. It's hidden by default on mobile and shown when the .active class is added.
-
CSS Styling:
- Basic Styles: Sets up the header with a dark background and white text, and styles the navigation links.
- Media Queries: When the screen width is 768px or less:
- The navigation is hidden (
display: none).
- The hamburger menu button is displayed (
display: block).
- When the
.active class is added to .nav, it becomes visible (display: block), showing the links in a vertical list.
-
JavaScript Functionality:
- Toggle Menu: Adds or removes the
.active class on the navigation menu when the hamburger button is clicked.
- Click Outside to Close (Optional): Closes the menu if the user clicks outside the menu or the toggle button.
- Responsive Behavior (Optional): Ensures that if the window is resized to a width greater than 768px, the mobile menu is closed.
Customization
- Links: Replace the
href attributes in the <a> tags with the actual URLs of your pages.
- Styling: Adjust colors, fonts, spacing, and other styles in the CSS to match your site's design.
- Accessibility: The hamburger button includes an
aria-label for better accessibility. You can enhance this further by managing focus states and using aria-expanded attributes to indicate menu state.
Preview
Here's a simple preview of how it works:
Final Notes
This example provides a foundational mobile menu that you can expand upon. Depending on your needs, you might want to add animations, improve accessibility features, or integrate it with a framework or library. Always test the menu across different devices and browsers to ensure compatibility and a smooth user experience.