The hamburger menu is a must when dealing with responsive design. Having the ability to show/hide the menu on small screens is essential to keep the navigation on the website easy. In this post, we will build a CSS-only responsive menu with a minimalist code.
This article will be a step-by-step tutorial where we will start by creating the icons until we reach a fully functional menu.
Creating the icons
To build the hamburger menu, we need two icons: The hamburger icon and the close icon. A lot of techniques exist to create such icons and many of them rely on a lot of code but here we are going to use only CSS applied to one HTML element.
For the hamburger menu, we need 3 CSS declarations as follows:
.menu {
width: 150px; /* control the size */
aspect-ratio: 1;
background: linear-gradient(#000 50%, #0000 0) 0 0/100% 40%;
}
We define a square element thanks to width and aspect-ratio then using a gradient we create the 3 bars. Below is an illustration to understand how the gradient is working:




















