minitutorials main logo the place to learn online

Sponsor This Site!
Sponsorship Details - miniTutorials is taking enquiries into various Sponsorship opportunities. Available NOW! Read More on our Sponsorship page.
This Page Sponsored by :
Proudly Sponsored by Your Company name here!
miniTutorials Sponsorship
Donate!
Consider donating to keep this site alive and up to date. Click on the Donate button to go straight to PayPal site and make a donation of an amount you choose.


More information on where the money goes on the Donations page.
Discussion Forums
visit the mini tutorial forums
For expert help and advice on any of our Tutorials or anything else .... visit the forums and ask away!!
Google Ads
My Profile

Details about the owner of this site can be found at :-

ohloh profile
View Gavin McDonald's profile on LinkedIn
Powered by a
UK2 Dedicated Server

CSS Menus

Creating a CSS driven Menu system - Part 2 - By Geoff Vines

Appearance and position in life are everything - so they say!

Lets start by adding in some <a> elements to provide our hyperlinks and some real world menu items.


   <div id='navigation'>
      <ul class='level1'>
         <li><a href='home.html'>Home</a></li>
         <li><a href='about.html'>About us</a></li>
         <li class='submenu' >Services
            <ul class='level2' id='sub1'>
               <li><a href='design.html'>Web site design</a></li>
               <li><a href='hosting.html'>Web site hosting</a></li>
               <li><a href='search.html'>Search engine submission</a></li>
            </ul>
         </li>
         <li><a href='contact.html'>Contact us</a></li>
      </ul>
   </div>   
   

Display this version in your browser and you will see that as well as looking a bit on the dull side there is some overlap of text. Lets start fixing these problems.

Good time to add some font styling.

   div#navigation {
   /* Coding as required for page location*/
      position: relative;
      font-size: 12px;
      font-family: Verdana, Arial, sans-serif;
   }
	
Add a border to the menu blocks but not to the bottom.
Set the width to accommodate the longest menu item.

   div#navigation ul {
      list-style: none;
      list-image: none;
      margin: 0;
      padding: 0;
      position: absolute;
      border: 1px solid #000;
      border-bottom: none;
      background-color: #C8C8C8;
      width: 80px;
   }
	
Set the bottom border for each menu item.

   div#navigation ul.level1 li {
      position: relative;
      border-bottom: 1px solid #000;
   }
   
Set the left offset for where the sub-menu appears.

   div#navigation li.submenu:hover ul.level2 {
      display: block;
      left: 79px;
      top: 0px;
   }
	
Set the width of the sub-menu 1.

   div#navigation ul#sub1 {
      width: 170px;
   }
	
By default an <a> element is an inline element which means it only takes up as much space as it needs.  By making it a block element, it will occupy all the space available to it.  The padding gives us some space around the text, all except the right side.

These two set the width of our <a> elements to the width of their respective menu blocks minus 3px for the left-hand padding.

   div#navigation li a {
      display: block;
      padding: 3px 0 3px 3px;
      text-decoration: none;
   }
	

   div#navigation ul.level1 a {
      width: 77px;   
   }
	

   div#navigation ul#sub1 a {
      width: 167px;
   }
	

   div#navigation li a:hover {
      color: #FFF;
      background-color: #00F;
   }
	

Apply this to our menu structure and the first level of the menu now displays like this:

Role the cursor over one of the hyperlinks and you get this:

Notice how the full width of the menu item is active when you move the cursor over it and not just the text of the menu item.

When we move the cursor over the 'Services' menu item we get:

If you look back in the styling code to the left offset we set for the sub-menu, you will see that its 1px short of the width of the first level menu.  This keeps the sub-menu just inside its parent and avoids the need to rush the cursor movement going from one to the other.

Where's my menu gone?

There's a common problem that catches people out with menus and it's to do with the way the browser give default z-index to the block elements as they resolve the page.  If you haven't come across this term before, z-index reflects the front to back position of an element (in other words, layers) in the same way that x and y reflect the left to right and top to bottom position.  What follows is deliberately contrived to make a point.

Add this piece of code below the menu structure,


   <div id='spoiler'>
      
   </div>
	

and this style coding to the bottom of the coding block,


   div#spoiler {
      position: absolute;
      top: 0;
      left: 88px;
      width: 200px;
      height: 150px;
      background-color: #F00;
   }
	

display your page and you see this:

Move the cursor over the 'Services' menu item and - no sub-menu.  In fact it has displayed but its behind the red block.  Revisit the styling code for the navigation <div> element and give it a z-index greater than 0.


   div#navigation {
   /* Coding as required for page location. */
      position: relative;
      z-index: 1;
      font-size: 12px;
      font-family: Verdana, Arial, sans-serif;
   }
	

Now try it!

The 'Services' menu item is looking a bit forgotten.  I leave it as an exercise for you to style.  Hint - its <li> element belongs to the class of 'submenu'.  You might try something that marks it out as having a sub-menu rather than it being just another menu item.

This tutorial is also available in a PDF version.
This tutorial is Part Two of a Three Part Series and was written by Geoff Vines from 1ontheweb .

Related Tutorials

Part One - The basic building blocks.
Part Three - Going Horizontal.

Top

tutorial sponsors logo

About Us | Site Map | Privacy Policy | Contact Us | ©2002 - 2007 miniTutorials.com