{"id":829,"date":"2010-07-28T10:21:05","date_gmt":"2010-07-28T09:21:05","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?page_id=829"},"modified":"2011-06-13T19:04:10","modified_gmt":"2011-06-13T18:04:10","slug":"css-horizontal-menu-with-dropdowns","status":"publish","type":"page","link":"https:\/\/www.simonbattersby.com\/blog\/css-horizontal-menu-with-dropdowns\/","title":{"rendered":"CSS horizontal menu with dropdowns"},"content":{"rendered":"<p>I was looking for a simple tutorial for a css-only horizontal dropdown menu earlier this week, aimed at a css beginner. And I couldn&#8217;t find one, so here&#8217;s how to build this menu&#8230;.<\/p>\r\n\r\n<ul class=\"horiznav clearfix\">\r\n<li><a href=\"#\">Item 1<\/a><\/li>\r\n<li><a href=\"#\">Item 2<\/a>\r\n     <ul>\r\n     <li><a href=\"#\">Item 2.1<\/a><\/li>\r\n     <li><a href=\"#\">Item 2.2<\/a><\/li>\r\n     <\/ul>\r\n<\/li>\r\n<li><a href=\"#\">Item 3<\/a>\r\n     <ul>\r\n     <li><a href=\"#\">Item 3.1<\/a><\/li>\r\n     <li><a href=\"#\">Item 3.2<\/a><\/li> \r\n     <li><a href=\"#\">Item 3.3<\/a><\/li>\r\n     <\/ul>\r\n<\/li>\r\n<li><a href=\"#\">Item 4<\/a>\r\n     <ul>\r\n     <li><a href=\"#\">Item 4.1<\/a><\/li>\r\n     <li><a href=\"#\">Item 4.2<\/a><\/li>\r\n     <li><a href=\"#\">Item 4.3<\/a><\/li>\r\n     <\/ul>\r\n<\/li>\r\n<\/ul>\r\n\r\n<p> I&#8217;m going to do this in two stages:<\/p>\r\n\r\n<h3>Creating a simple horizontal menu &#8211; no dropdown<\/h3>\r\n\r\n<p>Before we get into the dropdowns, let&#8217;s just sort the horizontal menu bit. Let&#8217;s start with the html markup, which is a standard unordered list. Let&#8217;s have four menu items:<\/p>\r\n\r\n<pre>&lt;ul id=&quot;horiznav&quot;&gt;\r\n&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 1&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 2&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 3&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 4&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;\/ul&gt;<\/pre>\r\n\r\n<p>Nothing too exciting there. Now let&#8217;s have a look at the css:<\/p>\r\n\r\n<pre>ul#horiznav{\r\nmargin:0;\r\npadding:0;<span class=\"code_comment\">\/*set both to zero to remove browser inconsistencies*\/<\/span>\r\nlist-style-type:none;<span class=\"code_comment\">\/*get rid of bullet points*\/<\/span>\r\nheight:32px<span class=\"code_comment\">\/*just to avoid some possible float issues*\/<\/span>\r\n}\r\n\r\n#horiznav li{\r\nfloat:left;<span class=\"code_comment\">\/*float the li element so the menu's horizontal...*\/<\/span>\r\nwidth:150px<span class=\"code_comment\">\/*...and set the width*\/<\/span>\r\n}\r\n\r\n#horiznav li a{\r\ndisplay:block;<span class=\"code_comment\">\/*make the link a block element...*\/<\/span>\r\nwidth:150px;<span class=\"code_comment\">\/*...with a fixed width...*\/<\/span>\r\nline-height:30px;<span class=\"code_comment\">\/*...and set the line-height to vertically centre the text*\/<\/span>\r\ntext-align:center;<span class=\"code_comment\">\/*horizontally centre the text...*\/<\/span>\r\ncolor:white;<span class=\"code_comment\">\/*...colour it white...*\/<\/span>\r\ntext-decoration:none;<span class=\"code_comment\">\/*...and remove the default underline*\/<\/span>\r\nbackground-color:#EA9531;<span class=\"code_comment\">\/*give the link an orange background...*\/<\/span>\r\nborder:1px solid white<span class=\"code_comment\">\/*...and a white border...*\/<\/span>\r\n}\r\n\r\n#horiznav li a:hover{\r\ncolor:#333333<span class=\"code_comment\">\/*change the text colour on :hover*\/<\/span>\r\n}\r\n<\/pre>\r\n\r\n<p>See comments in the css for an explanation. The key thing that&#8217;s making the menu horizontal is floating the <code>&lt;li&gt;<\/code> element. This code gives us this:<\/p>\r\n\r\n<ul class=\"horiznav clearfix\">\r\n<li><a href=\"#\">Item 1<\/a><\/li>\r\n<li><a href=\"#\">Item 2<\/a><\/li>\r\n<li><a href=\"#\">Item 3<\/a><\/li>\r\n<li><a href=\"#\">Item 4<\/a><\/li>\r\n<\/ul>\r\n\r\n<h3 class=\"clearboth\">Adding the drop down<\/h3>\r\n\r\n<p>Now we&#8217;ll add the drop down. The dropdown elements are included as additional <code>&lt;ul&gt;<\/code> elements nested within the appropriate <code>&lt;li&gt;<\/code> element. Like this:<\/p>\r\n\r\n<pre>&lt;ul id=&quot;horiznav&quot;&gt;\r\n&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 1&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 2&lt;\/a&gt;\r\n     &lt;ul&gt;\r\n     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 2.1&lt;\/a&gt;&lt;\/li&gt;\r\n     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 2.2&lt;\/a&gt;&lt;\/li&gt;\r\n     &lt;\/ul&gt;\r\n&lt;\/li&gt;\r\n&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 3&lt;\/a&gt;\r\n     &lt;ul&gt;\r\n     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 3.1&lt;\/a&gt;&lt;\/li&gt;\r\n     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 3.2&lt;\/a&gt;&lt;\/li&gt; \r\n     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 3.3&lt;\/a&gt;&lt;\/li&gt;\r\n     &lt;\/ul&gt;\r\n&lt;\/li&gt;\r\n&lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 4&lt;\/a&gt;\r\n     &lt;ul&gt;\r\n     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 4.1&lt;\/a&gt;&lt;\/li&gt;\r\n     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 4.2&lt;\/a&gt;&lt;\/li&gt;\r\n     &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Item 4.3&lt;\/a&gt;&lt;\/li&gt;\r\n     &lt;\/ul&gt;\r\n&lt;\/li&gt;\r\n&lt;\/ul&gt;<\/pre>\r\n\r\n<p>Right, now for the css. We need to make a few additions &#8211; marked in red:<\/p>\r\n\r\n<pre>\r\nul#horiznav, <span class=\"code_highlight\">#horiznav ul<\/span>{<span class=\"code_comment\">\/*remove the bullets from the dropdown ul as well*\/<\/span>\r\nmargin:0;\r\npadding:0;\r\nlist-style-type:none;\r\nheight:32px\r\n}\r\n\r\n#horiznav li{\r\nfloat:left;\r\nwidth:152px;\r\n<span class=\"code_highlight\">position:relative<\/span><span class=\"code_comment\">\/*set position:relative as the start point for absolutely positioning the dropdown*\/<\/span>\r\n}\r\n\r\n#horiznav li a{\r\ndisplay:block;\r\nwidth:150px;\r\nline-height:30px;\r\ntext-align:center;\r\ncolor:white;\r\ntext-decoration:none;\r\nbackground-color:#EA9531;\r\nborder:1px solid white\r\n}\r\n\r\n#horiznav li a:hover{\r\ncolor:#333333\r\n}\r\n\r\n<span class=\"code_highlight\">#horiznav li ul{\r\ndisplay:none;<span class=\"code_comment\">\/*hide the dropdown*\/<\/span>\r\nposition:absolute;<span class=\"code_comment\">\/*position it absolutely..*\/<\/span>\r\nleft:0;<span class=\"code_comment\">\/*...align the left edge with the left edge of the parent li...*\/<\/span>\r\ntop:32px<span class=\"code_comment\">\/*...and 32px down from the top - 30px height + 2px for the border*\/<\/span>\r\n}\r\n\r\n#horiznav li:hover ul {\r\ndisplay:block<span class=\"code_comment\">\/*display the ul when the parent li is hovered*\/<\/span>\r\n}\r\n\r\n#horiznav li ul a{\r\nbackground-color:#FFB33B<span class=\"code_comment\">\/*give the dropdown a different background colour*\/<\/span>\r\n}<\/span><\/pre>\r\n\r\n<p>See the comments in the code for explanations. A surprisingly small amount of css, really. The dropdowns are normally hidden, and appear when the parent <code>&lt;li&gt;<\/code> is hovered. So this gives us (same as at the top of the page):<\/p>\r\n\r\n<ul class=\"horiznav clearfix\">\r\n<li><a href=\"#\">Item 1<\/a><\/li>\r\n<li><a href=\"#\">Item 2<\/a>\r\n     <ul>\r\n     <li><a href=\"#\">Item 2.1<\/a><\/li>\r\n     <li><a href=\"#\">Item 2.2<\/a><\/li>\r\n     <\/ul>\r\n<\/li>\r\n<li><a href=\"#\">Item 3<\/a>\r\n     <ul>\r\n     <li><a href=\"#\">Item 3.1<\/a><\/li>\r\n     <li><a href=\"#\">Item 3.2<\/a><\/li> \r\n     <li><a href=\"#\">Item 3.3<\/a><\/li>\r\n     <\/ul>\r\n<\/li>\r\n<li><a href=\"#\">Item 4<\/a>\r\n     <ul>\r\n     <li><a href=\"#\">Item 4.1<\/a><\/li>\r\n     <li><a href=\"#\">Item 4.2<\/a><\/li>\r\n     <li><a href=\"#\">Item 4.3<\/a><\/li>\r\n     <\/ul>\r\n<\/li>\r\n<\/ul>\r\n\r\n<p>You&#8217;ll see that the use of absolute positioning makes the dropdown appear on top of anything else on the page &#8211; which is what we want. And this works fine in pretty much every browser in popular use today. Specifically I&#8217;ve tested in IE7, IE8, FF3, Chrome, Opera9, Opera 10, Safari4. You&#8217;ll spot there&#8217;s something missing. No prizes for guessing what browser causes a little extra difficulty&#8230;<\/p>\r\n\r\n<h3>Dealing with the dropdown in IE6<\/h3>\r\n<p>IE6 doesn&#8217;t support :hover, except on <code>&lt;a&gt;<\/code> elements. So without a bit of extra code, our nice dropdown won&#8217;t work at all in IE6. Fortunately, there&#8217;s a solution called csshover, which you\u2019ll find <a title=\"csshover widget\"href=\"http:\/\/www.xs4all.nl\/~peterned\/csshover.html\">here<\/a> along with instructions on how to use it. I&#8217;ve personally found version 2 of this software to be the most reliable.<\/p>\r\n\r\n<h3>Adding animation with jQuery<\/h3>\r\n<p>To add some animation &#8211; fades, slides etc &#8211; have a look at my jQuery plugin <a href=\"\/blog\/jquery-dropdown-menu-plugin-with-css-fallback\/\" title=\"jQuery dropdown plugin\">here<\/a>, which allows more sophisticated animation for javascript users while preserving the css approach for those without.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>I was looking for a simple tutorial for a css-only horizontal dropdown menu earlier this week, aimed at a css beginner. And I couldn&#8217;t find one, so here&#8217;s how to build this menu&#8230;. Item 1 Item 2 Item 2.1 Item 2.2 Item 3 Item 3.1 Item 3.2 Item 3.3 Item 4 Item 4.1 Item 4.2 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-829","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/comments?post=829"}],"version-history":[{"count":0,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/829\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}