{"id":107,"date":"2009-07-19T08:31:41","date_gmt":"2009-07-19T07:31:41","guid":{"rendered":"http:\/\/www.simonbattersby.com\/blog\/?page_id=107"},"modified":"2012-10-29T08:14:34","modified_gmt":"2012-10-29T08:14:34","slug":"building-a-css-vertical-expanding-menu-with-flyouts-stage-1","status":"publish","type":"page","link":"https:\/\/www.simonbattersby.com\/blog\/building-a-css-vertical-expanding-menu-with-flyouts-stage-1\/","title":{"rendered":"Building a css vertical expanding menu with flyouts &#8211; stage 1"},"content":{"rendered":"<p>This article describes how I built the menu used on this website, using just css. I didn&#8217;t want to use javascript, partly because it seemed unnecessary, partly because I didn&#8217;t understand it enough. I\u2019ve tried to make this explanation as basic as possible, because I could have done with something like this myself a year or so ago.<\/p>\r\n\r\n<p>What I wanted was an expanding vertical menu, where the second level menu is displayed only on the &#8220;parent&#8221; first level pages and second level pages &#8211; for example this page displays the second level &#8220;Websites&#8221; pages. Subsequently, as an addition to improve navigation, I added  a &#8220;flyout&#8221; menu for the other second level pages &#8211; try hovering over &#8220;Blog&#8221; on the menu here to see what I mean.<\/p>\r\n\r\n<p>I&#8217;ll walk through this in three stages. First I&#8217;ll create the top level menu and style it,  then extend this to the second level as well,  and finally cover the flyout behaviour.<\/p>\r\n\r\n<p>OK, let\u2019s start by creating the html, just with one level initially:<\/p>\r\n<pre>html\r\n\r\n&lt;div id=\"nav-menu\"&gt;\r\n&lt;ul&gt;\r\n&lt;li&gt;&lt;a href=\"#\"&gt;Home&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;li&gt;&lt;a href=\"#\"&gt;Blog&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;li&gt;&lt;a href=\"#\"&gt;Websites&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;li&gt;&lt;a href=\"#\"&gt;Photos&lt;\/a&gt;&lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n&lt;\/div&gt;<\/pre>\r\n<p>The menu items are list items &lt;li&gt; in an unordered list &lt;ul&gt;. I&#8217;ve used a div to hold the menu, but I could have just applied the id to the &lt;ul&gt; tag. So this produces, using the default styling of the browser \u2013 that is, with no css applied:<\/p>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu1\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>So, now to step through the css changes one at a time so it&#8217;s clear what&#8217;s happening.<\/p>\r\n\r\n<p>First I&#8217;ll remove those bullet points on the first level menu. To do this we create a css entry like this:<\/p>\r\n<pre>css\r\n\r\n#nav-menu ul\r\n {list-style-type:none}<\/pre>\r\n<p>This produces:<\/p>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu2\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>Next remove the indent on the text:<\/p>\r\n<pre>css\r\n\r\n#nav-menu ul\r\n {list-style-type:none;\r\n  <span class=\"code_highlight\">margin:0px;\r\n  padding:0px<\/span>}<\/pre>\r\n<p>I  need to set both margin and padding to 0px to cope with the different way in which different browsers render a list. Now we get this:<\/p>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu3\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>Now remove the underlining of the links themselves. This needs a new entry in the css:<\/p>\r\n<pre>css\r\n\r\n#nav-menu ul\r\n {list-style-type:none;\r\n  margin:0px;\r\n  padding:0px}\r\n\r\n<span class=\"code_highlight\">#nav-menu ul a\r\n {text-decoration:none}<\/span><\/pre>\r\n<div class=\"greybox\">\r\n<ul id=\"menu4\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>OK so far, but the links don\u2019t look much like buttons. First I\u2019ll put a  dark grey border (hex colour #303030) on the links, which will also make it easier to see what\u2019s happening:<\/p>\r\n<pre>css\r\n\r\n#nav-menu ul\r\n {list-style-type:none;\r\n  margin:0px;\r\n  padding:0px}\r\n\r\n#nav-menu ul a\r\n{text-decoration:none;\r\n<span class=\"code_highlight\"> border: 1px solid #303030<\/span>}<\/pre>\r\n<p>So now it looks like this:<\/p>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu5\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>Now I \u2019ll make all the links the same width. After initially stting the width in ems I ended up reverting to pixels as this produced the most consistent and predictable behaviour in different browsers. I used a width of 170 pixels. To get this to behave in IE6\/7 it&#8217;s best added to both the <code>ul<\/code> and <code>a<\/code> elements.<\/p>\r\n<pre>css\r\n\r\n#nav-menu ul\r\n {list-style-type:none;\r\n  margin:0px;\r\n  padding:0px;\r\n  <span class=\"code_highlight\">width:170px;<\/span>\r\n}\r\n\r\n#nav-menu ul a\r\n {text-decoration:none;\r\n  border:1px solid black;\r\n<span class=\"code_highlight\">  width:170px;\r\n  display:block<\/span>}<\/pre>\r\n<p>I\u2019ve added two statements here, the width itself and the display:block statement which tells the browser to display the link, in this case, as a block rather than a piece of text. Now we have:<\/p>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu6\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>Next I&#8217;ll centre the text in the box. (As I&#8217;m English I get a bit annoyed at this point because I have to spell centre incorrectly.):<\/p>\r\n<pre>css\r\n\r\n#nav-menu ul\r\n {list-style-type:none;\r\n  margin:0px;\r\n  padding:0px}\r\n\r\n#nav-menu ul a\r\n{text-decoration:none;\r\n border:1px solid #303030;\r\n width:170px;\r\n display:block;\r\n<span class=\"code_highlight\"> text-align:center<\/span>}<\/pre>\r\n<p>Which gives me this:<\/p>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu7\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>This is starting to look like what I want. Now I&#8217;m going to set the font size to be a bit bigger:<\/p>\r\n<pre>css\r\n\r\n#nav-menu ul\r\n {list-style-type:none;\r\n  margin:0px;\r\n  padding:0px}\r\n\r\n#nav-menu ul a\r\n{text-decoration:none;\r\n border:1px solid #303030;\r\n width:170px;\r\n display:block;\r\n text-align:center;\r\n<span class=\"code_highlight\"> font-size:14pt<\/span>}<\/pre>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu8\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>And now I&#8217;ll make the buttons a bit taller using the line-height property<\/p>\r\n<pre>css\r\n\r\n#nav-menu ul\r\n {list-style-type:none;\r\n  margin:0px;\r\n  padding:0px}\r\n\r\n#nav-menu ul a\r\n{text-decoration:none;\r\n border:1px solid #303030;\r\n width:170px;\r\n display:block;\r\n text-align:center;\r\n font-size:14pt;\r\n <span class=\"code_highlight\">line-height:2em<\/span>}<\/pre>\r\n<p>This will centre the text vertically in the box as well:<\/p>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu9\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>Getting there. Now I&#8217;ll give the buttons the dark grey gradient background that gives them the ridged appearance. This is done by creating a 3 px wide image which is as high as the button, setting this as the background image and repeating the background along the x-axis (the horizontal axis). The image name is Button_top.gif, and it&#8217;s in the folder Images\/Buttons. Hence:<\/p>\r\n<pre>css\r\n\r\n#nav-menu ul\r\n {list-style-type:none;\r\n  margin:0px;\r\n  padding:0px}\r\n\r\n#nav-menu ul a\r\n{text-decoration:none;\r\n border:1px solid #303030;\r\n width:170px;\r\n display:block;\r\n text-align:center;\r\n font-size:14pt;\r\n line-height:2em;\r\n <span class=\"code_highlight\">background:url(Images\/Buttons\/Button_top.gif) repeat-x left;<\/span>}<\/pre>\r\n<p>So that last line is referring to the background image itself, repeating on the x-axis, starting from the left. Now the menu looks like this:<\/p>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu10\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p>Finally, for the top level menu, I&#8217;ll make the text colour white and the font Arial :<\/p>\r\n<pre>css\r\n#nav-menu ul\r\n {list-style-type:none;\r\n  margin:0px;\r\n  padding:0px}\r\n\r\n#nav-menu ul a\r\n{text-decoration:none;\r\n border:1px solid #303030;\r\n width:170px;\r\n display:block;\r\n text-align:center;\r\n font-size:14pt;\r\n line-height:2em;\r\n background:url(Images\/Buttons\/Button_top.gif) repeat-x left;\r\n <span class=\"code_highlight\">font-family:Arial, Helvetica, sans-serif; color:white<\/span>}<\/pre>\r\n<p>This gives us our finished top level menu, all driven by css.<\/p>\r\n\r\n<div class=\"greybox\">\r\n<ul id=\"menu11\">\r\n<li><a href=\"#\">Home<\/a><\/li>\r\n<li><a href=\"#\">Blog<\/a><\/li>\r\n<li><a href=\"#\">Websites<\/a><\/li>\r\n<li><a href=\"#\">Photos<\/a><\/li>\r\n<\/ul>\r\n<\/div>\r\n\r\n<p><a title=\"Part 2\" href=\"\/blog\/building-a-css-vertical-expanding-menu-with-flyouts-stage-2\/\">Click here for Part 2<\/a><\/p> ","protected":false},"excerpt":{"rendered":"<p>This article describes how I built the menu used on this website, using just css. I didn&#8217;t want to use javascript, partly because it seemed unnecessary, partly because I didn&#8217;t understand it enough. I\u2019ve tried to make this explanation as basic as possible, because I could have done with something like this myself a year [&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-107","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/107","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=107"}],"version-history":[{"count":2,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/107\/revisions"}],"predecessor-version":[{"id":2584,"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/pages\/107\/revisions\/2584"}],"wp:attachment":[{"href":"https:\/\/www.simonbattersby.com\/blog\/wp-json\/wp\/v2\/media?parent=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}