The problem
You need an animated hamburger menu icon for mobile navigation.
The solution
.hamburger {
width: 30px;
height: 24px;
position: relative;
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.hamburger span {
display: block;
height: 3px;
width: 100%;
background: #fff;
border-radius: 3px;
transition: all 0.3s ease;
}
/* Active state - X shape */
.hamburger.active span:nth-child(1) {
transform: translateY(10.5px) rotate(45deg);
}
.hamburger.active span:nth-child(2) {
opacity: 0;
transform: translateX(-20px);
}
.hamburger.active span:nth-child(3) {
transform: translateY(-10.5px) rotate(-45deg);
}
/* Alternative: Arrow transformation */
.hamburger.arrow.active span:nth-child(1) {
transform: translateY(4px) rotate(45deg) scaleX(0.5);
transform-origin: right;
}
.hamburger.arrow.active span:nth-child(3) {
transform: translateY(-4px) rotate(-45deg) scaleX(0.5);
transform-origin: right;
}See it live
Put it to work
<button class="hamburger" onclick="this.classList.toggle('active')">
<span></span>
<span></span>
<span></span>
</button>Worth knowing
Toggle the .active class with JavaScript. Works great with mobile slide-out navigation.