Web Design
Simple Navbars using CSS
Introduction
Menu 1: Vertical
The first navbar will be vertical and look like this:
- Launch Dreamweaver and create a new HTML document. Save it as yourName_navbars.html.
- In the body, create an unordered list with the following items: Home, What's New, About Us, FAQ , and Contact. Highlight each item in turn and turn it into a 'dummy' link (<a href="#">). Surround the list with a <div> tag called "navigation." This will become the navigation menu. It should look something like this:
<ul id="navigation"> <li><a href="#">Home</a></li> <li><a href="#">What's New</a></li> <li><a href="#">About Us</a></li> <li><a href="#">FAQ</a></li> <li><a href="#">Contact</a></li> </ul>
- In the head portion of your document add the <style> tags:
<style type="text/css"> <!-- --> </style> - Copy the CSS code below and put it into the head of your HTML document between the comment tags (<!-- and -->):
This first style rule sets the font family, size and weight for all of the navigation menu items. Of course, you can alter these to suit your own needs. The width sets the width of the navigation menu column; by using ems the menu is expandable. If you use pixels, the menu column will remain a fixed width, but the text will get bigger and perhaps break-out of their boxes. The border is optional, just a design element. Setting the list-style to none removes the bullets in the list.
#navigation { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 0.8em; font-weight: bold; width: 8em; border-right: 1px solid #333; list-style: none; margin: 0; padding: 0; } - Now a short rule:
This style removes the default margins from the list items.
#navigation li { margin: 0; } - Now another big style rule:
The key line here is display: block. That particular property makes the area around the words "clickable," not just the words themselves. As a result, they look and behave more like buttons. Setting the text-decoration to none removes the default underlines on the links. Setting the width to 100% ensures that each menu item expands to the width of the menu column. Everything else determines visual style—colors, borders, and padding. All can be modified to suit your design (the borders especially are optional).
#navigation li a { display: block; padding: .2em .2em .2em .4em; border-top: 1px solid #003; border-left: 1px solid #369; border-right: 1px solid #69C; border-bottom: 1px solid #369; background-color: #036; color: #fff; text-decoration: none; width: 100%; } - But we're not done quite yet:
This line is a hack to make sure our menus format properly in Internet Explorer.
HTML>body #navigation li a { width: auto; } - Finally, we add the hover pseudo-style for rollovers. This will change the menu item colors when moused-over:
#navigation li a:hover { background-color: #69F; } - Save it and test it. Does it work?
- Customize. Try altering the colors, fonts, sizes, etc.
Menu 2: Horizontal
The second navbar will look like this:
- Make a new list below your first list/navbar. You can use the same code as in Menu 1 above, except change the ID name to "navigation2." It should look like this:
<ul id="navigation2"> <li><a href="#">Home</a></li> <li><a href="#">What's New</a></li> <li><a href="#">About Us</a></li> <li><a href="#">FAQ</a></li> <li><a href="#">Contact</a></li> </ul> - Add the following code to the CSS section:
So far it's very similar to the first menu. However, there are a couple significant differences. First of all, the width of this menu is 100% because we want it to stretch across the page. We've also added a float property; this does not appear to do anything, but it's important for it keeps the list from collapsing on itself when finished.
#navigation2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 0.8em; font-weight: bold; margin: 0; padding: 0; width: 100%; background-color: #036; list-style: none; float:left; /* This is needed to keep the list element from collapsing */ } - Now add the following rule:
This is the key to the whole operation; the float:left causes each menu item to line up horizontally, rather than vertically, which is normal for a list. Now let's style the links.
#navigation2 li { float:left; margin: 0; } - Add these rules:
Once again, we use the display:block to make the area around the list item "cliackable." We use line-height to give the navbar thickness (height). Colors and padding amounts can be adjusted according to your design.
#navigation2 li a { display: block; margin: 0; padding: 0 2em; line-height: 1.8em; color: #fff; text-decoration: none; } #navigation2 li a:hover { background-color: #69F; } - . Save it and test it. Does it work?
- Customize. Try altering the colors, fonts, sizes, etc.