- How To Make a Responsive Hamburger Menu [CSS]
- Advantages of a Responsive CSS Hamburger Menu
- Structure of a Responsive Hamburger Menu (HTML)
- Styling the Responsive Hamburger Menu (CSS)
- Adding Functionality to the Hamburger Menu with CSS
- Using Transition to Slide the Menu into View
- Add the Main Navbar
- Making the Hamburger Menu Responsive with CSS
- Final Result
- Related Posts:
- 15 genius CSS Hamburger Menus (+ animations)
- #1 Gooey Menu
- #2 SVG CSS3 Menu/Burger Button
- #3 Mobile Menu Animation
- #4 Page Tilt Effect
- #5 Elastic menu
- #6 Hamburger Icons Animations
- #7 SVG Gooey Hover Menu Concept
- #8 Hamburger Icon Animations
- #9 Hamburger menu animation with velocity.js
- #10 Atomic Hamburger Menu CSS
- #11 CSS Menu Concept
- #12 Pure CSS Menu
- #13 Button Menu Concept
- #14 Standard Nav Bar
- #15 Standard Menu 2
- Conslusion
How To Make a Responsive Hamburger Menu [CSS]
A CSS hamburger menu (responsive) is one of those cool slide-out navigation menus that appears when you click those three-line menu icons.
(It’s also a convenient way to buy fast food — but that’s not important right now)
Looking for ready-to-use hamburger menus examples? Check these 10 CodePens of CSS Hamburger Menus.
In this post, you’ll learn how to create a responsive hamburger menu (CSS only — no JS needed!). But you might be wondering, why bother? Why hide your beautiful navigation behind a hamburger icon?
Advantages of a Responsive CSS Hamburger Menu
- According to Oberlo, over 56% of web traffic comes through mobile devices.
- Full-width navigation menus are often unusable on small screens.
- Positioning menu items vertically solves this problem — but then the user has to scroll past the menu to get to the content — not ideal.
- By using fixed positioning on the hamburger icon, your visitors can access the nav no matter where they are on your page.
OK enough talk, let’s make one! First, we’ll start with the structure.
Structure of a Responsive Hamburger Menu (HTML)
If we were using JavaScript to do this, we’d set up an event listener to detect when the user clicks on the icon, then trigger the menu to appear.
Since we’re making this responsive hamburger menu CSS-style, we have to use a different approach.
We need two elements, a button for the icon, and a nav for the menu itself. The nav element needs to be nested inside the button:
button id="hamburger-menu">
nav id="sidebar-menu">
nav>
button>
You can fill your nav menu with anything you want. We’ll just use some common top-level pages for this example (don’t forget to replace # with your actual page urls!):
label id="hamburger-menu">
nav id="sidebar-menu">
h3>Menuh3>
ul>
li>a href="#">Homea>li>
li>a href="#">Storea>li>
li>a href="#">Bloga>li>
li>a href="#">Abouta>li>
li>a href="#">Contacta>li>
ul>
nav>
label>
div class="overlay">div>
OK, we have the structure sorted. but it doesn’t look how we want, and it doesn’t do anything. Let’s solve that with some CSS.
Styling the Responsive Hamburger Menu (CSS)
There are many ways to get the three lines of the hamburger icon itself — we’ll use a linear gradient as Chris Coyier over at CSS tricks explains:
#hamburger-menu
width: 50px;
height: 50px;
display: block;
border: none;
background: linear-gradient(
to bottom,
#3D0E61, #3D0E61 20%,
white 20%, white 40%,
#3D0E61 40%, #3D0E61 60%,
white 60%, white 80%,
#3D0E61 80%, #3D0E61 100%
);
>
Now, because the menu is nested inside the hamburger icon, we need to set its position to absolute . This takes it out of the flow and enables us to position it manually.
We’ll set the responsive hamburger menu’s top to 0, left to -250px, and width to 200px. This will position it off-screen.
Technically we only need to set left to -200px, since that’s how wide the element is. But I like to add a bit more, just for insurance. We’ll also set visibility to hidden for good measure.
#hamburger-menu #sidebar-menu
visibilty: hidden;
position: fixed;
top: 0;
left: -250px;
width: 200px;
height: 100%;
transition: 0.3s;
>
Now we have a hamburger menu icon, and we can’t see the menu — yet. So far so good:
Adding Functionality to the Hamburger Menu with CSS
So, how do you make the responsive hamburger menu actually work, without using JavaScript? How do we get a real Hamburger Menu CSS-styled?
We will use a hidden checkbox together with the :checked pseudo-class. It’s a small hack to make sure our checkbox not only works on desktop computers but also on touch screen devices, where focusing elements is not a thing.
But. how do make a checkbox change its :checked status whene it’s not visible? By using a label element for that checkbox and showing that label element instead.
input type="checkbox" id="hamburger-input" class="burger-shower" />
We use a `label` element with the `for` attribute
with the same value as our label `id` attribute
-->
label id="hamburger-menu" for="hamburger-input">
nav id="sidebar-menu">
h3>Menuh3>
ul>
li>a href="#">Homea>li>
li>a href="#">Storea>li>
li>a href="#">Bloga>li>
li>a href="#">Abouta>li>
li>a href="#">Contacta>li>
ul>
nav>
label>
Adding an overlay to show a dark
background when the menu appears
-->
div class="overlay">div>
We will style the label in a way that it looks like a burger menu, so when someone clicks on it, the hidden checkbox status will also change.
This way, we are able to conditioanlly trigger CSS changes in other elements by using the :checked pseudo-class.
#hamburger-input:checked + #hamburger-menu #sidebar-menu
visibility: visible;
left: 0;
>
#hamburger-input:checked ~ .overlay
visibility: visible;
opacity: 0.4;
>
- Notice how we are using the combinator + symbol in our CSS. This is a combinator symbol that allows us to select inmediate siblings to the first element.
- We are also using the ~ symbol. This allows us to select non inmediate siblings. And we need to use it because .overlay is not inmediately after #hamburger-input .
- Notice the styles we’ve applied. We set visibility to visible (always a good idea if you want people to see things!), and set the left property to 0 — this will bring it into view (remember it was -250px previously).
Using Transition to Slide the Menu into View
As it stands, this would make the CSS Hamburger menu appear instantly on the screen. But it’s much cooler to have it slide in from the left. To do that, we apply a transition to the #sidebar-menu element:
This means it’ll take 0.3 seconds to slide in — you can change this to fit your preferences.
OK, now let’s see how it looks! I’ve added a little extra styling to the menu too:
If the user wants to close the menu, they just need to click or tap on anything outside the menu itself — a common and intuitive way to do it.
Add the Main Navbar
If the CSS hamburger menu is all you need, you’re good to go — enjoy!
But if you’re also interested in setting up a responsive CSS hamburger menu, then stick around!
- If the visitor has a wide enough screen, we’ll show them a full-width nav bar.
- If they have a smaller screen, we’ll show them the CSS hamburger menu.
So first, let’s set up horizontal nav bar. The HTML:
nav id="main-menu">
ul>
li>a href="#">Homea>li>
li>a href="#">Storea>li>
li>a href="#">Bloga>li>
li>a href="#">Abouta>li>
li>a href="#">Contacta>li>
ul>
nav>
#main-menu
display: block;
height: 100px;
width: 100%;
background: #3D0E61;
margin: 0px;
>
#main-menu ul
max-width: 800px;
width: 100%;
height: 100%;
margin: 0px auto;
display: flex;
justify-content: space-evenly;
align-items: center;
>
#main-menu li
list-style-type: none;
font-size: 2rem;
>
#main-menu a
color: #B9FAF8;
font-size: 1.5rem;
text-decoration: none;
>
#main-menu a:hover
text-decoration: underline;
>
Because the hamburger icon is a block element, this navbar will push it out of position — so let’s make sure it stays in the top left of the screen by adding the following code to #hamburger-menu :
position: fixed;
top: 20px;
left: 20px;
Now for the ‘responsive’ part.
Making the Hamburger Menu Responsive with CSS
We’ll use a media query for this.
We need to choose a breakpoint — a screen width that will cause the display to switch between the full-width menu and the responsive CSS hamburger menu.
The width you choose will be unique to you — if you have lots of menu items, it’ll need to be wider. For this example, I’ll go with 750px:
@media screen and (max-width: 750px)
#main-menu
display: none;
>
#hamburger-menu
display: inline;
>
>
When the screen is smaller than 750px, these styles will be applied.
And we also need to hide the responsive hamburger menu (CSS) when the screen is wider than 750px. To do that, we just change display: block; to display: none in #hamburger-menu`. So it will be hidden by default.
Final Result
That’s it! Here’s the final CSS Hamburger menu (responsive):
Hope that was useful to you! You can take this as a template, and change the colors and styles to suit your needs.
Personally, I love how a CSS Hamburger Menu looks on full-screen websites. If you do not believe me, just check it for yourself:
Cool, right? If you like this fancy style, I recommend that you check out fullPage.js. It’s a JS library that enables you to create professional-looking responsive full-page websites really easily. Then you just have to add your CSS Responsive Hamburger Menu and. voilá! Your website is ready!
FullPage.js also offers some cool navigation options you might like, whether you want a scroll bar, navigation dots, anchor links, or continuous scrolling, you’re covered. And there are some great animations to take people from page to page — the drop effect is one of my favorites.
Related Posts:
Warren Davies is a front end developer based in the UK.
You can find more from him at https://warrendavies.net
Join 2,000+ readers and learn something new every month!
15 genius CSS Hamburger Menus (+ animations)
Responsive web design has long been standard on the web. Almost every website has a menu on mobile devices, which is mostly realized by a hamburger menu. A collection of the best CSS hamburger menus (+ icons & animations) can be found here!
In mobile navigation, i.e. on the tablet or smartphone, the navigation menu is very often hidden for space reasons and only visible by clicking on the hamburger menu. There is endless scope for design possibilities here. Some are programmed with pure CSS without JavaScript and some with CSS and JavaScript – there is something for everyone.
A menu of mostly 2-3 layers has established itself – just like a hamburger (bun, patty, bun), which reveals all menu items in an overlay by a click. Individual strokes can be rotated to an X, disappear or whiz around wildly. Endless possibilities! I have compiled the best ones for you here – let yourself be inspired.
The pens shown are licensed with MIT. You can find more info about your own use on the Codepen Blog.
#1 Gooey Menu
See the Pen Gooey Menu by Lucas Bebber (@lbebber) on CodePen.
Author: Luca Bebber;
Coded in: HTML, CSS (SCSS);
#2 SVG CSS3 Menu/Burger Button
Author: Kyle Henwood;
Coded in: HTML, CSS (SCSS), JS;
#3 Mobile Menu Animation
Author: Stas Melnikov;
Coded in: HTML, CSS, JS;
#4 Page Tilt Effect
Author: Marco Fugaro;
Coded in: HTML, CSS (SCSS), JS (jQuery);
#5 Elastic menu
Author: Long;
Coded in: HTML, CSS (SCSS), JS;
#6 Hamburger Icons Animations
Author: Ahmad Emran;
Coded in: HTML, CSS, JS;
#7 SVG Gooey Hover Menu Concept
Author: Michael Leonard;
Coded in: HTML, CSS, JS (jQuery);
#8 Hamburger Icon Animations
Author: Rosa;
Coded in: HTML, CSS, JS (jQuery);
#9 Hamburger menu animation with velocity.js
Author: Filippo;
Coded in: HTML, CSS (Less), JS (jQuery + velocity.js);
#10 Atomic Hamburger Menu CSS
Author: Alex Coven;
Coded in: HTML, CSS;
#11 CSS Menu Concept
Author: GEOX;
Coded in: HTML, CSS (SCSS);
#12 Pure CSS Menu
Author: Ivan Grozdic;
Coded in: HTML, CSS;
#13 Button Menu Concept
See the Pen Button Menu Concept by James Truhlar (@mdcrtv) on CodePen.
Author: James Truhlar;
Coded in: HTML, CSS, JS (jQuery);
#14 Standard Nav Bar
See the Pen Nav Bar by Justin (@nilbog) on CodePen.
Author: Justin;
Coded in: HTML, CSS (SCSS), JS (jQuery);
#15 Standard Menu 2
Author: Selcuk Cura;
Coded in: HTML, CSS (SCSS), JS (classList.js, underscore.js, TweenMax.js, ScrambleTextPlugin.js, smooth-scroll.js);
Conslusion
Which menu would you choose for your website? Feel free to write it in the comments. And if you also have a fancy hamburger menu (icon), I can expand the collection.
Note: All buttons are all published on codepen.io and not by me .
Not enough yet? Then this could be something for you: