<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Animated Hamburger Menu</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
overflow-x: hidden;
}
/* Demo content */
.content {
padding: 100px 20px;
color: white;
text-align: center;
font-size: 1.2rem;
}
/* Hamburger Menu Button */
.hamburger {
position: fixed;
top: 20px;
left: 20px;
z-index: 1001;
width: 50px;
height: 50px;
background: rgba(255, 255, 255, 0.9);
border: none;
border-radius: 50%;
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
transition: background 0.3s ease;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
.hamburger:hover {
background: rgba(255, 255, 255, 1);
}
.hamburger span {
display: block;
width: 25px;
height: 3px;
background: #333;
border-radius: 2px;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
transform-origin: center;
}
.hamburger span:nth-child(1) {
margin-bottom: 5px;
}
.hamburger span:nth-child(2) {
margin-bottom: 5px;
}
/* Active states for hamburger animation (to X) */
.nav-active .hamburger span:nth-child(1) {
transform: rotate(45deg) translate(7px, 7px);
}
.nav-active .hamburger span:nth-child(2) {
opacity: 0;
transform: scale(0);
}
.nav-active .hamburger span:nth-child(3) {
transform: rotate(-45deg) translate(6px, -6px);
}
/* Navigation Menu */
.nav-menu {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background: rgba(0, 0, 0, 0.95);
transform: translateX(-100%);
transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
z-index: 1000;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 20px;
}
.nav-active .nav-menu {
transform: translateX(0);
}
.nav-menu ul {
list-style: none;
}
.nav-menu li {
margin: 20px 0;
}
.nav-menu a {
color: white;
text-decoration: none;
font-size: 2rem;
font-weight: bold;
padding: 15px 30px;
border-radius: 50px;
transition: all 0.3s ease;
display: block;
backdrop-filter: blur(10px);
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
}
.nav-menu a:hover {
background: rgba(255, 255, 255, 0.2);
transform: translateY(-5px);
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
}
/* Hide hamburger on large screens (optional) */
@media (min-width: 768px) {
.hamburger {
display: none;
}
.nav-menu {
transform: translateX(0) !important;
position: relative;
width: auto;
height: auto;
background: none;
flex-direction: row;
padding: 0;
z-index: 10;
}
.nav-menu ul {
display: flex;
}
.nav-menu li {
margin: 0 0 0 20px;
}
.nav-menu a {
font-size: 1rem;
padding: 10px 20px;
background: none;
border: none;
}
}
/* Accessibility improvements */
.hamburger:focus {
outline: 3px solid #fff;
outline-offset: 3px;
}
</style>
</head>
<body>
<!-- Demo Content -->
<div class="content">
<h1>Animated Hamburger Menu Demo</h1>
<p>Resize your browser or view on mobile. Click the hamburger icon to open/close the menu.</p>
</div>
<!-- Hamburger Button -->
<button class="hamburger" aria-label="Toggle navigation menu" role="button">
<span></span>
<span></span>
<span></span>
</button>
<!-- Navigation Menu -->
<nav class="nav-menu" role="navigation" aria-label="Main navigation">
<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="#portfolio">Portfolio</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
<script>
// JavaScript to toggle the menu
document.addEventListener('DOMContentLoaded', function() {
const hamburger = document.querySelector('.hamburger');
const body = document.body; // Toggle class on body for simplicity
hamburger.addEventListener('click', function(e) {
e.stopPropagation();
body.classList.toggle('nav-active');
});
// Close menu when clicking outside (optional enhancement)
document.addEventListener('click', function(e) {
if (!hamburger.contains(e.target) && body.classList.contains('nav-active')) {
body.classList.remove('nav-active');
}
});
// Close menu on window resize (if desktop view)
window.addEventListener('resize', function() {
if (window.innerWidth >= 768) {
body.classList.remove('nav-active');
}
});
});
</script>
</body>
</html>
Features:
- Smooth Animations: Hamburger transforms into an "X" using CSS transforms and transitions. Menu slides in from the left.
- Mobile-First: Full-screen overlay on mobile, collapses to horizontal nav on desktop (≥768px).
- Accessible: ARIA labels, focus styles, keyboard-navigable.
- Touch-Friendly: Large tap targets, hover effects.
- Performance: GPU-accelerated transforms, no libraries needed.
- Customizable: Easy to tweak colors, timings, or add more links.
Copy-paste into an HTML file and open in a browser. Test on mobile for best effect! 🚀