{"id":195,"date":"2019-08-23T02:00:52","date_gmt":"2019-08-23T02:00:52","guid":{"rendered":"https:\/\/www.antpace.com\/blog\/?p=195"},"modified":"2025-08-25T13:57:50","modified_gmt":"2025-08-25T13:57:50","slug":"easy-hamburger-menu","status":"publish","type":"post","link":"https:\/\/www.antpace.com\/blog\/easy-hamburger-menu\/","title":{"rendered":"Easy hamburger (menu) recipe"},"content":{"rendered":"<p>I think it&#8217;s best to avoid using plug-ins when possible. It reduces bloat and &#8220;black-box&#8221; code.<\/p>\n<p>The mobile &#8220;hamburger&#8221; menu is a staple of responsive user interface design. Users know that clicking on that three-lined icon will show a menu. It&#8217;s a modern solution to displaying long navigation lists on smaller screens.<\/p>\n<p>&#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Hamburger_button\" target=\"_blank\" rel=\"noopener\">A &#8216;hamburger&#8217; menu is a button (usually in the corner of a screen) that toggles a menu or list of hyperlinks.<\/a>&#8221;<\/p>\n<p>Below is a simple rendition using basic web technology. I used this recently as part of\u00a0 a website that showcases the work of a graphic artist.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-230\" src=\"https:\/\/www.antpace.com\/blog\/wp-content\/uploads\/2019\/08\/mobile-menu.png\" alt=\"mobile menu example\" width=\"376\" height=\"656\" \/><\/p>\n<p><strong>HTML:<\/strong><\/p>\n<p>Drop this code in your header file for the menu (list of links) itself.<\/p>\n<pre>&lt;div class=\"mobile-menu\"&gt;\n\t&lt;span class=\"close-mobile-menu\"&gt;&lt;i class=\"far fa-times-circle\"&gt;&lt;\/i&gt;&lt;\/span&gt;\n\n\t&lt;ul&gt;\n\n\t\t\t&lt;li&gt;&lt;a href=\"\/biography\"&gt;Biography&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t&lt;li&gt;&lt;a href=\"\/education\"&gt;Education &amp; Awards&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t&lt;li&gt;&lt;a href=\"\/reviews?order=asc\"&gt;Reviews&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t&lt;li&gt;&lt;a href=\"\/etchings\"&gt;Etchings&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t&lt;li&gt;&lt;a href=\"\/category\/paintings\/1960s\/\"&gt;Paintings&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t&lt;li&gt;&lt;a href=\"\/mukfa-about\"&gt;Mukfa&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t&lt;li&gt;&lt;a href=\"\/category\/drawings\/human-comedy\/\"&gt;Drawings&lt;\/a&gt;&lt;\/li&gt;\n\t\t\t&lt;li&gt;&lt;a href=\"\/exhibitions-and-collections\"&gt;Exhibitions &amp; Collections&lt;\/a&gt;&lt;\/li&gt;\n\n\t&lt;\/ul&gt;\n\n&lt;\/div&gt;\n<\/pre>\n<p>Next, add this to your existing navigation, or wherever you&#8217;d like the hamburger button to show.<\/p>\n<pre>&lt;div class=\"mobile-hamburger mobile-only\"&gt;&lt;i class=\"fas fa-bars\"&gt;&lt;\/i&gt;&lt;\/div&gt;\n\n<\/pre>\n<p>I used <a href=\"https:\/\/fontawesome.com\/\">FontAwesome<\/a> to generate the hamburger icon itself (and the close icon). Alternatively, you can use an image file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-224\" src=\"https:\/\/www.antpace.com\/blog\/wp-content\/uploads\/2019\/11\/hamburger-menu.png\" alt=\"hamburger menu\" width=\"89\" height=\"79\" \/><\/p>\n<p><strong>CSS:<\/strong><\/p>\n<p>This code sets the hamburger button to only show on mobile devices. Mobile devices are specified at 787px or less by a media query.<\/p>\n<pre>.mobile-hamburger{\n\tfont-size: 36px;\n\tcolor: #005FAA;\n\tfloat: right;\n\tcursor: pointer;\n\tmargin-right: 16px;\n\tmargin-top: 5px;\n\n}\n.mobile-menu{\n\tdisplay: none;\n\twidth: 100%;\n\tbackground: #DCC7AA;\n\tposition: fixed;\n\theight: 100%;\n\tright: 0;\n\ttop: 0;\n\tz-index: 20;\n}\n.mobile-menu ul{\n\tlist-style-type: none;\n\tfont-size: 16px;\n\ttext-align: left;\n\tpadding: 25px;\n\tmargin: 50px 0px;\n}\n\n.mobile-menu ul li{\n\tmargin-top: 15px;\n}\n\n.close-mobile-menu{\n\tposition: absolute;\n\ttop: 5px;\n\tright: 16px;\n\tfont-size: 36px;\n\tcursor: pointer;\n}\n\n@media only screen and (min-width:787px) {\n\t.mobile-only{display: none;}\n}\n<\/pre>\n<p><strong>JavaScript:<\/strong><\/p>\n<p>With jQuery:<\/p>\n<pre>(function ($, root, undefined) {\n\n\t$(function () {\n\n\t\t'use strict';\n\n\t\t\/\/ DOM ready, take it away\n\t\t$(\".mobile-hamburger\").click(function(){\n\t\t\t$(\".mobile-menu\").show();\n\t\t});\n\n\t\t$(\".close-mobile-menu\").click(function(){\n\t\t\t$(\".mobile-menu\").hide();\n\t\t});\n\n\n\n\t});\n\n})(jQuery, this);\n\n<\/pre>\n<p>Or, plain vanilla JS:<\/p>\n<pre>document.addEventListener('click', function (event) {\n\n\tif (!event.target.matches('.mobile-hamburger')){\n\t\treturn;\n\t}\n\n\tdocument.getElementsByClassName('mobile-menu')[0].style.display = 'block';\n\n}, false);\n\ndocument.addEventListener('click', function (event) {\n\n\tif (!event.target.matches('.close-mobile-menu')){\n\t\treturn;\n\t}\n\n\tdocument.getElementsByClassName('mobile-menu')[0].style.display = 'none';\n\n}, false);\n<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>By the way, this is the tool I&#8217;ve been using to encode the HTML I paste into my WordPress posts (that way, it doesn&#8217;t actually render on page):\u00a0<a href=\"https:\/\/github.com\/mathiasbynens\/mothereff.in\/tree\/master\/html-entities\">https:\/\/github.com\/mathiasbynens\/mothereff.in\/tree\/master\/html-entities<\/a><\/p>\n<p><a href=\"https:\/\/mothereff.in\/html-entities\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-227\" src=\"https:\/\/www.antpace.com\/blog\/wp-content\/uploads\/2019\/11\/html-entity-encoded-decoded.png\" alt=\"html entities encoded and decoded\" width=\"1520\" height=\"866\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I think it&#8217;s best to avoid using plug-ins when possible. It reduces bloat and &#8220;black-box&#8221; code. The mobile &#8220;hamburger&#8221; menu is a staple of responsive user interface design. Users know that clicking on that three-lined icon will show a menu. It&#8217;s a modern solution to displaying long navigation lists on smaller screens. &#8220;A &#8216;hamburger&#8217; menu &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.antpace.com\/blog\/easy-hamburger-menu\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Easy hamburger (menu) recipe&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3151,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-195","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development"],"_links":{"self":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":1,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":3152,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions\/3152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/media\/3151"}],"wp:attachment":[{"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.antpace.com\/blog\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}