Designing a Responsive Navbar
The user wants a responsive navigation bar that turns into a hamburger menu on smaller screens, using HTML, CSS, and JavaScript. I need to provide sample code with explanations, focusing on layout, flexbox, and media queries.
I'll give concise code snippets with comments and ensure it includes accessibility features. This will involve toggling classes for open and close states in JavaScript, as well as using media queries for responsiveness. I'll also prepare a working example to show the final implementation.Creating a Responsive Navigation Bar
I’m laying out a responsive navigation bar that becomes a hamburger menu on smaller screens. In the CSS, I'm using flexbox for alignment and adjusting display properties based on screen size. The .nav-links will be hidden initially on small screens and shown when the .open class is applied. In the JavaScript, I'm toggling the .open class on click, along with updating the aria-expanded attribute for accessibility. I'll keep everything concise while ensuring it's functional. Here’s a simple responsive navigation bar that turns into a hamburger menu on smaller screens.
Full Example (HTML + CSS + JS)
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Responsive Navbar</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<header class="navbar">
<a href="#" class="logo">MySite</a>
<!-- Hamburger button -->
<button
class="hamburger"
aria-label="Toggle navigation"
aria-expanded="false"
aria-controls="nav-menu"
>
<span></span>
<span></span>
<span></span>
</button>
<!-- Navigation links -->
<nav id="nav-menu" class="nav-links">
<a href="#home">Home</a>
<a href="#features">Features</a>
<a href="#pricing">Pricing</a>
<a href="#about">About</a>
</nav>
</header>
<main>
<section id="home"><h1>Home</h1></section>
<section id="features"><h1>Features</h1></section>
<section id="pricing"><h1>Pricing</h1></section>
<section id="about"><h1>About</h1></section>
</main>
<script src="script.js"></script>
</body>
</html>
styles.css
*,
*::before,
*::after {
box-sizing: border-box;
}
body {
margin: 0;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
/* NAVBAR BASE STYLES */
.navbar {
position: sticky;
top: 0;
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.75rem 1.5rem;
background: #111827;
color: #f9fafb;
}
.logo {
color: inherit;
font-weight: 700;
font-size: 1.25rem;
text-decoration: none;
}
/* Navigation links (desktop) */
.nav-links {
display: flex;
gap: 1.25rem;
}
.nav-links a {
color: #e5e7eb;
text-decoration: none;
padding: 0.25rem 0;
position: relative;
font-size: 0.95rem;
}
.nav-links a::after {
content: "";
position: absolute;
left: 0;
bottom: -0.2rem;
width: 0;
height: 2px;
background: #3b82f6;
transition: width 0.2s ease;
}
.nav-links a:hover::after {
width: 100%;
}
/* Hamburger button (hidden on large screens) */
.hamburger {
display: none;
flex-direction: column;
justify-content: center;
gap: 0.35rem;
width: 2rem;
height: 2rem;
padding: 0;
border: none;
background: transparent;
cursor: pointer;
}
.hamburger span {
display: block;
height: 2px;
width: 100%;
background: #e5e7eb;
border-radius: 999px;
transition: transform 0.2s ease, opacity 0.2s ease, top 0.2s ease;
}
/* Mobile styles / breakpoint */
@media (max-width: 768px) {
/* Show hamburger, hide links by default */
.hamburger {
display: flex;
}
.nav-links {
position: absolute;
top: 100%;
left: 0;
right: 0;
display: none;
flex-direction: column;
background: #111827;
padding: 0.75rem 1.5rem 1rem;
border-top: 1px solid #1f2937;
}
.nav-links a {
padding: 0.5rem 0;
}
/* When menu is open */
.nav-links.open {
display: flex;
}
/* Animate hamburger to "X" when open */
.hamburger.open span:nth-child(1) {
transform: translateY(6px) rotate(45deg);
}
.hamburger.open span:nth-child(2) {
opacity: 0;
}
.hamburger.open span:nth-child(3) {
transform: translateY(-6px) rotate(-45deg);
}
}
script.js
const hamburger = document.querySelector(".hamburger");
const navMenu = document.querySelector(".nav-links");
hamburger.addEventListener("click", () => {
const isOpen = navMenu.classList.toggle("open");
hamburger.classList.toggle("open", isOpen);
hamburger.setAttribute("aria-expanded", String(isOpen));
});
// Optional: close menu when a link is clicked (on mobile)
navMenu.addEventListener("click", (e) => {
if (e.target.tagName === "A" && navMenu.classList.contains("open")) {
navMenu.classList.remove("open");
hamburger.classList.remove("open");
hamburger.setAttribute("aria-expanded", "false");
}
});
Copy these three files into a folder and open index.html in a browser. Resize the window below 768px width to see the navigation bar turn into a hamburger menu.