{"id":1481,"date":"2019-08-29T16:48:11","date_gmt":"2019-08-29T20:48:11","guid":{"rendered":"https:\/\/www.harrisweb.ca\/blog\/?p=1481"},"modified":"2019-10-04T10:42:33","modified_gmt":"2019-10-04T14:42:33","slug":"tutorial-create-responsive-navigation","status":"publish","type":"post","link":"https:\/\/www.harrisweb.ca\/blog\/tutorial-create-responsive-navigation\/","title":{"rendered":"Tutorial: Create Responsive Navigation from Scratch"},"content":{"rendered":"<p>[et_pb_section fb_built=&#8221;1&#8243; admin_label=&#8221;section&#8221; _builder_version=&#8221;3.22&#8243; fb_built=&#8221;1&#8243; bb_built=&#8221;1&#8243; _i=&#8221;0&#8243; _address=&#8221;0&#8243;][et_pb_row admin_label=&#8221;row&#8221; _builder_version=&#8221;3.25&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; _i=&#8221;0&#8243; _address=&#8221;0.0&#8243;][et_pb_column type=&#8221;4_4&#8243; _builder_version=&#8221;3.25&#8243; custom_padding=&#8221;|||&#8221; _i=&#8221;0&#8243; _address=&#8221;0.0.0&#8243; custom_padding__hover=&#8221;|||&#8221;][et_pb_text _builder_version=&#8221;3.27.4&#8243; background_size=&#8221;initial&#8221; background_position=&#8221;top_left&#8221; background_repeat=&#8221;repeat&#8221; use_border_color=&#8221;off&#8221; border_color=&#8221;#ffffff&#8221; border_style=&#8221;solid&#8221; _i=&#8221;0&#8243; _address=&#8221;0.0.0.0&#8243;]<\/p>\n<p>&nbsp;<\/p>\n<h1>How To Code a Responsive Navigation<\/h1>\n<h2>What this post is about<\/h2>\n<p>This post is about creating responsive navigation for any website using HTML, CSS and a bit of javaScript. In the end, there will only be 30 lines of jQuery to make it all work. Below there is a video that you can follow along and code while you are watching. The code blocks are available below so you can just cut &amp; paste into your favourite editor.<\/p>\n<p><strong>Keep on Coding!<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>[\/et_pb_text][\/et_pb_column][\/et_pb_row][et_pb_row _builder_version=&#8221;3.27.3&#8243; _i=&#8221;1&#8243; _address=&#8221;0.1&#8243;][et_pb_column type=&#8221;4_4&#8243; _builder_version=&#8221;3.27.3&#8243; _i=&#8221;0&#8243; _address=&#8221;0.1.0&#8243;][et_pb_video src=&#8221;https:\/\/youtu.be\/IlWqo7HhR34?rel=0&#8243; _builder_version=&#8221;3.27.3&#8243; _i=&#8221;0&#8243; _address=&#8221;0.1.0.0&#8243;][\/et_pb_video][\/et_pb_column][\/et_pb_row][\/et_pb_section][et_pb_section fb_built=&#8221;1&#8243; _builder_version=&#8221;3.27.3&#8243; fb_built=&#8221;1&#8243; _i=&#8221;1&#8243; _address=&#8221;1&#8243;][et_pb_row _builder_version=&#8221;3.27.3&#8243; _i=&#8221;0&#8243; _address=&#8221;1.0&#8243;][et_pb_column type=&#8221;4_4&#8243; _builder_version=&#8221;3.27.3&#8243; _i=&#8221;0&#8243; _address=&#8221;1.0.0&#8243;][et_pb_text _builder_version=&#8221;3.27.4&#8243; _i=&#8221;0&#8243; _address=&#8221;1.0.0.0&#8243;]<\/p>\n<h2>The Finished Code<\/h2>\n<h3>File Structure<\/h3>\n<p>Just so you can follow along a bit easier this is the file structure I used<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1493\" src=\"https:\/\/www.harrisweb.ca\/blog\/wp-content\/uploads\/2019\/08\/file-structure-420x293.jpg\" alt=\"file structure for responsive navigation | HarrisWeb Creative\" width=\"350\" height=\"244\" \/><\/p>\n<h3>HTML Code<\/h3>\n<p>There isn&#8217;t much to say about this. It is a index.php and a nav.php, pretty standard.<\/p>\n<h4>index.php<\/h4>\n<p>[\/et_pb_text][et_pb_text admin_label=&#8221;CodeBlock&#8221; module_class=&#8221;codeblock&#8221; _builder_version=&#8221;3.29.3&#8243; text_font=&#8221;||||||||&#8221; text_font_size=&#8221;14px&#8221; hover_enabled=&#8221;0&#8243; _i=&#8221;1&#8243; _address=&#8221;1.0.0.1&#8243;]<\/p>\n<pre>\n&lt;html&gt;\n&lt;head&gt;\n\t&lt;title&gt;Responsive Navigation&lt;\/title&gt;\t\n\t&lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"css\/style.css\"&gt;\t\n\t&lt;script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.12.4\/jquery.min.js\"&gt;&lt;\/script&gt;\n\t&lt;script src=\"js\/navScript.js\"&gt;&lt;\/script&gt;\t\n&lt;\/head&gt;\n&lt;body&gt;\n\t&lt;nav&gt;\n\t\t&lt;?php include 'nav.php' ; ?&gt;\n\t&lt;\/nav&gt;\n\t&lt;div class=\"spacer\"&gt;&lt;\/div&gt;\n\t\t&lt;h1&gt;Responsive Navigation&lt;\/h1&gt;\t\n\t&lt;section&gt;\n\tLorem ipsum dolor sit amet, consectetur adipiscing elit,\n\tJust put a bunch of fake text in here\n\t&lt;\/section&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;3.27.4&#8243; _i=&#8221;2&#8243; _address=&#8221;1.0.0.2&#8243;]<\/p>\n<h4>nav.php<\/h4>\n<p>[\/et_pb_text][et_pb_text admin_label=&#8221;CodeBlock&#8221; module_class=&#8221;codeblock&#8221; _builder_version=&#8221;3.27.4&#8243; text_font=&#8221;||||||||&#8221; text_font_size=&#8221;14px&#8221; _i=&#8221;3&#8243; _address=&#8221;1.0.0.3&#8243;]<\/p>\n<pre>\n&lt;ul&gt;\n\t&lt;li id=\"mobile-open\"&gt;&lt;button&gt;MENU&lt;\/button&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=\"index.php\"&gt;Home&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=\"page1.php\"&gt;Page 1&lt;\/a&gt;\n\t\t&lt;ul&gt;\n\t\t\t&lt;li&gt;&lt;a href=\"page1a.php\"&gt;Page 1a&lt;\/a&gt;&lt;\/li&gt;\n\t\t&lt;\/ul&gt;\n\t&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=\"#\"&gt;Page 2&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=\"#\"&gt;Page 3&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li&gt;&lt;a href=\"#\"&gt;Page 4&lt;\/a&gt;&lt;\/li&gt;\n\t&lt;li id=\"mobile-close\"&gt;&lt;button&gt;Close Menu&lt;\/button&gt;&lt;\/li&gt;\n\t\n&lt;\/ul&gt;\n<\/pre>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;3.27.4&#8243; _i=&#8221;4&#8243; _address=&#8221;1.0.0.4&#8243;]<\/p>\n<h3>CSS stylesheet<\/h3>\n<p>[\/et_pb_text][et_pb_text admin_label=&#8221;CodeBlock&#8221; module_class=&#8221;codeblock&#8221; _builder_version=&#8221;3.29.3&#8243; text_font=&#8221;||||||||&#8221; text_font_size=&#8221;14px&#8221; _i=&#8221;5&#8243; _address=&#8221;1.0.0.5&#8243;]<\/p>\n<pre>*{box-sizing:border-box}\n\n\nbody{margin:0; padding:0; font-family:'Arial',san-serif; font-size:1vw; color:#000000;} \n\n\nnav{position:fixed; background:linear-gradient(#000, #666);<br \/>    color:#fff; width:100%; margin:0; padding:0; z-index:+500;}<br \/>\nnav ul{list-style:none; text-align:center; margin:0; padding:0}\nnav ul li{display:inline-block; padding:1.15vw 1vw; margin:0}\nnav ul li a{color:#fff; text-decoration:none; padding:1em 2px; }\nnav ul li:hover{background:#666}\n#mobile-open, #mobile-close{display:none}\nnav &gt; ul &gt; li.active{background:#000;}\nnav ul li ul{position:absolute; top:4vw; }\nnav ul li ul li{display:none; background:#333; border-bottom:solid 2px #ccc}\nnav ul li ul li:hover{background:#999}\n\n.spacer{height:5vw; width:100%}\n\n\nsection{width:33%; display:block; margin: 20px auto; }\n\n\n@media only screen and (max-width:800px){\n\tbody{font-size:16px; }\n\tnav ul li{margin:0; padding:15px 0; bottom-border:1px solid #ccc; width:100%}\n\tnav ul li{display:none}\n\tnav ul{background: #333}\n\t#mobile-open{display:block; }\n\t\n}\n\n<\/pre>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;3.27.4&#8243; _i=&#8221;6&#8243; _address=&#8221;1.0.0.6&#8243;]<\/p>\n<h3>jQuery Script<\/h3>\n<p>I probably didn&#8217;t need the <strong>$(document).ready()<\/strong> on each one of these scripts, just the first one.<\/p>\n<p>[\/et_pb_text][et_pb_text admin_label=&#8221;CodeBlock&#8221; module_class=&#8221;codeblock&#8221; _builder_version=&#8221;3.27.4&#8243; text_font=&#8221;||||||||&#8221; text_font_size=&#8221;14px&#8221; _i=&#8221;7&#8243; _address=&#8221;1.0.0.7&#8243;]<\/p>\n<pre>\n$(document).ready(function(){\n\tvar pgurl =window.location.href.substr(window.location.href.lastIndexOf(\"\/\")+1);\n\t\n\t$(\"nav ul li a\").each(function(){\n\t\tif($(this).attr(\"href\") == pgurl || $(this).attr(\"href\") == ''){\n\t\t\t$(this).parent().addClass(\"active\");\n\t\t}\n\t})\n});\n\n$(document).ready(function(){\n\t$(\"#mobile-open\").click(function(){\n\t\t$(\"nav ul li\").css(\"display\",\"block\");\n\t});\n\t$(\"#mobile-close\").click(function(){\n\t\t$(\"#mobile-open\").siblings().css(\"display\",\"none\")\n\t});\n});\n\n$(document).ready(function(){\n  var x = (window.innerWidth);\n\tif(x > 800){\n\t\t$(\"nav ul li\").hover(\n\t\t\tfunction(){\n\t\t\t\t$(this).find(\"ul li\").css(\"display\",\"block\");\n\t\t\t},\n\t\t\tfunction(){\n\t\t\t\t$(this).find(\"ul li\").css(\"display\",\"none\");\n\t\t\t}\n\t\t)\n\t}\n});\n\n<\/pre>\n<p>[\/et_pb_text][et_pb_text _builder_version=&#8221;3.27.4&#8243; _i=&#8221;8&#8243; _address=&#8221;1.0.0.8&#8243;]<\/p>\n<p>That&#8217;s it folks, pretty simple. hope this helped you out.<\/p>\n<p>[\/et_pb_text][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; How To Code a Responsive Navigation What this post is about This post is about creating responsive navigation for any website using HTML, CSS and a bit of javaScript. In the end, there will only be 30 lines of jQuery to make it all work. Below there is a video that you can follow [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1484,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[20],"tags":[],"class_list":["post-1481","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-website-design"],"_links":{"self":[{"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/posts\/1481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/comments?post=1481"}],"version-history":[{"count":26,"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/posts\/1481\/revisions"}],"predecessor-version":[{"id":1611,"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/posts\/1481\/revisions\/1611"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/media\/1484"}],"wp:attachment":[{"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/media?parent=1481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/categories?post=1481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.harrisweb.ca\/blog\/wp-json\/wp\/v2\/tags?post=1481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}