CSS Horizontal Navigation Menu
I’ve had a request to expand my css navigation tutorial to include a horizontal navigation. This is an extention of my previous css navigation menu tutorial.
To create a horizontal navigation menu in the header, you’ll need to create two horizontally stacked divs within the header. The way I normally code it is to create a div in the header named header top, and under that I’ll put the nav div. Make sure to size them both to the header width so they can’t appear side by side. You’ll also need to add a clearing div after the header so that following divs will begin in the correct place:
<div id=”header”>
<div id=”header-top”></div>
<div id=”nav”>
<ul>
<li><a href=”#”>Home</a></li>
<li><a href=”#”>Nothing</a></li>
</ul>
</div>
</div>
<div id=”clr”></div>
As a convention I started the naming of the navigation styles starting with the nav div. This change will need to “cascade” to all of the other styles within the unordered list. As you can see below, the key to making the nav list horizontal is adding float:left to the li tag. The header-top now displays the header image as a background, and the header div is now just a container to hold the header-top and nav divs. I added a background color to it for aesthetics.#header{
width: 780px;
height: 84px;
background: #333333;
}
#header-top{
width: 780px;
background-image: url(‘images/header.jpg’);
background-repeat: no-repeat;
height: 50px;
}
#nav{
width:780px;
height: 32px;
}
div#clr{
clear: both;
}
div#nav ul li{
float:left;
}
div#nav ul{
list-style: none;
padding: 0px;
margin: 0px;
}
div#nav ul li a{
display: block;
padding:5px;
margin: 1px;
border: 1px solid white;
width: 100px;
height: 20px;
background: #333333;
color: white;
text-decoration: none;
}
div#nav ul li a:hover{
background: #666666;
}



Hi Napkin,
Thank you so much for taking the time to show me how to move the navigation to the top of the header. Very nice of you.
I saw the trick of using the clear:both. This is what I was missing.
I think you have the css under control. Keep up the good work.
I will be visiting your blog very often. Until next time, take care.
KK-
I have another way to accomplish this, which you can check out here:
http://blog.jelthure.com/code/horizontal-navigation-a-how-to-xhtml-css-sprite/
thanks