A basic navigational menu can be created from a list (ordered or unordered) by making each list item a link and then "stylize" the whole list to make it behave like a menu.
First let's start by creating a simple list of paragraphs by typing a list (i.e., Home, Products, etc.) in Design View – clicking ENTER or RETURN key after each paragraph (in this case, the list is added in the sidebar1 div of a custom Dreamweaver layout page):
<p>Home</p>
<p>Products</p>
<p>Services</p>
<p>Contact Us</p>
Home
Products
Services
Contact Us
Next, let's convert list of paragraphs to a simple unordered (bullet) list by selecting the list first in the tag chooser and clicking on the bullet list button in the Property Panel.
<ul>
<li>Home</li>
<li>Products</li>
<li>Services</li>
<li>Contact Us</li>
</ul>
Next, let's convert the list to a list of links by applying an anchor tag <a> to the text in each of the list item (highlighted in bold).
NOTE: We could have named the first line home.html; however, it is best practice to name the "home page" index.html.
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="product.html">Products</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="contact_us.html">Contact Us</a></li>
</ul>
However, the list still looks more like a bullet list with links associated with it. Well,let's correct this problem.
#nav {
list-style-type: none;
background: #09F;
width:180px;
padding:0;
margin:0;
}
Now, let's address the links themselves by creating a contextual selector (i.e., #nav a) to target the link tag (i.e.,<a>) to:
#nav a {
text-decoration: none;
color: #FFF;
padding: 0 15px;
line-height: 2.1;
border-bottom: 1px solid #FFF;
}
There is a problem with the current implementation. Because the above rules were applied to the <a> tag, the padding will only be added to the right and left of the text – not across the width of its parent container (<ul>). Also, the white borders at the bottom will only extend to the end of the inline <a> elements. If you were to preview this in a browser, you would see that clickable area would only be over the text and the padding on both sides. There is a quick solution to this problem.
Remember from our discussion of inline vs block-level element that inline elements only consume only the amount of space they need. However, block-level elements consume the whole width of its parent container. So to fix this problem, all we have to do is convert the inline elements to block-level elements by adding the following rule (highlighted in bold) to the existing #nav a block:
#nav a {
text-decoration: none;
color: #FFF;
padding: 0 15px;
line-height: 2.1;
border-bottom: 1px solid #FFF;
display:block;
}
If you click the Live View button or preview this page again in a browser (F12), you will see that the clickable area extends across the whole width of each menu item.
NOTE: Since you only have one level in this menu, we only create a less specific rule #nav a. Had we created a multilevel menu, we would had to be more specific in defining our rule: #nav li a.
NOTE: Notice the padding values of 0 and 15px. When a value is zero, there is no need to specify a unit because it does not really matter if the unit is in pixels, inches, etc. since the value is zero. Also, when only two values instead of four are written for padding, the first value refers to top or bottom and the second value refers to left or right of the box element.
Now, let's enhance our menu by creating a hover effect when the user moves the mouse of a menu item.
To create a hover effect, we will apply a hover pseudo-class to the <a> tag inside the nav area by writing the following rule:
#nav a:hover {
background: #006;
}
To "see" the hover effect, preview in a browser and roll mouse over menu.
NOTE: You can also use a background image for the Active and Hover state as well as change the font color, etc
.
TIMESAVING TIP: Once you have some or all of the CSS rules you need to create a functional menu, you can create a snippet of the CSS code and then reuse the code again in a new web site or new web site file by simply inserting the snippet in the <style> tag or an external style sheet or create new style sheet altogether: The advantage of this is that once you have code that work you don't have to type it again or figure out how it was done again.
To let the user know what page he or she is currently on, we will write some code the "detect" what page the browser is viewing and highlight the corresponding menu according.
1. Write ID for each <li> tag by updating the unordered list (highlighted in bold) in the Code View.
<ul id="nav">
<li id="nav_home"><a href="home.html">Home</a></li>
<li id="nav_products"><a href="products.html">Products</a></li>
<li id="nav_services"><a href="services.html">Services</a></li>
<li id="nav_contact_us"><a href="contact_us.html">Contact Us</a></li>
</ul>
2. Write a series of group ID selectors to target their respective pages.
#body_home #nav_home a,
#body_products #nav_products a,
#body_services #nav_services a,
#body_contact_us #nav_contact_us a {
background:#00F;
font-weight:bold;
cursor:auto; /* to change cursor back to an arrow */
}
NOTE: The #body_home, #body_products, etc. will provide the necessary "hooks" to target the necessary id (i.e., #nav_home, #nav_products, etc.) for the specific menu item.
NOTE: The cursor:auto property is used to change the cursor back to the default arrow and allow the button to not change state when it is mouse-over and the user will not be tempted to click it.
NOTE: The above is a "group" of selectors. While not necessary each selector is placed on a line by itself separated by commas except for the last selector.
NOTE: This technique is best practice because:
3. Add IDs to each page in body tag to target menu item for those pages by opening the template page and then selecting Modify > Templates > Make Attributes Editable.... In the Editable Tag Attributes dialog box that appears, enter the following info:
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
<!-- TemplateParam name="id" type="text" value="body_home" -->
</head>
<body id="@@(id)@@">
<body id="body_products">
If you preview the page, you should see the Products Menu highlights with a different color with its text in bold without having to hover over that menu.
4. Test several times by changing the id attribute in the body tag from <body id="body_products"> to <body id="body_home"> then <body id="body_services"> and <body id="body_contact_us">. This is for testing purposes only, in real life, add these attributes to the corresponding pages (i.e., body.html, products.html, etc.)
IMPORTANT NOTE: If you are intending to use a template-based web-site, select Modify > Templates > Make Attributees Editable... and make the ID attribute editable. Then, on EACH page select Modify > Template Properties and add the corresponding page id (i.e., nav_body, nav_products, etc.)
Make the menu expand across the whole width of parent container (i.e., in this case, sidebar1 div) by changing the width from 180px to 100% (highlighted in bold)
#nav {
list-style-type: none;
width:100%;
background: #09F;
padding:0;
margin:0;
}
Also, in the sidebar1 selector change:
.twoColFixLtHdr #sidebar1 {
float: left;
width: 250px;
padding: 15px 0;
background: #EBEBEB;
}
While not a major problem, it would be nice to have a border at the top of the menu as well so that it can be seen if the sidebar1 background color is not white. To include this in your menu for now or in the future, we will target the <ul> element and add a top border to it.
NOTE:
To see the effect, change ul#nav background-color to something other than white, if necessary.
Now, create the following rule to add a top border:
ul#nav {
border-top: 1px solid white;
}
NOTE: This rule (ul#nav) basically says target the top of the <ul> container element with an ID of "nav" and add a border. The other rule (#nav li) says target the bottom of the <li> container elements (not the <ul> element) that is inside a container with a ID name of #nav# and add a border.
Add the following code to the #nav a:hover rule:
#nav a:hover {
background: #006;
border-left: 5px solid red;
}
If a viewer is using an older version of Internet Explorer (i.e., IE 5 and below), they may encounter a bug known as the "whitespace bug" that incorrectly render whitespace between list items. To resolve this problem add the following code (highlighted in bold)
#nav {
list-style-type: none;
width:100%;
background: #6F6146;
padding:0;
margin:0;
float:left
}
Also, by floating the list items, IE will render them without whitespace.
#nav li {
margin:0;
padding:0;
float: left;
width:100%;
}
We needed to float the unordered list element in order to contain the floated list items, and extend the background color behind all of them. Because the widths are specified for both elements, the float property has no real effect. (SEE pg . 91 of The Art and Science of CSS)