{"id":125,"date":"2008-05-20T14:24:03","date_gmt":"2008-05-20T19:24:03","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=125"},"modified":"2008-05-20T14:24:03","modified_gmt":"2008-05-20T19:24:03","slug":"horizontal-dropdown-menus","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/horizontal-dropdown-menus\/","title":{"rendered":"Horizontal CSS Dropdown Menus"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/05\/horizontalmenu-400.gif\" alt=\"\"  width=\"400\" height=\"62\" class=\"alignnone size-full wp-image-129\" \/><\/p>\n<p>Last week, CSS Newbie reader <a href=\"http:\/\/www.andreapluhar.com\/\">Andrea Pluhar<\/a> wrote in with an interesting problem: she wanted to use <a href=\"http:\/\/cssnewbie.com\/easy-css-dropdown-menu\/\">CSS dropdown menus like the ones we featured last week<\/a> on a website that she was building, but the design called for the submenu to be arranged horizontally, not vertically. She sent me a mockup of what she was after (excerpted above) and wondered if there was a way to accomplish this effect using CSS. It turns out that there <em>is<\/em> a CSS-riffic way to do this, and in the spirit of maximizing benefit, I thought a tutorial would be in order. <\/p>\n<p>The XHTML involved is identical to that used in our regular dropdown menus: a nested unordered list, where the nested lists become the submenus. It looks something like this:<\/p>\n<pre lang=\"html4strict\" escaped=\"true\" line=\"1\">&lt;ul id=&quot;navbar&quot;&gt;\n\t&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item One&lt;\/a&gt;&lt;ul&gt;\n\t\t&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Subitem One&lt;\/a&gt;&lt;\/li&gt;\n\t\t&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Second Subitem&lt;\/a&gt;&lt;\/li&gt;\n\t\t&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Numero Tres&lt;\/a&gt;&lt;\/li&gt;&lt;\/ul&gt;\n\t&lt;\/li&gt;\n\t&lt;!-- ... and so on ... --&gt;\n&lt;\/ul&gt;<\/pre>\n<p>Next we\u2019ll move to the CSS. I started out by moving the navigation bar to the top-right corner, like the design called for, removing the list styling, and floating the items left to make them line up in a row:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#navbar {\n\tposition: absolute;\n\ttop: 0;\n\tright: 0;\n\tmargin: 0;\n\tpadding: 0;}\n#navbar li {\n\tlist-style: none;\n\tfloat: left; }<\/pre>\n<p>Next, I styled the primary anchor tags to make them look more like the navigation Andrea was looking for. The code looks like this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#navbar li a {\n\tdisplay: block;\n\tpadding: 3px 8px;\n\ttext-transform: uppercase;\n\ttext-decoration: none; \n\tcolor: #999;\n\tfont-weight: bold; }\n#navbar li a:hover {\n\tcolor: #000; }<\/pre>\n<p>I\u2019ve added a bit of padding to the link, and used the text-transform property to make everything uppercase like the mockup called for. That way, the original XHTML can be lowercase or camel-case (capitalized first letters)&#8230; which would be a little easier to read in an unstyled document.<\/p>\n<p>Next up, we hide the nested lists by default, and then style them when the containing list item is hovered over:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#navbar li ul {\n\tdisplay: none;  }\n#navbar li:hover ul, #navbar li.hover ul {\n\tposition: absolute;\n\tdisplay: inline;\n\tleft: 0;\n\twidth: 100%;\n\tmargin: 0;\n\tpadding: 0; }<\/pre>\n<p>The code above is the bit that really makes most of this magic work, so I\u2019ll explain the important parts in some detail. First, because IE6 doesn\u2019t support hover states on anything other than anchor tags, we\u2019re writing our rules to account for the hover state <strong>and<\/strong> a hover class. This class is applied to elements when they&#8217;re being hovered over, using an ingenious little bit of JavaScript (which is <a href=\"\/easy-css-dropdown-menu\/\">explained in this previous dropdown menu tutorial<\/a>).<\/p>\n<p>Next up, we\u2019re absolutely positioning our nested lists and using the \u201cleft\u201d property to move the list to the left-most side. This isn\u2019t moving the list to the left-most side of the screen, but instead the left-most side of <em>its parent positioned element<\/em>, which in this case happens to be the main unordered list that we positioned right at the start. As such, this trick relies of the whole list being positioned in some manner, even if it\u2019s just relatively positioned and left in place.<\/p>\n<p>The display: inline rule is a little more complicated. So much so, I don\u2019t even completely understand what it\u2019s doing. What I <em>do<\/em> know is, without that rule, the list items in the submenus simply don\u2019t show up whatsoever in any major browser. I <em>think<\/em> it has something to do with the fact that the containing elements are floated (which we\u2019ll get to in a bit), but I can\u2019t prove that. If anyone has any better insight into the technical aspect, please let me know in the comments.<\/p>\n<p>Lastly, the width: 100% rule is somewhat important. It\u2019s preventing the unordered list from collapsing down to a smaller size in certain browsers. Specifically, without setting this width specified, the nested list sometimes collapses to the size of its \u201ccontaining\u201d list item (even though it\u2019s absolutely positioned and therefore technically no longer contained). Note that older versions of Opera don\u2019t deal well with the 100% width&#8230; if you want it to work on older versions, you\u2019ll need to specify a width according to a definite size (such as pixels). However, the most recent version of Opera (9.27) handles it fine, and I get the impression that Opera users tend to upgrade more frequently than, say, IE users.<\/p>\n<p>Finally, we just float the elements left (to put them in a nice horizontal row), and give them some colors:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">#navbar li:hover li, #navbar li.hover li {\n\tfloat: left; }\n#navbar li:hover li a, #navbar li.hover li a {\n\tcolor: #000; }\n#navbar li li a:hover {\n\tcolor: #357; }<\/pre>\n<p>And that\u2019s it! <a href=\"\/example\/css-dropdown-menu\/horizontal.html\">You can see a working example here.<\/a> This has been tested and works fine in IE 6+, Firefox 2, Safari (Mac and PC), and Opera 9.27. <\/p>\n<p>The only portion of the mockup I wasn\u2019t quite able to duplicate was a way to keep the primary menu item highlighted when the submenu was in use: because the nested list is absolutely positioned, the browser doesn\u2019t seem to consider them a matched set any longer (except, it seems, in terms of default width). If anyone has a solution to this bit, I would love to hear about it!<\/p>\n<p>Thanks to Andrea for inspiring a hopefully useful tutorial! And if you ever have a CSS-related question that you think might make a good article here, don\u2019t hesitate to send me a message, either <a href=\"\/contact\/\">via my contact page<\/a> or <a href=\"http:\/\/www.twitter.com\/robbyg\/\">on Twitter.<\/a> I can\u2019t guarantee I\u2019ll use every question posed, but I\u2019ll do what I can as time and situation allow.<\/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>CSS Newbie reader Andrea Pluhar wrote in with an interesting problem: she wanted to use a CSS dropdown menu on a website that she was building, but the design called for the submenu to be arranged horizontally. She wondered if there was a way to accomplish this effect using CSS. It turns out that there is. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/horizontal-dropdown-menus\/\" title=\"Click to read 'Horizontal CSS Dropdown Menus'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[165,188],"tags":[79,245,251,88,382],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/125"}],"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=125"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/125\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}