Jump to content

How to build horizontal navigation menus in CSS

+ 1
  adfm's Photo
Posted Jan 22 2010 11:12 AM

If you would like to create a horizontal navigation menu out of an unordered set of links, like the example shown in Figure 7.13, you'll appreciate this excerpt from Christopher Schmitt's CSS Cookbook, Third Edition.


Figure 7.13. The default appearance of the links

Attached Image

Solution

First, create a properly constructed set of unordered links:

 <div id="navsite" class="clearfix">

 <p>Site navigation:</p>

 <ul>

 <li><a href="/">Home</a></li>

 <li><a href="/about/">About</a></li>

 <li><a href="/archives/">Archives</a></li>

 <li><a href="/writing/">Writing</a></li>

 <li><a href="/speaking/" id="current">Speaking</a></li>

 <li><a href="/contact/">Contact</a></li>

 </ul>

</div>

Then set the CSS rules for the navigation structure, making sure to adjust the list items to float, as shown in Figure 7.14:

#navsite p {

 display: none;

}

#navsite ul {

	width: 100%;

	float: left;

 padding: 0;

 margin-left: 0;

 border-bottom: 1px solid #778;

 font: bold 12px Verdana, sans-serif;

}

<span class="strong"><strong>#navsite ul li {</strong></span>

 <span class="strong"><strong>list-style: none;</strong></span>

 <span class="strong"><strong>margin: 0;</strong></span>

 <span class="strong"><strong>float: left;</strong></span>

<span class="strong"><strong>}</strong></span>

#navsite ul li a {

 padding: 12px 0.5em;

 margin-left: 3px;

 border: 1px solid #778;

 border-bottom: none;

 background-color: #666;

 text-decoration: none;

 background-image: url(title-glass.png);

 background-position: 50%;

 background-repeat: repeat-x;

 display: block;

 width: 7em;

}

#navsite ul li a:link {

 color: white;

}

#navsite ul li a:visited {

 color: #667;

}

#navsite ul li a:link:hover, #navsite ul li a:visited:hover {

 color: #000;

 background-color: #aae;

 border-color: #227;

}

#navsite ul li a#current {

 background-color: white;

 border-bottom: 1px solid white;

color: #448;

margin-bottom: −1px;

}

#navsite ul li a#current:hover {

 background-image: url(title-glass.png);

 background-position: 50%;

 background-repeat: repeat-x;

}

.clearfix:after {

 content: ".";

 display: block;

 height: 0;

 clear: both;

 visibility: hidden;

}

/* for IE6 */

* html .clearfix {

 height: 1%;

}

/* for IE7 */

*:first-child+html .clearfix {

 min-height: 1px;

}

Figure 7.14. The tab-based navigation

Attached Image

Discussion

The first part of the Solution hides the heading. This is done because the visual representation of the tab navigation design is enough to inform users that these are navigation links:

#navsite p {

 display: none;

}

The next rule defines the padding and margin for the box that is created by the unordered list element, ul. The line that stretches across the bottom of the folder tabs is drawn by the border-bottom property (see Figure 7.15):

#navsite ul {

 width: 100%;

 float: left;

 padding: 0;

 margin-left: 0;

 border-bottom: 1px solid #778;

 font: bold 12px Verdana, sans-serif;

}

Figure 7.15. The line the navigation tabs rest upon

Attached Image

The declaration that makes this horizontal navigation work with the unordered list is float for the list items:

#navsite ul li {

 list-style: none;

 margin: 0;

 <span class="strong"><strong>float: left;</strong></span>

}

Note

Another method for building horizontal menus is to use the inline property. Although both approaches do obtain a horizontal appearance, by setting a list item to float, you still retain the block-level properties of the element, allowing you to set properties such as margin and width for consistent menu tags. If you don’t want consistent tab widths, or if you prefer the spacing between the menu labels to be consistent, the inline property is an acceptable alternative with the padding-left or padding-right property.

Instead of stacking the list items on top of each other by default, the browser now lays out the list items as it would text, images, and other inline elements (see Figure 7.16).

Figure 7.16. The list spread out horizontally

Attached Image

To create the look of the folder tab, use the border property in the following CSS rule:

#navsite ul li a {

 padding: 12px 0.5em;

 margin-left: 3px;

 <span class="strong"><strong>border: 1px solid #778;</strong></span>

 <span class="strong"><strong>border-bottom: none;</strong></span>

 background-color: #666;

 text-decoration: none;

 background-image: url(title-glass.png);

 background-position: 50%;

 background-repeat: repeat-x;

 display: block;

 width: 7em;

}

The first border property is a shorthand property that dictates a solid, 1-pixel border around the link. However, immediately following the border property is the border-bottom property, which tells the browser not to display a border beneath the link.

The value of the border-bottom property is displayed over the border shorthand property (as shown in Figure 7.17). This overwriting occurs because the border-bottom declaration overrides the values in the border declaration because of the order in which they are declared.

Figure 7.17. Visible tabs

Attached Image

After you’ve created the look of the border tab, set the color of the text links and rollover states:

#navsite ul li a:link {

 color: white;

}

#navsite ul li a:visited {

 color: #667;

}

#navsite ul li a:link:hover, #navsite ul li a:visited:hover {

 color: #000;

 background-color: #aae;

 border-color: #227;

}

The final CSS rule defines how the “current” link appears. This style is applied to the link that represents the page being viewed by the user (see Figure 7.18):

#navsite ul li a#current {

 background: white;

 border-bottom: 1px solid white;

}

Figure 7.18. The look of the current link

Attached Image

Next, add a self-clearing float to the entire navigation menu on the div element. This makes sure that any text or content in the rest of the page doesn’t wrap around the menu navigation:

.clearfix:after {

 content: ".";

 display: block;

 height: 0;

 clear: both;

 visibility: hidden;

}

/* for IE6 */

* html .clearfix {

 height: 1%;

}

/* for IE7 */

*:first-child+html .clearfix {

 min-height: 1px;

}

See Also

The original tab menu bar (as well as other navigation styles) at http://css.maxdesign...orizontal05.htm

Cover of CSS Cookbook
Learn more about this topic from CSS Cookbook, 3rd Edition. 

Learn how to solve the real problems you face with CSS. This cookbook offers hundreds of practical examples for using CSS to format your web pages, and includes code samples you can use right away. You'll find exactly what you need, from determining which aspects of CSS meet the specific needs of your site to methods for resolving differences in the way browsers display it.

Learn More Read Now on Safari


Tags:
0 Subscribe


2 Replies

 : Jan 22 2010 01:12 PM
I need a specific variation on the CSS-based horizontal navigation... I need a version that can handle a variable number of items and have the items' widths expand or contract automatically to take up the full width of the <div> holding the menu.

I need it to do this WITHOUT any sort of on-the-fly setting of CSS widths of the <li> elements. I need/want it to do the same type of thing a table can do...automatically shrink or expand the <td> cells to fill the available width.

I have been unable figure out a way to do this, and I have been unable to find an example online.

Does anyone know of a way?
Chris Spurgeon

chris - at - spurgeonworld - dot - com
 : Jan 22 2010 02:06 PM
thank you for the css tutorial. It is so complicated for me and I am starting to make sense of it all.