The navigation bar was traditionally composed of graphic images arranged in a row of table cells. Now that tables are no longer recommended for positioning any nontabular page content, many Web authors are looking for ways to create navigation bars using structural XHTML markup and CSS formatting.
A simple CSS navigation bar
Perhaps the simplest solution to a CSS-styled text navigation bar is to put all the links in a single line of text, as is done in Example A.
This approach seems logical and straightforward. The problem is that having all the links in a single line of text provides precious little control over spacing around and between the links. So, to avoid crowding the links together, you usually end up inserting pipe (vertical bar) and nonbreaking space characters as spacers.
As the following code shows, the result is hardly the kind of clean, structural markup that we're looking for:
<div id="navbar1">
<a href="link1a.html">Button 1</a> | <a
href="link2a.html ">Button 2</a> | <a href="link3a.html ">
Button 3</a>
</div>
The markup gets even more cluttered if you add <span> tags around each link in order to apply the additional CSS styles that create button backgrounds and rollover effects.
A list-based CSS navigation bar
Another approach to creating a CSS navigation bar is to mark up the links as an unordered list using the <ul> and <li> tags.
Using an unordered list for a navigation bar may seem counterintuitive at first because we're accustomed to thinking of the unordered list as a stacked column of list items, each preceded by a bullet. That doesn't seem to fit the horizontal orientation of a navigation bar.
However, the logical structure of a list as a collection of individual list items is appropriate for the links in a navigation bar; and CSS rules enable you to override the default presentation of the list items to eliminate the bullets and arrange the list items to flow across the page instead of down the page.
With that in mind, let's look at Example B of the CSS styles and XHTML markup for a navigation bar based on an unordered list.
Here's the XHTML markup:
<div id="navbar2">
<ul>
<li><a href="link1.html">Button 1</a></li>
<li><a href="link2.html ">Button 2</a></li>
<li><a href="link3.html ">Button 3</a></li>
</ul>
</div>
Regular readers of this column may recognise this as being the same markup that I've used for other CSS button examples. One of the appealing things about this technique is that the markup is the same for any group of buttons, whether they're stacked in a column to one side of the main body text or arranged horizontally in a navigation bar across the top of the page.
Here's the CSS code that transforms the list of text links into a navigation bar:
div#navbar2 {
height: 30px;
width: 100%;
border-top: solid #000 1px;
border-bottom: solid #000 1px;
background-color: #336699;
}
div#navbar2 ul {
margin: 0px;
padding: 0px;
font-family: Arial, Helvetica, sans-serif;
font-size: small;
color: #FFF;
line-height: 30px;
white-space: nowrap;
}
div#navbar2 li {
list-style-type: none;
display: inline;
}
div#navbar2 li a {
text-decoration: none;
padding: 7px 10px;
color: #FFF;
}
div#navbar2 li a:link {
color: #FFF:
}
div#navbar2 li a:visited {
color: #CCC;
}
div#navbar2 li a:hover {
font-weight: bold;
color: #FFF;
background-color: #3366FF;
}
The div#navbar2 style sets the dimensions and background color of the div that contains the navigation bar links.
The div#navbar2ul style includes margin and padding declarations to override the default spacing assigned to unordered lists and sets the general text formatting. The white-space:nowrap declaration keeps the list on one horizontal line, even if the browser window is too narrow to display the whole line.
The real secret to this technique is in the div#navbar2li style. The list-style-type:none declaration removes the bullets that normally mark each list item, and the display:inline declaration causes the list items to flow from left-to-right across the page instead of each item appearing in a separate line.
Another key element of this technique is the div#navbar2li a rule. The text-decoration:none declaration removes the underscore that is normal for links, and the padding:7px10px declaration controls the spacing of the links in the navigation bar. The left and right padding controls the horizontal spacing, while the top and bottom padding is needed to make the colored background of the rollover effect (and the clickable area of the button) extend the full height of the navigation bar. You could also add left and right margin values if you want additional space between the buttons.
Finally, the rules for the :link, :visited, and :hover pseudoclasses set the color changes for the rollover effects.
You can use variations of this same basic technique to create many different navigation bar effects, including the popular "folder tab" look.
Do you need help with Web Technologies? 





1
Simon - 13/11/06
Please help me, I am trying to create a 100% naviagtion bar where the links will expand and contract, here is what I have don so far
http://www.gadgetmall.co.uk (Look at bar for "Asia Dragon Shop" "Asia Dragon Lifestyle" "Asia Dragon Business"
I want this bar to expand and contract depending on display, I can do this easily with a table and dont understand why it is so difficult in css
The problem is I just cannot find a way to achieve this and thought you maybe able to help
Thanks
» Report offensive content
2
JB - 18/11/06
Hi there, I was having real problems building a nav bar using unordered list. This bit of code helped me out no end! The top and bottom padding in the anchor style really was foxing me.
Cheers, saved me tearing my hair out all evening!
JB
» Report offensive content
3
Kagiso - 17/07/07
I have found this info very helpful and I wana thank you very much for this tho.
Im using this sample css for asp code and the body doesnt show so what can I do.It works for asp, or html but for aspx, its not.
form {
margin-bottom: 0;
}
input, textarea, select {
font-family: tahoma, "ms sans serif", arial, verdana, helvetica;
border-style: solid;
border-width: 1;
font-size: 11px;
}
.no-border {
font-family: tahoma, "ms sans serif", arial, verdana, helvetica;
border-style: solid;
border-width: 0;
font-size: 11px;
}
/* page default settings */
body, td {
font-family: tahoma, "ms sans serif", arial, verdana, helvetica;
color: #333;
font-size: 11px;
}
body {
margin: 0;
/*scrollbar-face-color: #d0d0d0;
scrollbar-highlight-color: #fff;
scrollbar-shadow-color: #e1f2f2;
scrollbar-3dlight-color: #e1f2f2;
scrollbar-arrow-color: #000;
scrollbar-track-color: #fff;
scrollbar-darkshadow-color: #1b1e25;*/
}
.full-size-table {
width: 780;
height: 100%;
}
.row1 {
height: 5%;
vertical-align : top;
/******************************************/
background-image: url("images/HomePage.png");
}
.row2 {
height: 90%;
vertical-align : top;
background-color: #f4f4f4;
}
.row3 {
height: 5%;
vertical-align : top;
}
a {
color: #333;
text-decoration: none;
}
a:hover{
color: #000;
}
/* images , the line "background-image: url("images/header.jpg")" means that there is a background image used;*/
/* I removed the image and put mine own*/
.header {
/*****************************************************/
background-image: url("images/header.jpg");
background-repeat : no-repeat;
}
.h-dot-line {
background-image: url("images/h-dot-line.jpg");
background-repeat : repeat-x;
background-position: center center;
}
.h-dot-linea {
background-image: url("images/h-dot-line1.jpg");
background-repeat : repeat-x;
background-position: center center;
}
.tile {
background-image: url("images/top-tile.jpg");
background-repeat : repeat-x;
}
.box-img {
/***********************************************/
background-image: url("images/Baby&Dady.png");
background-repeat : no-repeat;
}
/* links */
.left-link a {
color: #fff;
font-size: 13px;
text-decoration: none;
background-image: url("images/bullet.jpg");
background-repeat : no-repeat;
background-position: left 8;
padding: 3 1 4 15;
font-weight: bold;
display: block;
}
.left-link a:hover{
color: #000;
}
/* background colours */
.white {
background-color: #fff;
}
.gray {
background-color: #838381;
}
.graya {
background-color: #767673;
}
.grayb {
background-color: #cecece;
}
/* borders */
.border-bot {
border-bottom : #bbb 1px solid;
background-color: #fff;
}
.border-lr {
border-left : #bbb 1px solid;
border-right : #bbb 1px solid;
background-color: #fff;
}
/* text */
.black-txt {
color: #000;
}
.white-txt {
color: #fff;
}
.blue-txt {
color: #2f78cc;
}
/* headings */
h1 {
font-family: arial;
font-size: 25px;
margin: 0;
}
h2 {
font-family: arial;
font-size: 16px;
margin: 0;
}
h3 {
font-family: arial;
font-size: 14px;
margin: 0;
}
h4 {
font-family: arial;
font-size: 13px;
margin: 0;
}
» Report offensive content
4
Kagiso - 17/07/07
I have found this info very helpful and I wana thank you very much for this tho.
Im using this sample css for asp code and the body doesnt show so what can I do.It works for asp, or html but for aspx, its not.
form {
margin-bottom: 0;
}
input, textarea, select {
font-family: tahoma, "ms sans serif", arial, verdana, helvetica;
border-style: solid;
border-width: 1;
font-size: 11px;
}
.no-border {
font-family: tahoma, "ms sans serif", arial, verdana, helvetica;
border-style: solid;
border-width: 0;
font-size: 11px;
}
/* page default settings */
body, td {
font-family: tahoma, "ms sans serif", arial, verdana, helvetica;
color: #333;
font-size: 11px;
}
body {
margin: 0;
/*scrollbar-face-color: #d0d0d0;
scrollbar-highlight-color: #fff;
scrollbar-shadow-color: #e1f2f2;
scrollbar-3dlight-color: #e1f2f2;
scrollbar-arrow-color: #000;
scrollbar-track-color: #fff;
scrollbar-darkshadow-color: #1b1e25;*/
}
.full-size-table {
width: 780;
height: 100%;
}
.row1 {
height: 5%;
vertical-align : top;
/******************************************/
background-image: url("images/HomePage.png");
}
.row2 {
height: 90%;
vertical-align : top;
background-color: #f4f4f4;
}
.row3 {
height: 5%;
vertical-align : top;
}
a {
color: #333;
text-decoration: none;
}
a:hover{
color: #000;
}
/* images , the line "background-image: url("images/header.jpg")" means that there is a background image used;*/
/* I removed the image and put mine own*/
.header {
/*****************************************************/
background-image: url("images/header.jpg");
background-repeat : no-repeat;
}
.h-dot-line {
background-image: url("images/h-dot-line.jpg");
background-repeat : repeat-x;
background-position: center center;
}
.h-dot-linea {
background-image: url("images/h-dot-line1.jpg");
background-repeat : repeat-x;
background-position: center center;
}
.tile {
background-image: url("images/top-tile.jpg");
background-repeat : repeat-x;
}
.box-img {
/***********************************************/
background-image: url("images/Baby&Dady.png");
background-repeat : no-repeat;
}
/* links */
.left-link a {
color: #fff;
font-size: 13px;
text-decoration: none;
background-image: url("images/bullet.jpg");
background-repeat : no-repeat;
background-position: left 8;
padding: 3 1 4 15;
font-weight: bold;
display: block;
}
.left-link a:hover{
color: #000;
}
/* background colours */
.white {
background-color: #fff;
}
.gray {
background-color: #838381;
}
.graya {
background-color: #767673;
}
.grayb {
background-color: #cecece;
}
/* borders */
.border-bot {
border-bottom : #bbb 1px solid;
background-color: #fff;
}
.border-lr {
border-left : #bbb 1px solid;
border-right : #bbb 1px solid;
background-color: #fff;
}
/* text */
.black-txt {
color: #000;
}
.white-txt {
color: #fff;
}
.blue-txt {
color: #2f78cc;
}
/* headings */
h1 {
font-family: arial;
font-size: 25px;
margin: 0;
}
h2 {
font-family: arial;
font-size: 16px;
margin: 0;
}
h3 {
font-family: arial;
font-size: 14px;
margin: 0;
}
h4 {
font-family: arial;
font-size: 13px;
margin: 0;
}
» Report offensive content
5
KaneDoGG - 23/02/08
Nice man really nice, easy to understand and stright to the point i like it. I was also battling when it came to getting the list items to lay exactly where i wanted them to go. after this code of yours i got my head around it and use this theory now all the time.
regards
» Report offensive content
6
sweetangel - 11/06/08
css lessons - css scrollbar examples code
-- http://css-lessons.ucoz.com/scroll-css-examples.htm --
» Report offensive content
7
joe bott - 13/11/09
could just the a tag be used to control buttons states:
a:hover{xxxxxx}
a:visited{xxxxx}
work just as well.
» Report offensive content