Vertical CSS menu with a ‘behavior’ file.

Author: Stefan Vervoort | Please Comment!

Today, I’ll continue on a topic I wrote about some time ago. A CSS menu. This time we will build a vertical CSS menu with rollover submenu’s. This menu will be written in CSS and HTML, and will use a so-called ‘behavior’ file. What are we going to produce? The menu I’ve made can be found as an example and as .rar file to download below.

Vertical CSS menu - Download
Vertical CSS menu - Example

Horizontal Menu

You might be interesting the horizontal version too, I have wrote an article about that. Read it here.

We start with the structure

To give you a better idea of the structure we’ll use, I’ve made a little image explaining this. We start with an <ul> tag (blue). Inside this tag, we’ll add an <li> tag (pink) to display the menu items. The submenu has the same structure as the orginal menu. It is a menu in a menu. You can see what’s going on in the picture; we’ll add another <ul> tag (green) with < li> tags (orange) inside the pink tag.

htmlstructure.jpg

To explain it some more; look below. The menu structure will look like this in your browser. I colored the items the way I did in the structure picture so you recognize what codes displays what.

CSS menu

I hope this part is clear now. Next are the HTML codes:

<ul>
	<li><a href="#">Home</a></li>
	<li><a href="#">Services</a>
<ul>
	<li><a href="#">Webdesign</a></li>
	<li><a href="#">Development</a></li>
	<li><a href="#">Illustration</a></li>
	<li><a href="#">Search engine</a></li>
	<li><a href="#">Wordpress</a></li>
</ul>
</li>
	<li><a href="#">Blog</a>
<ul>
	<li><a href="#">Webdesign</a></li>
	<li><a href="#">Development</a></li>
</ul>
</li>
	<li><a href="#">Contact</a></li>
</ul>

Behavior .htc file

In CSS, you can add the :hover pseudo-class to many elements. Most modern browsers support this technique already, except the biggest one; Microsoft’s Internet Explorer. IE only supports :hover pseudo-classes on the tag. The menu we are going to built has to work in IE as well. How to do this? With a behavior file. With this file we are able to add extra functions to your CSS properties, which won’t work in other browsers than Internet Explorer. This way, the ability to script that :hover function does exist! I’m not scripting my own behavior file, because it’s written in Javascript, a language I’ve never looked into. The file we are going to use can be found here; Behavior file.

And now the CSS

First, we add the behavior file to your CSS file. This .htc file only has to load one time, so we’ll place it in an element that loads just one time. Like this:

 body { behavior:url("csshover.htc"); }

Our CSS menu will require some ‘core’ codes. These codes are needed to let the menu work the way we want it to. There are a lot things you could change to let it fit your requirements. Margin between the items, background or text colors, whatever. To let our menu work, use the codes below:

 ul {
list-style: none; /* Remove the bullets */
margin: 0;
padding: 0;
width:200px; /* Menu width */
}
ul li {
display: block; /* Make a menu item a block (height 100%, width 100%) */
height:100%;
width:100%;
position: relative;
float:left; /* Make sure (in IE) there is no margin between the menu items */
}
ul li a{
display:block; /* When rollover the menu item, the whole item will be seen as link. */
}
ul li a:hover {
padding:0;
}
ul ul {
position: absolute;
top: 0;
display:none; /* Display nothing when not 'hovered' */
}
ul li ul li{}
ul li ul li a:hover{}
ul li:hover ul{
display: block; /* Make sure the submenu is displayed as blocks as well. */
left:200px; /* Your menu width. The submenu appears there, when you hover a menu item. */ }

The problem for your Internet Explorer browser is this line of codes: ‘ul li:hover ul’. It will not display this part, because it doesn’t know what ‘li:hover’ means. To make sure IE does recognize it the way we want it to, we use our ‘behavior’ file.

We’re done

The menu works. I think this version is one of the most simple CSS menu’s around. But well, it doesn’t look great yet. I’ve made a example to show you how to give this menu some borders, background colors and hover styles. Try it yourself and you can post your menu in the comments! If you have a problem with this menu, my explanation or just want to ask me something, please leave me a comment or contact me directly. I hope you have enjoyed this article and keep on coming back! To make this more easy to modify this for you, I have included all the files in a .rar file and this file is available for download below!

Vertical CSS menu - Download
Vertical CSS menu - Example

Liked this post? Subscribe, or Share and Enjoy:
  • Digg
  • del.icio.us
  • Blogosphere News
  • Design Float
  • description
  • StumbleUpon
Tags:

20 Comments. Add yours!

  1. Aminur Rahman [aka Tom R]
    1:10 am on February 1st, 2008

    can it be used ad horizontal menubar aswell?

  2. Stefan Vervoort
    11:57 am on February 3rd, 2008

    This time it is a Vertical tutorial. I will publish a horizontal version very soon.

  3. Sio
    4:21 pm on February 19th, 2008

    Thanks for the tutorial.

    IE is making web designer more work to do.

  4. Rashidah K Vire
    8:54 am on March 5th, 2008

    Thanks very much i was looking for this menu style forever and you have shown me the easiest way to do it thank you.

  5. Bedri Özgür Güler
    1:18 pm on June 30th, 2008

    Thank you very very much pal

  6. A graphic designer
    5:52 am on July 14th, 2008

    That menu is very simple indeed. I’ve never seen a behavior file used that way though. I’m just wondering how compatible that menu system is.

  7. klughing
    6:51 pm on July 14th, 2008

    How do you make the top menu stay highlighted as we go through the sub-menus of that top menu?

  8. Aseel
    6:12 am on August 20th, 2008

    Thanks for your tutorial , it’s help me a lot.
    I used this menu inside table and it’s work well in ie6, ie7 , but in Firefox the submenu didn’t appears well, is there any solution ? please help

    Thanks again

  9. Prajith
    1:54 pm on September 2nd, 2008

    Even though your example is a simple menu, its clean. I was looking for a horizontal menu.

  10. Stefan Vervoort
    3:06 pm on September 8th, 2008

    Horizontal coming up!

  11. Danh ba web 2.0
    4:22 am on September 13th, 2008

    Thanks your tutorial again.
    Good luck !

  12. Kelly
    9:47 am on September 26th, 2008

    Stefan,

    Thanks so much for this post. I’ve started to get involved in wanting to customize parts of my site, and this kind of coding tutorial makes a lot of sense. Gonna take a look around for more jems here!

    Kelly

  13. Per Karlsson
    6:15 pm on November 1st, 2008

    Thanks, great tutorial.
    Just one thing, this menu system seems to work exactly the same withouth the use of the behaviour file. I’ve tried it in IE, Firefox and Opera and none of them display anything different without the behaviour file, so what exactly is the purpose of it?

  14. Samantha
    10:20 am on November 8th, 2008

    I thought it was something wrong with me, but your site is broken in Firefox! Thanks for the tutorial, very useful for a beginner like myself.

  15. nadz
    7:46 pm on November 24th, 2008

    thx buddy will try it for sure as very less people use it on blogs. will be helpful for my readers for sure to navigate

  16. Ali
    3:38 am on November 27th, 2008

    Haw can we change the position of submenus from right to left?

  17. Yousef
    3:48 pm on December 14th, 2008

    Thanks very much i was looking for this menu style forever and you have shown me the easiest way to do it thank you.

Trackbacks

  1. Pre-load images in a CSS menu » DivitoDesign - Webdesign Blog
  2. Tutorial Machine » Blog Archive » Vertical CSS menu with a ‘behavior’ file
  3. How-to: DropDown CSS Menu » DivitoDesign - Webdesign Blog

leave a reply

This blog is a DoFollow blog. This means your URL counts as a backlink. Some basic HTML is allowed. Please keep all comments constructive, polite and on-topic. Any spam or offensive comments will be deleted.