Simple Navigation Bar With CSS And (x)HTML

Steven Bradley

by Steven Bradley
on Sunday, May 14th, 2006
in CSS

« New Search Products From Google

Matt Cutts Confirms Link Quality Important At Google »

Last week I showed how you can create a simple menu using an (x)HTML list and CSS. Today I thought I’d use the same technique to create a simple navigation bar. Once again we’ll structure things with a list of links and use some CSS to turn that list into a navigation bar.

(x)HTML List

Just like last time the (x)HTML structure will be an unordered list with links inside each of the list items. In fact it’s basically the same list used in the menu example (with a different id applied) in part to show how powerful CSS can be in generating a different look to the same (x)HTML structure.


<ul id="list-nav">
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Contact</a></li>
</ul>

Just a typical list of links with an id of list-nav applied to allow us to specify the list in the CSS. The code as it is now will look like:

Styling The Navigation Bar With CSS

Let’s start by setting padding and margin on the list itself so we have things looking the same in Firefox and IE. We’ll also remove the bullets and set a width on the overall list.


ul#list-nav {
margin:20px;
padding:0;
list-style:none;
width:525px;
}

List items normally display as block level elements, but here we want them to display inline to achieve the look of a navigation bar as opposed to a menu.


ul#list-nav li {
display:inline
}

For the links we’ll again take out the underline, add a little padding, give the links the familiar green background color and white text color. We’ll also give the links a width and use 100px for each link. The key this time will be in positioning the links by floating each one left.


ul#list-menu li a {
text-decoration:none;
padding:5px 0;
width:100px;
background:#485e49;
color:#eee;
float:left;
}

The navigation bar is now taking shape and should look like:

All that’s really left is to add some styling touches and also a rollover effect.

Finishing Touches

The text looks a little off up against the left egde of the bar so let’s center it. We’ll also add a white border in between the links to help differentiate where each one begins and ends.


ul#list-nav li a {
text-align:center;
border-left:1px solid #fff;
}

Here the border has only been applied to the left side of each link. This leaves a 1px border on the left edge of the overall navigation bar, but since it’s currently sitting on a white background it disappears into that background. Depending on the structure of the rest of your document this could throw the positioning of things off by 1px, but it’s easy enough to remove the border from the leftmost link if it’s an issue.

We’ll do the same thing with the rollover as last time. We’ll just change the color of the background and the color of the text itself. Naturally enough the CSS is exactly the same we used in previously when creating the rollover on the menu.


ul#list-nav li a:hover {
background:#a2b3a1;
color:#000
}

The complete navigation bar should look like:

Just as with the simple menu the navigation bar is also quite simple. In fact if you compare the code for both you’ll find that the majority is exactly the same for each. The (x)HTML is the same with the exception of the different id, as is much of the CSS. The major differences here are making the list wider since the navigation bar is wider then the menu, and changing the list items to display inline and floating the links left to get them to display next to each other instead of stacked on top of each other.

Complete Navigation Bar (x)HTML And CSS

The complete code for our simple navigation bar is:


<ul id="list-nav">
<li><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Contact</a></li>
</ul>

ul#list-nav {
list-style:none;
margin:20px;
padding:0;
width:525px
}

ul#list-nav li {
display:inline
}

ul#list-nav li a {
text-decoration:none;
padding:5px 0;
width:100px;
background:#485e49;
color:#eee;
float:left;
text-align:center;
border-left:1px solid #fff;
}

ul#list-nav li a:hover {
background:#a2b3a1;
color:#000
}

Just as with the simple menu I hope you’ve seen how is it is to create a simple navigation bar. The (x)HTML is as simple as it can be and even the CSS isn’t too complicated. By changing the CSS a little you can easily get different size bars or different colored bars. It would also be rather simple to use an image behind the links as opposed to a block of color. The power here is really in the css. Compare how easy it was to change a menu into a navigation bar with just a few minor changes and then apply a little creativity to style your own navigation bars from a basic (x)HTML list.

Popularity: 100%

Spread some karma These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • StumbleUpon

Subscribe to TheVanBlog | Email This Post Email This Post

28 Responses to “Simple Navigation Bar With CSS And (x)HTML”

MyAvatars 0.2
anna
2006-05-19 04:48:04

this is easy to understand and use, i like it :) 10/10 for me.

 
MyAvatars 0.2
2006-06-26 23:52:13

Thanks for this! I’ve been looking for navigation bar code to use on my fledgling site.

I will add this important navigation feature soon!

In the meantime, hope you’ll stop by - and feel free to refer others looking for expert writing services.

Best,
Barbara Sealock
write-to-the-point

 
MyAvatars 0.2
2007-05-28 08:14:50

Thanks! I’ve been looking for a nav bar for awhile and I think I found it. I’ve been thinking about changing the look of my website and this helps.

Thanks again.

 
MyAvatars 0.2
2007-05-29 19:56:31

Glad I could help. When you have a menu or navigation bar think lists first. If you get good at styling lists with css you can create some nice menu systems.

 
MyAvatars 0.2
Miles
2008-02-06 22:20:02

Thanks for the info…I am in a web programming class now and will be using this nav bar for my 1st lab. I do have a question. How would I center the nave bar on my page ussing CSS? Right now it shows to the left. Any help would be greatly appreciated.

Thanks

Miles

 
MyAvatars 0.2
2008-02-06 22:24:06

Thanks Miles. I actually wrote a post on centering with css so I’ll direct you to that post to answer your question.

Centering With CSS: Web Pages And Block Level Elements

I hope it helps and feel free to ask questions if it doesn’t.

By the way I’m sorry your comment never went through. Every so often the comments here hiccup and don’t seem to want to go through. I manually entered it to make sure it would work. Sorry about that.

 
MyAvatars 0.2
Duy
2008-02-14 01:12:10

I found your website very useful and thank you so much for your info. I am doing my homepage for my website (http://www.bongdavietnam.org) and I have the same problem as Craig did. My navigation bar appears on browsers as a list instead of inline even though it appeared as inline in Dreamweaver. Could you please help me with this?

Best,
Duy

 
MyAvatars 0.2
2008-02-14 01:19:44

Thanks Duy. I think the problem you’re having is a simple one. It looks like your css file isn’t linked correctly. At the end of your link tag you have this:

type=”text/css MEDIA=screen”

Change it to:

type=”text/css” media=”screen”

and I think you’ll find your navigation bar will work.

 
MyAvatars 0.2
Debbie
2008-06-16 17:00:56

Thank you very much. Your instructions are helping me finally learn CSS. I appreciate the help.

MyAvatars 0.2
2008-06-17 18:05:28

Glad I could help Debbie. If you have questions feel free to ask. I’ll be happy to answer and try to help.

 
 
MyAvatars 0.2
2008-08-23 23:51:19

this is helpful for me since i want to organize my page. but the links and profile still appears in my page. can u help me with this..i appreciate it.

MyAvatars 0.2
2008-08-24 00:19:40

I’m not sure what you mean by the links and profile still appearing. Is your page online? if so do you mind posting a link to it so I can take a look? I’ll be happy to help if I can.

 
 
MyAvatars 0.2
2008-08-24 13:27:34

ahm, what i means is that i want my About Me, Blogroll, and others to be organized using the navigation menu. I tried your codes here and it looks good. but my problem is that my About Me and Blogroll still appears in my page though i already have the navigation menu. What will i do? I will highly appreciate ur help. Pls pls teach me how.

MyAvatars 0.2
2008-08-24 16:06:26

I understand now. The navigation bar here wasn’t really written for WordPress or any specific CMS for that matter. It’s meant as a simple thing for a static site.

You can use the basic ideas and incorporate it into your CMS. I’m essentially using similar code to what I have in this post on the site right now, which is running on WordPress.

You’ll have to look where the extra About Me and Blogroll links are appearing and remove the code that’s showing them. The details will depend on the type of CMS you’re using (I’m guessing WordPress) and how your specific site has been set up. I couldn’t really give specifics how without being able to see the files, since there’s more than one way the code could be there.

 
 
MyAvatars 0.2
2008-08-24 18:50:15

hi..i am using blogger. so if i delete the codes of About Me and Blogroll in the html section of the template they will not appear in my page but in the navigation menu instead? tnx for d reply steve.

MyAvatars 0.2
2008-08-24 19:46:34

I’ve never worked with Blogger so I can’t say for certain, but I think I can guess a little at how it’s put together.

I would think there’s a line or two of code to display the About Me page and the Blogroll. My guess is that the same line(s) of code can be used anywhere in the template so removing them from where you don’t want to see them and adding them into the navigational menu should be right.

But to be on the safe side make a copy of the template or at least the lines of code you remove so you can put things back just in case.

I’m happy to try to help even if all I can do is guess a little.

 
 
MyAvatars 0.2
2008-08-24 19:58:40

hi steve.tnx for d reply.will try working on it now. i hope it will look okay :) will message u again ok.

MyAvatars 0.2
2008-08-26 13:02:10

I hope everything works. Let me know if you have any more problems. I’m not sure how much help I can be since I don’t know Blogger well, but if I can point you in the right direction I will.

 
 
MyAvatars 0.2
Andrew Subscribed to comments via email
2008-09-03 18:31:35

Okay, I started learning HTML today and I’m just messing around, and this might be a stupid question but whatever…

I have the beginning navigation things that have the bullets and is a list, but when you start talking about changing the style, where do u put those codes or whatever?

MyAvatars 0.2
2008-09-04 14:57:28

There are different ways to style html elements. The preferred method is to use css. You can include css inline (directly in the html element), in the head section of the html document itself, or by linking to an external style sheet.

External style sheets are really the best method.

Here’s a really good beginner css tutorial from W3Schools. If you click the Home link at the top of the menu on the left you can also find tutorials on html, javascript, and most any other technology you would use building a website.

CSS isn’t really any harder to learn than html. Using it to layout a site can take some time and practice and is something of an art, but things like making an item in a list a certain color are fairly easy.

 
 
MyAvatars 0.2
2008-09-25 00:20:28

I think this code would be useful for me. I have a lot to learn though. I have questions about how to be able to place navbar code on a page and then then to have it appear on all my site pages w/out having to copy and past the code individually. I mean, if I have to do that it could get to be a very time-consuming process.

MyAvatars 0.2
2008-10-01 17:53:51

Julie what you’ll want to do is use an include statement. It’s something I should write a post about. You can include other files into your page with any server side language. My choice is to use PHP.

The basic idea is that the code for your navigation would be located in one file and then each of your pages would use PHP (or again any server side language) to include that file on the page.

The first thing to do is decide what programming language you want to use and gain some familiarity with using it. Includes are fairly easy and you don’t need to know too much in order to use them.

I’ll put writing a tutorial about includes on my list for a future post.

MyAvatars 0.2
2008-11-06 05:16:35

Yeah, I was wondering about that. I was thinking perhaps I would use PHP or Javascript. I realize it would involve making a master page and then including a link to that master page into each web page. Then when I update my navbar It automatically changes when I add new pages right?

BTW, I will be looking for that new post you are going to do regarding this. Please inform me as to the link of it when you do. I would greatly appreciate it.

 
MyAvatars 0.2
2008-11-06 05:17:34

Forgot to say to notify me by e-mail, thus this additional post.

(wrote my reply to your reply. Mine should be right above this.)

MyAvatars 0.2
2008-11-06 19:13:29

Will do. It may be awhile before I can get to it, but I’ll let you know when I do. Of course you’re always free to subscribe to the blog via rss or email. :)

 
 
 
 
MyAvatars 0.2
Dan
2008-11-05 15:44:18

Hey, this is a pretty impressive tutorial - I found it through google ;)

My querie is this:

I am playing around with a few layouts for websites which I will eventually use for my course, and with your Navbar, I was thinking about placing it in the center of the web page. Now I have done the, but it is off center due to the float command aligning it slightly to the left. Removing this command, or changing it to ‘right’ doesnt not give out the desired effect without changing the appearance of the cells themselves.

Any help on moving this bar dead center in my page would be a great help, thanks :)

MyAvatars 0.2
2008-11-06 19:23:39

Thanks Dan.

Try changing margin:20px to margin: 20px auto on ul#list-nav.

You can change the 20px to whatever suits your needs too. That’s usually how I center things in css. If it still doesn’t work try wrapping the list with a div or some other element and try centering that wrapper div.

The key is making sure the margin is set to auto for the left and right and also specifying a width on the element. You also need a proper doctype for IE.

I wrote a post on centering with css with more details.

If you still can’t get it to work let me know and send me a link to the page if it’s online so I can take a look.

 
 

What others are saying about this post

quicker alignment method
[...] If you don't mind using a little css here's an article I wrote on creating a simple navigation bar. ...

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment

Subscribe

Search TheVanBlog

Recommended

Small Business Forum SEO Book Training

Lunarpages.com Web Hosting

Popular Posts