{"id":5079,"date":"2022-01-12T01:00:00","date_gmt":"2022-01-12T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/hamburger-menu-css-responsive\/"},"modified":"2024-02-08T00:36:08","modified_gmt":"2024-02-07T23:36:08","slug":"hamburger-menu-css-responsive","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/hamburger-menu-css-responsive\/","title":{"rendered":"How To Make a Responsive Hamburger Menu [CSS]"},"content":{"rendered":"\n<p>A <strong>CSS hamburger menu (responsive)<\/strong> is one of those cool slide-out navigation menus that appears when you click those three-line menu icons.<\/p>\n\n\n\n<p>(It\u2019s also a convenient way to buy fast food &#8211; but that\u2019s not important right now)<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Looking for ready-to-use hamburger menus examples? Check these <a href=\"https:\/\/alvarotrigo.com\/blog\/hamburger-menu-css\/\">10 CodePens of CSS Hamburger Menus<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"..\/assets\/imgs\/2022-01-12\/create-responsive-hamburger-menu-css.gif\" alt=\"Create a Responsive Hamburger Menu with CSS\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/><\/figure>\n\n\n\n<p>In this post, you\u2019ll learn <strong>how to create a responsive hamburger menu (CSS only &#8211; no JS needed!)<\/strong>. But you might be wondering, why bother?  Why hide your beautiful navigation behind a hamburger icon?<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages-of-a-responsive-css-hamburger-menu\">Advantages of a Responsive CSS Hamburger Menu<\/h2>\n\n\n<ol class=\"wp-block-list\">\n<li>According to Oberlo, over <a href=\"https:\/\/www.oberlo.co.uk\/statistics\/mobile-internet-traffic\" target=\"_blank\" rel=\"noopener nofollow\">56% of web traffic<\/a> comes through mobile devices.<\/li>\n\n\n\n<li>Full-width navigation menus are often unusable on small screens.<\/li>\n\n\n\n<li>Positioning menu items vertically solves this problem &#8211; but then the user has to scroll past the menu to get to the content &#8211; not ideal.<\/li>\n\n\n\n<li>By using fixed positioning on the hamburger icon, your visitors can access the nav no matter where they are on your page.<\/li>\n<\/ol>\n\n\n\n<p>OK enough talk, let&#8217;s make one! First, we&#8217;ll start with the structure&#8230;<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"structure-of-a-responsive-hamburger-menu-(html)\">Structure of a Responsive Hamburger Menu (HTML)<\/h2>\n\n\n<p>If we were using JavaScript to do this, we&#8217;d set up an event listener to detect when the user clicks on the icon, then trigger the menu to appear.<\/p>\n\n\n\n<p>Since we&#8217;re making this responsive hamburger menu CSS-style, we have to use a different approach.<\/p>\n\n\n\n<p>We need two elements, a <code>button<\/code> for the icon, and a <code>nav<\/code> for the menu itself. The <code>nav<\/code> element needs to be nested inside the button:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"hamburger-menu\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">nav<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"sidebar-menu\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">nav<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can fill your <code>nav<\/code> menu with anything you want. We&#8217;ll just use some common top-level pages for this example (don&#8217;t forget to replace <code>#<\/code> with your actual page urls!):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"hamburger-menu\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">nav<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"sidebar-menu\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span>&gt;<\/span>Menu<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Home<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Store<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Blog<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>About<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Contact<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">nav<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"overlay\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_ExwNYwj\" data-src=\"\/\/codepen.io\/anon\/embed\/ExwNYwj?height=450&amp;theme-id=1&amp;slug-hash=ExwNYwj&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed ExwNYwj\" title=\"CodePen Embed ExwNYwj\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>OK, we have the structure sorted&#8230; but it doesn&#8217;t look how we want, and it doesn&#8217;t do anything. Let&#8217;s solve that with some CSS&#8230;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Check out these <a href=\"https:\/\/alvarotrigo.com\/blog\/slide-menus\/\">examples of great side menus<\/a> for your webpage!<\/p>\n<\/blockquote>\n\n\n<h2 class=\"wp-block-heading\" id=\"styling-the-responsive-hamburger-menu-(css)\">Styling the Responsive Hamburger Menu (CSS)<\/h2>\n\n\n<p>There are many ways to get the three lines of the hamburger icon itself &#8211; we&#8217;ll use a linear gradient as Chris Coyier over at CSS tricks <a href=\"https:\/\/css-tricks.com\/three-line-menu-navicon\/\" target=\"_blank\" rel=\"noopener nofollow\">explains<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-id\">#hamburger-menu<\/span> {\n    <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">50px<\/span>;\n    <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">50px<\/span>;\n    <span class=\"hljs-attribute\">display<\/span>: block;\n    <span class=\"hljs-attribute\">border<\/span>: none;\n    <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-built_in\">linear-gradient<\/span>(\n      to bottom, \n      #<span class=\"hljs-number\">3<\/span>D0E61, #<span class=\"hljs-number\">3<\/span>D0E61 <span class=\"hljs-number\">20%<\/span>, \n      white <span class=\"hljs-number\">20%<\/span>, white <span class=\"hljs-number\">40%<\/span>, \n      #<span class=\"hljs-number\">3<\/span>D0E61 <span class=\"hljs-number\">40%<\/span>, #<span class=\"hljs-number\">3<\/span>D0E61 <span class=\"hljs-number\">60%<\/span>, \n      white <span class=\"hljs-number\">60%<\/span>, white <span class=\"hljs-number\">80%<\/span>, \n      #<span class=\"hljs-number\">3<\/span>D0E61 <span class=\"hljs-number\">80%<\/span>, #<span class=\"hljs-number\">3<\/span>D0E61 <span class=\"hljs-number\">100%<\/span>\n    );\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, because the menu is nested inside the hamburger icon, we need to set its <code>position<\/code> to <code>absolute<\/code>. This takes it out of the flow and enables us to position it manually.<\/p>\n\n\n\n<p>We&#8217;ll set the responsive hamburger menu&#8217;s <code>top<\/code> to 0, <code>left<\/code> to -250px, and <code>width<\/code> to 200px. This will position it off-screen.<\/p>\n\n\n\n<p>Technically we only need to set <code>left<\/code> to -200px, since that&#8217;s how wide the element is. But I like to add a bit more, just for insurance. We&#8217;ll also set <code>visibility<\/code> to <code>hidden<\/code> for good measure.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-id\">#hamburger-menu<\/span> <span class=\"hljs-selector-id\">#sidebar-menu<\/span> {\n    <span class=\"hljs-attribute\">visibilty<\/span>: hidden;\n    <span class=\"hljs-attribute\">position<\/span>: fixed;\n    <span class=\"hljs-attribute\">top<\/span>: <span class=\"hljs-number\">0<\/span>;\n    <span class=\"hljs-attribute\">left<\/span>: -<span class=\"hljs-number\">250px<\/span>;\n    <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">200px<\/span>;\n    <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\n    <span class=\"hljs-attribute\">transition<\/span>: <span class=\"hljs-number\">0.3s<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now we have a hamburger menu icon, and we can&#8217;t see the menu &#8211; yet. So far so good:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_mdBObBB\" data-src=\"\/\/codepen.io\/anon\/embed\/mdBObBB?height=450&amp;theme-id=1&amp;slug-hash=mdBObBB&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed mdBObBB\" title=\"CodePen Embed mdBObBB\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-functionality-to-the-hamburger-menu-with-css\">Adding Functionality to the Hamburger Menu with CSS<\/h2>\n\n\n<p>So, how do you make the responsive hamburger menu actually work, without using JavaScript? How do we get a real Hamburger Menu CSS-styled?<\/p>\n\n\n\n<p>We will use a hidden checkbox together with the <code>:checked<\/code> pseudo-class. It&#8217;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.<\/p>\n\n\n\n<p>But&#8230; how do make a checkbox change its <code>:checked<\/code> status whene it&#8217;s not visible? By using a <code>label<\/code> element for that checkbox and showing that <code>label<\/code> element instead.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">\n<span class=\"hljs-comment\">&lt;!-- We add the checkbox --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"checkbox\"<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"hamburger-input\"<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"burger-shower\"<\/span> \/&gt;<\/span>\n\n<span class=\"hljs-comment\">&lt;!-- \n  We use a `label` element with the `for` attribute \n  with the same value as  our label `id` attribute \n--&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">label<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"hamburger-menu\"<\/span> <span class=\"hljs-attr\">for<\/span>=<span class=\"hljs-string\">\"hamburger-input\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">nav<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"sidebar-menu\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h3<\/span>&gt;<\/span>Menu<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h3<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Home<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Store<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Blog<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>About<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Contact<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">nav<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">label<\/span>&gt;<\/span>\n\n<span class=\"hljs-comment\">&lt;!-- \n  Adding an overlay to show a dark \n  background when the menu appears\n--&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"overlay\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We will style the <code>label<\/code> in a way that it looks like a burger menu, so when someone clicks on it, the hidden checkbox status will also change.<\/p>\n\n\n\n<p>This way, we are able to conditioanlly trigger CSS changes in other elements by using the <code>:checked<\/code> pseudo-class.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-id\">#hamburger-input<\/span><span class=\"hljs-selector-pseudo\">:checked<\/span> + <span class=\"hljs-selector-id\">#hamburger-menu<\/span> <span class=\"hljs-selector-id\">#sidebar-menu<\/span> {\n    <span class=\"hljs-attribute\">visibility<\/span>: visible;\n    <span class=\"hljs-attribute\">left<\/span>: <span class=\"hljs-number\">0<\/span>;\n}\n<span class=\"hljs-selector-id\">#hamburger-input<\/span><span class=\"hljs-selector-pseudo\">:checked<\/span> ~ <span class=\"hljs-selector-class\">.overlay<\/span>{\n   <span class=\"hljs-attribute\">visibility<\/span>: visible;\n  <span class=\"hljs-attribute\">opacity<\/span>: <span class=\"hljs-number\">0.4<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Two things to note here:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Notice how we are using the combinator <code>+<\/code> symbol in our CSS. This is a combinator symbol that allows us to select inmediate siblings to the first element.<\/li>\n\n\n\n<li>We are also using the <code>~<\/code> symbol. This allows us to select non inmediate siblings. And we need to use it because <code>.overlay<\/code> is not inmediately after <code>#hamburger-input<\/code>.<\/li>\n\n\n\n<li>Notice the styles we&#8217;ve applied. We set <code>visibility<\/code> to <code>visible<\/code> (always a good idea if you want people to see things!), and set the <code>left<\/code> property to <code>0<\/code> &#8211; this will bring it into view (remember it was <code>-250px<\/code> previously).<\/li>\n<\/ol>\n\n\n<h3 class=\"wp-block-heading\" id=\"using-transition-to-slide-the-menu-into-view\">Using Transition to Slide the Menu into View<\/h3>\n\n\n<p>As it stands, this would make the CSS Hamburger menu appear instantly on the screen. But it&#8217;s much cooler to have it slide in from the left. To do that, we apply a <code>transition<\/code> to the <code>#sidebar-menu<\/code> element:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">transition<\/span>: 0<span class=\"hljs-selector-class\">.3s<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This means it&#8217;ll take 0.3 seconds to slide in &#8211; you can change this to fit your preferences.<\/p>\n\n\n\n<p>OK, now let&#8217;s see how it looks! I&#8217;ve added a little extra styling to the menu too:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_OJxbLxv\" data-src=\"\/\/codepen.io\/anon\/embed\/OJxbLxv?height=450&amp;theme-id=1&amp;slug-hash=OJxbLxv&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed OJxbLxv\" title=\"CodePen Embed OJxbLxv\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>If the user wants to close the menu, they just need to click or tap on anything outside the menu itself &#8211; a common and intuitive way to do it.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"add-the-main-navbar\">Add the Main Navbar<\/h2>\n\n\n<p>If the CSS hamburger menu is all you need, you&#8217;re good to go &#8211; enjoy!<\/p>\n\n\n\n<p>But if you&#8217;re also interested in setting up a <em>responsive<\/em> CSS hamburger menu, then stick around!<\/p>\n\n\n\n<p>Here&#8217;s the plan:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the visitor has a wide enough screen, we&#8217;ll show them a full-width nav bar.<\/li>\n\n\n\n<li>If they have a smaller screen, we&#8217;ll show them the CSS hamburger menu.<\/li>\n<\/ul>\n\n\n\n<p>So first, let&#8217;s set up horizontal nav bar. The HTML:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">nav<\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"main-menu\"<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ul<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Home<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Store<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Blog<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>About<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">li<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"#\"<\/span>&gt;<\/span>Contact<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">li<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">ul<\/span>&gt;<\/span>  \n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">nav<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>&#8230;and the CSS:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-id\">#main-menu<\/span> {\n  <span class=\"hljs-attribute\">display<\/span>: block;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100px<\/span>;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-number\">#3D0E61<\/span>;\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0px<\/span>;\n}\n\n<span class=\"hljs-selector-id\">#main-menu<\/span> <span class=\"hljs-selector-tag\">ul<\/span> {\n  <span class=\"hljs-attribute\">max-width<\/span>: <span class=\"hljs-number\">800px<\/span>;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0px<\/span> auto;\n  <span class=\"hljs-attribute\">display<\/span>: flex;\n  <span class=\"hljs-attribute\">justify-content<\/span>: space-evenly;\n  <span class=\"hljs-attribute\">align-items<\/span>: center;\n}\n\n<span class=\"hljs-selector-id\">#main-menu<\/span> <span class=\"hljs-selector-tag\">li<\/span> {\n  <span class=\"hljs-attribute\">list-style-type<\/span>: none;\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">2rem<\/span>;\n}\n\n<span class=\"hljs-selector-id\">#main-menu<\/span> <span class=\"hljs-selector-tag\">a<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-number\">#B9FAF8<\/span>;\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">1.5rem<\/span>;\n  <span class=\"hljs-attribute\">text-decoration<\/span>: none;\n}\n\n<span class=\"hljs-selector-id\">#main-menu<\/span> <span class=\"hljs-selector-tag\">a<\/span><span class=\"hljs-selector-pseudo\">:hover<\/span> {\n  <span class=\"hljs-attribute\">text-decoration<\/span>: underline;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because the hamburger icon is a block element, this navbar will push it out of position &#8211; so let&#8217;s make sure it stays in the top left of the screen by adding the following code to <code>#hamburger-menu<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">position<\/span>: <span class=\"hljs-selector-tag\">fixed<\/span>;\n<span class=\"hljs-selector-tag\">top<\/span>: 20<span class=\"hljs-selector-tag\">px<\/span>;\n<span class=\"hljs-selector-tag\">left<\/span>: 20<span class=\"hljs-selector-tag\">px<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now for the &#8216;responsive&#8217; part&#8230;<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"making-the-hamburger-menu-responsive-with-css\">Making the Hamburger Menu Responsive with CSS<\/h2>\n\n\n<p>We&#8217;ll use a media query for this.<\/p>\n\n\n\n<p>We need to choose a breakpoint &#8211; a screen width that will cause the display to switch between the full-width menu and the responsive CSS hamburger menu.<\/p>\n\n\n\n<p>The width you choose will be unique to you &#8211; if you have lots of menu items, it&#8217;ll need to be wider. For this example, I&#8217;ll go with 750px:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-keyword\">@media<\/span> screen <span class=\"hljs-keyword\">and<\/span> (<span class=\"hljs-attribute\">max-width:<\/span> <span class=\"hljs-number\">750px<\/span>) {\n  <span class=\"hljs-selector-id\">#main-menu<\/span> {\n    <span class=\"hljs-attribute\">display<\/span>: none;\n  }\n  <span class=\"hljs-selector-id\">#hamburger-menu<\/span> {\n    <span class=\"hljs-attribute\">display<\/span>: inline;\n  }\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When the screen is smaller than 750px, these styles will be applied.<\/p>\n\n\n\n<p>And we also need to hide the responsive hamburger menu (CSS) when the screen is wider than 750px. To do that, we just change <code>display: block;<\/code> to <code>display: none in <\/code>#hamburger-menu`. So it will be hidden by default.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"final-result\">Final Result<\/h2>\n\n\n<p>That&#8217;s it! Here&#8217;s the final CSS Hamburger menu (responsive):<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_MWEbgEd\" data-src=\"\/\/codepen.io\/anon\/embed\/MWEbgEd?height=450&amp;theme-id=1&amp;slug-hash=MWEbgEd&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed MWEbgEd\" title=\"CodePen Embed MWEbgEd\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>Hope that was useful to you! You can take this as a template, and change the colors and styles to suit your needs.<\/p>\n\n\n\n<p>Personally, I love how a CSS Hamburger Menu looks on full-screen websites. If you do not believe me, just check it for yourself:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_ExwgbZv\" data-src=\"\/\/codepen.io\/anon\/embed\/ExwgbZv?height=450&amp;theme-id=1&amp;slug-hash=ExwgbZv&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed ExwgbZv\" title=\"CodePen Embed ExwgbZv\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codepen.io\/alvarotrigo\/pen\/ExwgbZv\" title=\"Preview\" target=\"_blank\" rel=\"noopener nofollow\">Preview<\/a><\/div>\n<\/div>\n\n\n\n<p>Cool, right? If you like this fancy style, I recommend that you check out <a href=\"https:\/\/alvarotrigo.com\/fullPage\/\">fullPage.js<\/a>. It&#8217;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&#8230; voil\u00e1! Your website is ready!<\/p>\n\n\n\n<p>\n    <a href=\"https:\/\/alvarotrigo.com\/fullPage\/\">\n        <video width=\"680\" height=\"382\" controls muted autoplay loop playsinline>\n        <source src= \"https:\/\/cdn.jsdelivr.net\/gh\/alvarotrigo\/blog-assets\/videos\/fullPage_intro.mp4\" type=\"video\/mp4\" \/>\n        <source src= \"https:\/\/cdn.jsdelivr.net\/gh\/alvarotrigo\/blog-assets\/videos\/fullPage-intro.webm\" type=\"video\/webm\"\/>\n        Your browser does not support the video tag.\n        <\/source><\/source><\/video>\n    <\/a>\n<\/p>\n\n\n\n<p>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&#8217;re covered. And there are some great animations to take people from page to page &#8211; the <a href=\"https:\/\/alvarotrigo.com\/fullPage\/extensions\/drop-effect.html\">drop effect<\/a> is one of my favorites.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"related-posts%3A\">Related Posts:<\/h2>\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/hamburger-menu-css\/\">10+ Hamburger Menu Examples with only CSS<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/css-transition-duration\/\">CSS Transition duration &#8211; Beginners Guide<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/css-transition-timing-function\/\">CSS Transition [Timing Function &amp; Delay]<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/sticky-navbar\/\">How to Create a Sticky Navbar [CSS &amp; JS]<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/best-css-button-hover-effects\/\">10 Best CSS button hover effects<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to make a fully responsive hamburger menu for your website &#8211; in CSS only, no JavaScript needed!<\/p>\n","protected":false},"author":6,"featured_media":5078,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[106],"tags":[9,16,17],"class_list":["post-5079","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","tag-css","tag-design","tag-web"],"acf":[],"_links":{"self":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/comments?post=5079"}],"version-history":[{"count":5,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5079\/revisions"}],"predecessor-version":[{"id":9572,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5079\/revisions\/9572"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/5078"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=5079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=5079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=5079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}