{"id":3073,"date":"2017-07-25T11:26:16","date_gmt":"2017-07-25T16:26:16","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=2384"},"modified":"2017-07-25T11:26:16","modified_gmt":"2017-07-25T16:26:16","slug":"create-responsive-navigation-bars-for-desktop-and-mobile-websites","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/create-responsive-navigation-bars-for-desktop-and-mobile-websites\/","title":{"rendered":"Create Responsive Navigation Bars for Desktop and Mobile Websites"},"content":{"rendered":"<p>With minimalist side design still on the rise, it\u2019s sometimes easy for website visitors to get confused. Navigating a website should be easy and intuitive, and is one of the most important elements of a website. Using CSS, you can turn simple HTML menus into dynamic and aesthetically pleasing navigation bars.<\/p>\n<h2>Responsive Design<\/h2>\n<p>As the majority of people today use their mobile gadgets like cellphones and tablets to go online, it also goes without saying that your site\u2019s design should look good on both desktops and mobile devices.<\/p>\n<p>If you are <a href=\"http:\/\/cssreset.com\/css-learning-resources-books\/\">new to web design<\/a> and the concept of \u201cresponsive\u201d design seems foreign to you, basically it refers to your designs ability to \u201cflow\u201d as you resize the screen or move to a device with a smaller width. Responsive design is built to work on both big and small screens, and this includes the navigation or menu bar visitors use to access other pages of your site.<\/p>\n<h2>Navigation Bar Skeleton<\/h2>\n<p>Just like many of our tutorials, you will have to combine HTML knowledge with CSS techniques to successfully build and design interesting websites. A navigation bar, in this case, will need a basic HTML skeleton that lists the different links you want to list.<\/p>\n<p>Remember the unordered list &lt;ul&gt; and list item &lt;li&gt; HTML tags? We will use that as the skeleton.<\/p>\n<pre>&lt;ul&gt;\n\n&lt;li&gt;&lt;a href=\"#home\"&gt;Home&lt;\/a&gt;&lt;\/li&gt;\n\n&lt;li&gt;&lt;a href=\"#products \"&gt;Products&lt;\/a&gt;&lt;\/li&gt;\n\n&lt;li&gt;&lt;a href=\"#company \"&gt;Company&lt;\/a&gt;&lt;\/li&gt;\n\n&lt;li&gt;&lt;a href=\" #contact \"&gt;Contact&lt;\/a&gt;&lt;\/li&gt;\n\n&lt;\/ul&gt;<\/pre>\n<p>The best part about this is that you will only need to update the CSS for changes to appear on your menu \u2013 no further tweaking in the HTML body is necessary!<\/p>\n<h2>Formatting the Links List<\/h2>\n<p>So how do we turn a simple bulleted links list into something like this?<\/p>\n<p><a href=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2017\/07\/Screen-Shot-2017-07-24-at-11.28.04-AM.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-2416\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2017\/07\/Screen-Shot-2017-07-24-at-11.28.04-AM.png\" alt=\"\" width=\"600\" height=\"44\" \/><\/a><br \/>\nFirst, using CSS (which we will either enter between the &lt;style&gt; tags in the &lt;head&gt; or in an external CSS file), we can remove the bullets and margins\/padding that automatically formats a list by default:<\/p>\n<pre>ul {\n\nlist-style-type: none;\n\nmargin: 0;\n\npadding: 0;\n\n}<\/pre>\n<p>This code will work for both vertical and horizontal navigation bars.<\/p>\n<h2>Formatting into a Vertical Navigation Bar<\/h2>\n<p>We will now further modify the above CSS so that the &lt;a&gt; element will also have a customized style:<\/p>\n<pre>li a {\n\ndisplay: block;\n\nwidth: 60px;\n\n}<\/pre>\n<p><strong>display: block;<\/strong>\u00a0turns the links into block-type elements, making the entire block area clickable instead of just the text. Above, we modified only the width, but you can also modify other elements such as margin, height, padding, colors, and many other aspects.<\/p>\n<h2>Formatting Into a Horizontal Navigation Bar<\/h2>\n<p>There are two ways to modify the basic CSS to create a horizontal navigation bar \u2013 either <strong>inline<\/strong> or <strong>floating<\/strong>.<\/p>\n<h3>Inline Horizontal Nav Bar<\/h3>\n<p>When you edit the &lt;li&gt; element\u2019s CSS style as in this example, you are telling the browser to get rid of the line breaks that format each list item, so they display horizontally in one line.<\/p>\n<pre>li {\n\ndisplay: inline;\n\n}<\/pre>\n<h3>Floating Horizontal Nav Bar<\/h3>\n<p>The second method of creating a horizontal navigation bar is to make the elements float:<\/p>\n<pre>li {\n\nfloat: left;\n\n}\n\n\u00a0\n\na {\n\ndisplay: block;\n\npadding: 8px;\n\nbackground-color: #dddddd;\n\n}<\/pre>\n<p>Using float, the individual block elements or list items will slide next to each other.<\/p>\n<h2>Specifying the \u201cActive\u201d Link<\/h2>\n<p>You can use a nifty CSS trick so the visitor knows which page on your site they are on:<\/p>\n<pre>.active {\n\nbackground-color: #4CAF50;\n\n}<\/pre>\n<p>The <strong>.active<\/strong> class is now a different color than the rest of the menu when the visitor is on a specific page of your website.<\/p>\n<h2>Fixed Navigation Bar<\/h2>\n<p>You can make the nav bar stay on the top or bottom of your page, even when the visitor scrolls the page, using one of the following codes:<\/p>\n<h5><em>For fixed top navigation bars, use:<\/em><\/h5>\n<pre>ul {\n\nposition: fixed;\n\ntop: 0;\n\nwidth: 100%;\n\n}<\/pre>\n<h5><em>For fixed bottom navigation bars, use:<\/em><\/h5>\n<pre>ul {\n\nposition: fixed;\n\nbottom: 0;\n\nwidth: 100%;\n\n}<\/pre>\n<h2>Responsive Navigation Bars<\/h2>\n<p>You can use CSS media queries so your top or side navigation bars will become responsive and would work on mobile devices like a tablet or cellphone. Just <a href=\"http:\/\/cssnewbie.com\/how-to-use-css3s-width-and-height-properties\/\">specify the widths and heights<\/a> as in the codes below.<\/p>\n<h5><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-2391 size-full\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2017\/07\/navbar_responsive_ver.jpg\" alt=\"navbar_responsive_ver\" width=\"1184\" height=\"562\" \/><br \/>\n<em>For responsive top navigation bars, use:<\/em><\/h5>\n<pre>ul.topnav {\n\nlist-style-type: none;\n\nmargin: 0;\n\npadding: 0;\n\noverflow: hidden;\n\nbackground-color: #333;\n\n}\n\n\u00a0\n\nul.topnav li {float: left;}\n\n\u00a0\n\nul.topnav li a {\n\ndisplay: block;\n\ncolor: white;\n\ntext-align: center;\n\npadding: 14px 16px;\n\ntext-decoration: none;\n\n}\n\n\u00a0\n\nul.topnav li a:hover:not(.active) {background-color: #111;}\n\n\u00a0\n\nul.topnav li a.active {background-color: #4CAF50;}\n\n\u00a0\n\nul.topnav li.right {float: right;}\n\n\u00a0\n\n@media screen and (max-width: 600px){\n\nul.topnav li.right,\n\nul.topnav li {float: none;}\n\n}<\/pre>\n<h5><em>For responsive side navigation bars, use:<\/em><\/h5>\n<pre>ul.sidenav {\n\nlist-style-type: none;\n\nmargin: 0;\n\npadding: 0;\n\nwidth: 25%;\n\nbackground-color: #f1f1f1;\n\nposition: fixed;\n\nheight: 100%;\n\noverflow: auto;\n\n}\n\n\u00a0\n\nul.sidenav li a {\n\ndisplay: block;\n\ncolor: #000;\n\npadding: 8px 16px;\n\ntext-decoration: none;\n\n}\n\n\u00a0\n\nul.sidenav li a.active {\n\nbackground-color: #4CAF50;\n\ncolor: white;\n\n}\n\n\u00a0\n\nul.sidenav li a:hover:not(.active) {\n\nbackground-color: #555;\n\ncolor: white;\n\n}\n\n\u00a0\n\ndiv.content {\n\nmargin-left: 25%;\n\npadding: 1px 16px;\n\nheight: 1000px;\n\n}\n\n\u00a0\n\n@media screen and (max-width: 900px) {\n\nul.sidenav {\n\nwidth: 100%;\n\nheight: auto;\n\nposition: relative;\n\n}\n\nul.sidenav li a {\n\nfloat: left;\n\npadding: 15px;\n\n}\n\ndiv.content {margin-left: 0;}\n\n}\n\n\u00a0\n\n@media screen and (max-width: 400px) {\n\nul.sidenav li a {\n\ntext-align: center;\n\nfloat: none;\n\n}<\/pre>\n<h2>Conclusion<\/h2>\n<p>There you have it! Don\u2019t forget to have fun experimenting with colors, borders, and other cool tricks you have learned.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>With minimalist side design still on the rise, it\u2019s sometimes easy for website visitors to get confused. Navigating a website should be easy and intuitive, and is one of the most important elements of a website. Using CSS, you can [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/create-responsive-navigation-bars-for-desktop-and-mobile-websites\/\" title=\"Click to read 'Create Responsive Navigation Bars for Desktop and Mobile Websites'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":2933,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[172,185,192,193,194,195],"tags":[214,306,156,335,336],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3073"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=3073"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/3073\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/2933"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=3073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=3073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=3073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}