The Secrets Behind a Functional CSS Flyout Menu

postsecret.blogspot.com - 1.I
Creative Commons License photo credit: Foxtongue

For some time now I have been using a few CSS Flyout Menu’s on different projects by just copying and pasting code from different websites that I have come across. Some of you may do just that with this article. But in a project I am currently working on I thought I would sit down and actually understand what is going on behind the scenes in the CSS to make this happen. During the early days of my development this is what I used to do to get by copying and pasting examples from other sites trying to get things done as quick as possible, well not any more. Feel free to copy and paste away with this but I urge you to take the time to read what I am about to write and take it all in so you to can understand what is involved.

I have wanted to write a pure CSS flyout menu for a while now so I though now is a good a time as any to get it done. In this example I will so you how to create it. You can have a look at the end result here. I have made the different elements different colors to separate them and make it easier to look at it in the CSS markup

Step 1. The (X)HTML Markup

For this example I used simple nested unordered lists, you can take a look at the code here. As you can see there is a top level UL with a List Element (LI), then nested inside that LI element there is another Unordered List (UL) with List Elements inside that as well. Make sense?. Take a look at the file and work through it. I found it really important to make sure I knew how this was set up. You can take a look at the result of the output here.

Step. 2. The CSS Markup

Now that you have got your head around the nesting of the Unordered Lists, let’s take a look at how we can use Cascading Style Sheets to give us that fly out effect.

.cssfly {
font-family: arial, sans-serif; width:106px; height:150px;
position:relative; margin:0; font-size:11px; margin:50px 0;
}
.cssfly ul li a, .menu ul li a:visited {
display:block; text-decoration:none; color:#000; width:104px;
height:20px; text-align:center; border:1px solid #fff;
border-width:1px 1px 0 0; background: #003399; color:#FFFFFF;
line-height:19px; font-size:11px;
}
.cssfly ul {padding:0; margin:0;list-style-type: none; }
.cssfly ul li {float:left; margin-right:1px; position:relative;}
.cssfly ul li ul {display: none;}

This first block of CSS is just some general styling. Things to note here is the use of position:relative on the list elements, and also the display:none on the nested UL. A good way to work out what each does is to comment out (/* */) those entries and look at the result.

The next block of CSS looks like this.


.cssfly ul li:hover a {color:#fff; background:#000;}
.cssfly ul li:hover ul {
display:block; position:absolute; top:0;
left:105px; width:105px;
}
.cssfly ul li:hover ul li a.hide {
background:#ccc; color:#000;
}
.cssfly ul li:hover ul li:hover a.hide {width:150px;}
.cssfly ul li:hover ul li ul {display: none;}
.cssfly ul li:hover ul li a {
display:block; background:#ccc; color:#000; width:150px;
}
.cssfly ul li:hover ul li a:hover {background:red; color:#000;}
.cssfly ul li:hover ul li:hover ul {
display:block; position:absolute; left:151px; top:0; color:#000;
}
.cssfly ul li:hover ul li:hover ul li a {
display:block; width:200px; background:#dfc184; color:#000;
}
.cssfly ul li:hover ul li:hover ul li a:hover {background:#bd8d5e;
color:#fff;
}

The things to look at and understand in this section is the posistion:absolute entries on the .cssfly ul li:hover ul line. Again a technique that I find very useful is to comment out things and see what the result is. If you comment out that and then preview it you will notice that the nested UL will appear directly underneath top UL.

That is why that line of CSS in so important and the top:0 left:105px gave me the position I was after.

So as you can see a pure CSS Flyout Menu is not really that hard to achieve. And the big thing that I find very helpful is actually understanding what each part of the CSS Markup does. As you may notice with the above CSS I have included the facility to add a Third Level to the menu with the following line
.cssfly ul li:hover ul li:hover ul. So all you need to do in the (X)HTML markup is add another nested UL inside the LI Element that you want. See this for the added UL.

I hope this has helped you understand what is behind a CSS Flyout Menu.

Don’t want to miss out on any TIPS?, SUBSCRIBE to my Web and Blog Design Tips Newletter and get the tips that everyone else is getting.


| del.icio.us | Digg it | Furl | reddit | StumbleUpon | Yahoo MyWeb

You can follow any responses to this entry through the RSS feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

10 Responses to “The Secrets Behind a Functional CSS Flyout Menu”

  1. [...] last article was about creating a Pure CSS Flyout Menu. In this article I want to teach you how you can integrate that concept into your WordPress Theme [...]

  2. I’m working through your code but find when testing in Firefox 3, the third level does not align with the 2nd level flyout. That is “Forum News” is level with “Site Map”, not “Forum”, as is the case in IE 7. I’m relatively new to CSS so may not see what is amiss!!

  3. it doesn’t work in IE 6…

  4. This is helpful. Coming from someone who is not up to speed on XHTML, CSS, etc, it would be good to mention that you have to specify the DOCTYPE in order for this to work in IE 7. I figured it out by process of elimination. :)

  5. Robert, of course this does not work in IE6. This is a PURE CSS flyout menu. IE6 does not support the :hover pseudo class on anything except for (spaced so it shows up) tags. For IE6 to work with this, you would need to add Javascript to make it do what you want. We developers can’t wait for the day IE6 no longer matters. It causes SOOOO many problems when trying to use CSS.

    Great tutorial though. I wanted to find one that I could use to make the menu pop out the left side as the nav is placed on the right hand side of the page and this works beautifully.

    I will go through it and understand more about the css that makes it all happen.

  6. the tags I tried to space still did not show up. I was trying to say that IE6 does not support the :hover pseudo class on anything but a(link) tags. I will try entities and see it that shows up…..

    <a href="#">Link</a>

  7. This nice! I understand what you are doing here, but I see that .menu class in the CSS, but there isn’t a menu class in the HTML (that I can see). It seems to me that is how you make the distinction between the main menu item and the sub menu (menu that does the flyout), but where is that class in the HTML?

  8. Please Can a CSS menu Flyout Work Just like a Java script Menu Flyout.

  9. Changing my submenus ul’s to absolute positioning finally gave me what I wanted. I really appreciate this tutorial. U RAWK!

  10. Great tutorial. The only part of the code that I don’t understand is “a.hide”. Does this refer to a “hide” class?

    Thanks.

Leave a Reply