How To Build An Animated Hamburger Menu With Only CSS

Web Dev Simplified · 142K views · 2023 · 24:05

Open on YouTube

Web developmentUX & UI designtutorial

Why Stack Overflow recommends it

Stack Overflow users cite it when they have followed this tutorial's hamburger menu build and are debugging a problem such as pseudo-element display, bar-to-X animation sizing, or a dropdown disappearing on click.

Recommended by 1 people across 1 different months, 2023 to 2025.

What it is

A 24-minute tutorial from Web Dev Simplified that builds a responsive, animated hamburger menu using only HTML and CSS, with no JavaScript. It covers the markup, CSS variables, the three-bar styling, the bars-to-X animation, a sliding sidebar, and accessibility styles.

What people said

Recommends Mike McCollister · 2025-07 · Stack Overflow · on “Hamburger menu animation causing label "container" to grow”

I am trying to make a hamburger menu. I have most of the mechanics down but I am struggling with the animations for the three lines turning into an "x". When I rotate the top and bottom lines and change the width, the lines move to the right. From what I can tell, the width of the label "container" gets the width of the two lines, even though when rotated, they should fit in label "container" without it growing. Any ideas how to get it not to become wider? I would like it so that the "x" is bound by the original label "container" size. That is if it is wider than tall, then the height will be the limiting factor. The included code shows what happens. I enlarged everything and slowed down the

Cites Michael · 2023-08 · Stack Overflow · on “Why I don't see ::after and ::before element without display: flex?”

ABOUT PROBLEM I tried to make hamburger-menu from [this video] . In 6.23 minute we had a problem with displaying ::after and ::before element - without display: flex on .hamburger-menu class we don't see black spaces. In tutorial I heard we have the problem because .hamburger-menu is inline by default. I don't understand why we can see black elements only when parent class is flex or grid (I check) or when I fill content property with the text. Even if I set it to block element I don't see the change. CODE .hamburger-menu{ display:flex; } .hamburger-menu:after, .hamburger-menu:before, .hamburger-menu input{ width:60px; height:7px; content:""; background-color:rgb(30,30,30); border-radius:10000px;

Cites BjorneX · 2023-11 · Stack Overflow · on “Dropdown menu dissapears on click”

I have tried everything but it still dissapears on click. This is the video I followed btw [this video] . CSS :root { --bar-width: 60px; --bar-height: 8px; --hamburger-gap: 6px; --foreground: #333; --background: white; --hamburger-margin: 125px; --animation-timing: 200ms ease-in-out; --hamburger-height: calc(var(--bar-height)*3+var(--hamburger-gap)*2) } .hamburger-menu { --x-width: calc(var(--hamburger-height)*1.41421356237); display: flex; flex-direction: column; gap: var(--hamburger-gap); width: max-content; position: absolute; top: var(--hamburger-margin); left: var(--hamburger-margin); z-index: 999; cursor: pointer; } .hamburger-menu::before, .hamburger-menu::after, .hamburger-menu input { co