CSS menu


Home - Tutorials - CSS tutorials

In this tutorial I will show you how to create a vertical or horizontal menu system for your website using pure CSS.

Tutorial info:


Name:CSS menu
Total steps:1
Category:CSS tutorials
Date:2008-04-01
Level:Beginner
Product:See complete product
Viewed:932

Bookmark CSS menu



AddThis Social Bookmark Button

Step 1 - CSS menu


CSS menu

What you need on almost every websites is a menu or navigation block. You can place this block on the top, left, right or anywhere you want. In most cases the links are organised vertically or horizontally. If you checks the source of some sites you can see that the menu items are ordered in table rows or columns. This is not a good solution as the page layout should not use tables at all. To avoid this problem some new solutions use list instead of tables. Now I will show you a clean solution without tables and lists. At the end you will get a menu like these:

Horizontal menu
Vertical menu 

So let's create a main menu with the following menu items:

The HTML code for this is very simple:

Code:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <title>CSS Menu</title>
  4. </head>
  5.  
  6. <a href="#">Home</a>
  7. <a href="#">Products</a>
  8. <a href="#">Forum</a>
  9. <a href="#">About Us</a>
  10. </body>
  11. </html>

But the output is almost useless for us. So it's time to make it better with CSS. Before starting with CSS coding first put our links in a surrounding div block like this:

Code:
  1. <div id="menu">
  2. <a href="#">Home</a>
  3. <a href="#">Products</a>
  4. <a href="#">Forum</a>
  5. <a href="#">About Us</a>
  6. </div>

First try to make a menu block where the menu items are listed one under another. To do this set the width parameter for the div block and for the a tag as well. Don't forget to calculate with margin, border and padding values.

However in case of a tags it is not enough just to set the width parameter but you need to set the display parameter as well. This should be display:block. So the following CSS code results a better displayed menu.

Code:
  1. <title>CSS Menu</title>
  2. <style type="text/css">
  3. #menu {
  4. width:120px;
  5. }
  6.  
  7. #menu a {
  8. width:110px;
  9. display:block;
  10. }
  11.  
  12. </style>
  13. </head>

We can make it much better. Let's use some border, rollover effects and colors like this:

Code:
  1. #menu {
  2. width:120px;
  3. }
  4.  
  5. #menu a {
  6. width:110px;
  7. display:block;
  8. border:1px solid #ddd;
  9. background-color:#fafafa;
  10. padding:2px;
  11. margin:3px 1px;
  12. text-decoration:none;
  13. }
  14.  
  15. #menu a:hover {
  16. background-color:#ccc;
  17. }
  18.  

It is much better isn't it? And we didn't used any unnecessary element in our HTML code.

Horizontal alignment 

If you want to display your menu horizontally you only need to edit the CSS and set the div block width value and the float property of the a tag to float:left.

Code:
  1. #menu {
  2. width:600px;
  3. }
  4.  
  5. #menu a {
  6. width:110px;
  7. display:block;
  8. border:1px solid #ddd;
  9. background-color:#fafafa;
  10. padding:2px;
  11. margin:3px 1px;
  12. text-decoration:none;
  13. float:left;
  14. }
  15.  
  16. #menu a:hover {
  17. background-color:#ccc;
  18. }
  19.  






Tags: css menu system, css navigation system, css menu, css navigation

CSS menu - Table of contents
Step 1 - CSS menu

F1 Site Family
AJAX F1
CSS F1
Database F1
Forex F1
Flash F1
HTML F1
JavaScript F1
PhotoShop F1
PHP F1
 

 
MaxTutorial
Monthly mortgage payment calculator
WebFormGenerator
Forex mini accounts

Total time: 0.1554