html
<!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>
<style>
/* Reset and base styles */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
line-height: 1.6;
color: #333;
}
/* Skip link for accessibility */
.skip-link {
position: absolute;
top: -40px;
left: 6px;
background: #000;
color: #fff;
padding: 8px;
text-decoration: none;
z-index: 100;
transition: top 0.3s;
}
.skip-link:focus {
top: 6px;
}
/* Header and Nav */
header {
background: #2563eb;
color: white;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
nav {
max-width: 1200px;
margin: 0 auto;
padding: 0 1rem;
position: relative;
}
/* Hamburger toggle (hidden checkbox) */
.nav-toggle {
display: none;
}
.nav-toggle-label {
display: none;
position: absolute;
top: 1rem;
right: 1rem;
cursor: pointer;
padding: 0.5rem;
z-index: 101;
border: none;
background: none;
role: button;
tabindex: 0;
}
.nav-toggle-label span {
display: block;
width: 25px;
height: 3px;
background: white;
margin: 5px 0;
transition: 0.3s;
transform-origin: center;
}
/* Hamburger to X animation */
.nav-toggle:checked + .nav-toggle-label span:nth-child(1) {
transform: rotate(45deg) translate(5px, 5px);
}
.nav-toggle:checked + .nav-toggle-label span:nth-child(2) {
opacity: 0;
}
.nav-toggle:checked + .nav-toggle-label span:nth-child(3) {
transform: rotate(-45deg) translate(7px, -6px);
}
/* Menu styles */
.menu {
list-style: none;
display: flex;
margin: 0;
padding: 0;
}
.menu li {
margin: 0;
}
.menu a {
display: block;
padding: 1rem 1.5rem;
color: white;
text-decoration: none;
transition: background 0.3s;
}
.menu a:hover,
.menu a:focus {
background: rgba(255,255,255,0.1);
outline: none;
}
/* Desktop: horizontal menu */
@media (min-width: 769px) {
.nav-toggle-label {
display: none !important; /* Never show on desktop */
}
.menu {
flex-direction: row;
}
}
/* Mobile: vertical overlay menu */
@media (max-width: 768px) {
.nav-toggle-label {
display: block;
}
.menu {
display: none; /* Hidden by default */
flex-direction: column;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background: #1e40af;
z-index: 100;
padding-top: 5rem;
transform: translateX(-100%);
transition: transform 0.3s ease-in-out;
}
.nav-toggle:checked ~ .menu {
display: flex;
transform: translateX(0);
}
.menu li {
width: 100%;
}
.menu a {
padding: 1.5rem;
border-bottom: 1px solid rgba(255,255,255,0.1);
}
}
/* Main content */
main {
max-width: 1200px;
margin: 2rem auto;
padding: 0 1rem;
}
h1 {
font-size: 2.5rem;
margin-bottom: 1rem;
}
/* Footer */
footer {
background: #1f2937;
color: white;
text-align: center;
padding: 2rem 1rem;
margin-top: 4rem;
}
/* Screen reader only */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
</style>
</head>
<body>
<a href="#main-content" class="skip-link">Skip to main content</a>
<header>
<nav aria-label="Main navigation">
<!-- Hidden checkbox for toggle (CSS-only mechanism, enhanced with JS for ARIA) -->
<input type="checkbox" id="nav-toggle" class="nav-toggle sr-only">
<!-- Toggle label styled as button with ARIA -->
<label for="nav-toggle" class="nav-toggle-label" aria-expanded="false" role="button" tabindex="0" aria-controls="primary-menu">
<span></span>
<span></span>
<span></span>
<span class="sr-only">Toggle menu</span>
</label>
<!-- Navigation menu -->
<ul class="menu" id="primary-menu" role="menubar">
<li role="none"><a href="#home" role="menuitem" tabindex="-1">Home</a></li>
<li role="none"><a href="#about" role="menuitem" tabindex="-1">About</a></li>
<li role="none"><a href="#services" role="menuitem" tabindex="-1">Services</a></li>
<li role="none"><a href="#contact" role="menuitem" tabindex="-1">Contact</a></li>
</ul>
</nav>
</header>
<main id="main-content">
<h1>Welcome to Our Site</h1>
<p>This is a responsive navigation menu example using semantic HTML (<nav>, <main>, <footer>), ARIA attributes for accessibility, and CSS for responsiveness. The menu collapses to a hamburger icon on mobile devices (under 769px width) and expands to a horizontal bar on desktop.</p>
<p>Key features:</p>
<ul>
<li>Mobile-first design with flexbox.</li>
<li>CSS-only toggle using checkbox hack.</li>
<li>Hamburger-to-X animation.</li>
<li>ARIA roles, labels, aria-expanded, aria-controls, and aria-label for screen readers.</li>
<li>Skip link for keyboard navigation.</li>
<li>Focus management (tabindex adjusted in menu items).</li>
</ul>
</main>
<footer role="contentinfo">
<p>© 2023 Example Site. Built with semantic HTML and accessibility in mind.</p>
</footer>
<script>
// Minimal JS for dynamic ARIA updates (best practice for aria-expanded)
// Syncs checkbox state with aria-expanded on the toggle button
const toggleCheckbox = document.getElementById('nav-toggle');
const toggleLabel = document.querySelector('.nav-toggle-label');
const menu = document.getElementById('primary-menu');
toggleCheckbox.addEventListener('change', () => {
const isExpanded = toggleCheckbox.checked;
toggleLabel.setAttribute('aria-expanded', isExpanded);
menu.setAttribute('aria-hidden', !isExpanded);
});
// Keyboard support: Enter/Space to toggle
toggleLabel.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
toggleCheckbox.checked = !toggleCheckbox.checked;
toggleCheckbox.dispatchEvent(new Event('change'));
}
});
// Close menu on link click (mobile UX)
document.querySelectorAll('.menu a').forEach(link => {
link.addEventListener('click', () => {
toggleCheckbox.checked = false;
toggleCheckbox.dispatchEvent(new Event('change'));
});
});
</script>
</body>
</html>