Simple Menus With (x)HTML Lists And CSS

Steven Bradley

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

«

»

When it comes to creating a menu for your site one of the easiest ways is to use a simple unordered list as your (x)HTML structure and then style it using css. With the right styling you can even achieve some creative effects Let’s get right into the code and build a simple menu.

(x)HTML List

The structure of the menu is just an unordered list with links inside each of the list items.


<ul id="list-menu">
<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>

Nothing really exceptional to this list. You would naturally add real URLs to your pages inside the href attribute. The only thing I added was an id of ‘list-menu’ just so we can refer to the menu later. The code as we have it now will look like:

Styling The Menu With CSS

The (x)HTML only menu is certainly workable and I’ve used something as simple in sites before. What we’re after here is a little something more so let’s dress it up a little with some css.

One issue you may come across with lists is where that indent in the list comes from. Firefox and IE indent the list using a different css property. In Firefox it’s the padding that indents the list and in IE it’s the margin. The easiest way to get them both in line with each other is to set both margin and padding to 0. From there I like to add back some margin to get the menu where I want. Let’s also turn off the bullets since we won’t be using them here and let’s give the menu a width. We’ll let the height take care of itself.


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

For the links we’ll take out the underline and give them a little padding, a text color, and background color. The most important thing we need to add to the links is to get them to work the way we want is display:block. Setting the display property of the links to block will get each to expand to fill their container, in this case the list items, instead of just being the size of just the anchor text.


ul#list-menu li a {
text-decoration:none;
display:block;
padding:5px;
width:140px;
background:#485e49;
color:#eee
}

I’ve set the css to act on ul#list-menu li a. The li isn’t really needed and the css could also have been applied to ul#list-menu a or even just #list-menu a, but I used the longer form to be more specific when it comes to cascading rules. It’s not something that will be important here, but it can be depending on how complex the css for the rest of your page gets.

The two main things to pay attention to with the css above is the display:block which is what will really make the whole structure work. It’s what will make the link fill up the entire list item so anywhere we place our mouse will be a link instead of just the text. It’s easy to test this by taking the display:block out of the code and see how the menu looks. The other thing to pay attention to is the width. The width of the links is a little less than the overall list to account for the 5px padding on the left and right of the link. Adding the width to the links also fixes a problem with IE in the vertical spacing of the links within the list.

As we currently have it coded our menu should now look like the following:

It’s getting there, but we can still make it look a little nicer and give it some kind of rollover effect.

Final Touches

For a little extra style let’s give the menu and the individual links a border so they stand apart from each other a little more. We’ll do this is by adding a 2px border to the <ul> with the exception of the top which will only be 1px. We’ll then give the links a 1px top border. The 1px less on the overall list gets accounted for with the 1px border of the first link. The rollover effect can be kept simple as well. We’ll just change the color of the anchor text and also the background color. Here’s the new css:


ul#list-menu {
border:solid #668265;
border-width:1px 2px 2px 2px;
}

ul#list-menu li a {
border-top:1px solid #77a487;
}

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

For the menu I’ve used the border shortcut property for the style and color of the border and specified the widths with the border-width property since we want to use different widths. Since the border on the links is only on one side I just used the shortcut. Just as with the link I also used the specific selector when referring to the hover on the link. We now have our final menu which should look like:

Nothing to it. With just a very simple list structure and not so complicated css we have a pretty decent looking menu. With a little more css you could easily style the list more and achieve some interesting effects. For an example take a look at Office Support 911 which is a site I recently designed and developed. The menu on the site adds a couple of images for the handle of the file cabinet and a little javascript to make it all work in IE, but at it’s core it’s nothing more than a styled list.

Complete Menu (x)HTML And CSS

The complete code for our simple menu is:


<ul id="list-menu">
<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-menu {
list-style:none;
margin:20px;
padding:0;
border:solid #668265;
border-width:1px 2px 2px 2px;
width:150px
}

ul#list-menu li a {
text-decoration:none;
display:block;
border-top:1px solid #77a487;
padding:5px;
background:#485e49;
color:#eee
}

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

I hope you’ve seen how easy it is to create a simple menu. Lists can also be used to create horizontal navigation and by nesting lists with just a pinch of JavaScript you can create some nice dynamic drop down menus. So the next time you build a menu for a site use an HTML list structure, some css styling, and a little of your own creativity for some nice effects that will be much easier to code than they look.

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

23 Responses to “Simple Menus With (x)HTML Lists And CSS”

MyAvatars 0.2
kaloba haron
2007-03-12 15:11:36

please i would like to know more about the designing of website especially the way how you designs the dropdown menu using some codes from html because i fing it some difficult that is only my request.

 
MyAvatars 0.2
2007-03-12 15:15:28

My apologies kaloba haron. For some reason your comment didn’t go through initially or perhaps I accidentally deleted. Either way please accept my apology.

I’ve been using the suckerfish code when I need drop down menus. I think it’s pretty easy to use, though you may need to spend a little time looking at the code to understand what it’s doing. The system is also very lightweight and validates.

I hope that helps.

MyAvatars 0.2
kimbettara Subscribed to comments via email
2010-01-19 16:15:31

steven do you know where i can get a template for a easy temple plate for a recipe site all i want is something that i can add the name of a recipe and then click on that name to go to the recipe do u have ant ideas where i can get this templte for my site please any help would be apprecated

MyAvatars 0.2
2010-01-25 15:22:43

I don’t know of any off the top of my head, but I would think recipe site templates or themes would exist. You might need to search around a little and accept that what’s available isn’t exactly what you want, but you should be able to find something at least close to what you’re looking for.

 
 
 
MyAvatars 0.2
Fred Bollinger
2008-06-30 16:25:30

Example CSS doesn’t look so great in IE7. I noticed that your own CSS to make the example work doesn’t match the example given.

MyAvatars 0.2
2008-06-30 16:55:59

I’ll have to take a look. In all fairness to me though, this post was written long before there was an IE7 so it was kind of hard to test in it at the time.

Thanks for pointing it out. It’s probably (hopefully) something minor that I can tweak.

The only difference between the css code as presented and the actual css used in the example should all be for formatting within this post and so as not to conflict with the rest of the css for the site.

Thanks again for pointing it out. This post is a couple years old now and can probably use some updating.

 
 
MyAvatars 0.2
Behzad Subscribed to comments via email
2009-01-14 20:58:28

Your menu is awesome.
So great simple and useful.
Thank you so much
I love it this is what i was looking for .

MyAvatars 0.2
2009-01-20 12:56:36

Thanks Behzad. I’m glad you found the menu helpful and easy to use. I appreciate the compliment too.

I don’t always succeed, but I do try to keep things simple.

 
 
MyAvatars 0.2
2009-05-28 18:10:53

question can you make a css header or footer or sidebar appear on all of your web pages or is it better to do a php include with a header php file.

let me know thanks

trying to figure out the best way to do this.

thank you.
Scott

MyAvatars 0.2
2009-05-28 20:33:27

Scott the best way is to use the include file. It doesn’t have to be php, of course. You can use any server side language. Just about anything that’s going to consistently repeat from page to page across your site (header, footer, sidebar) is best to keep in a separate file and then use php (or other) to include the file on the page.

It makes maintaing your site much easier.

Also keep your css and javascript in their own external files. The more modular you make your site the happier you’ll be when it comes time to change something.

 
 
MyAvatars 0.2
2009-05-29 10:25:41

ok excellent, thank you, I really appreciate the help.

just to make sure on one point though, their is no CSS include script to appear on all your web pages you have. Correct (besides sprites).. I can’t use CSS solely to do this? or can I.

I believe it has to be php, asp, correct for the include tag.

do you have a reccomendation on how you would do this?

I appreciate the help.

thank you.

Scott White

MyAvatars 0.2
2009-06-09 21:57:53

Sorry it too me so long to respond.

With css you include your external file sheets using a link tag. It should look something like this:

<link rel=”stylesheet” href=”path-to/style.css” type=”text/css” media=”screen” />

There’s also an @import command, but the link tag is the preferred way to include your stylesheets.

 
 
MyAvatars 0.2
Barrett Smith Subscribed to comments via email
2009-10-09 14:51:11

Two part question:

Using IE7 I am having a problem with empty white spaces between the menu items. I have tried playing with the spacing, but I can’t seem to get it to go away. Any suggestions?

Second, I am trying to combine a horizontal navigation bar, with a vertical menu bar on the left without a space between them. (So there would be a nav bar on top, a menu on the left, and text in the middle without using frames). Any suggestions there?

Thanks a lot for the great site!

MyAvatars 0.2
2009-10-14 02:46:32

Thanks Barrett.

If you want to point me to the site in question I’ll be happy to take a look and see if I can figure out what the problem might be.

I’m guessing that the space between the menu items is the background behind the menu showing through. Did you apply a background color to the ul or just the li tags? Am I right that you applied a background color?

For your second question check out a post I wrote awhile back on 2 column css layouts. You can put the nav bar inside the header and then the menu inside the left column. There’s no reason why there has to be a space between things.

See if you can get that working. Sometimes different elements inside the header or content or sidebar areas will create spacing due to their defaults. For example an h1 tag will have some margin on it by default and that margin pushes the whole content area down a little leaving a space at the top.

You can use a css reset file to remove the defaults on many elements.

MyAvatars 0.2
Barrett Smith Subscribed to comments via email
2009-10-14 14:40:32

Thanks a ton! The 2 column layout totally saved me so much time and stress!

The only other question I have now is in regards to the “menu” section of the 2 column layout. Is there a way to make the “menu” scroll down with the page so that the links there are always visible on the page, no matter how far down a page a user scrolls?

Thanks a bunch for the help.

MyAvatars 0.2
2009-10-15 13:28:04

If you mean where the menu has a noticeable movement as you scroll then that’s usually done with JavaScript.

If you mean where the menu itself never moves, but everything else can scroll up and down that’s done with css positioning. You’d set the position of the menu to be fixed.

What I think you’d want to do is add position: fixed to the menu itself, not the floated div that creates the column containing the menu.

 
 
 
 
MyAvatars 0.2
kimbettara Subscribed to comments via email
2010-01-19 16:22:23

it like applepie and then i click on applepie the word and then the recipe will show up nice and simple

 
MyAvatars 0.2
kimbettara Subscribed to comments via email
2010-01-25 18:40:18

thank you for a reply back

 
MyAvatars 0.2
Mike Tanner
2010-02-02 17:47:01

Very impressed with the clarity of your tutorial, and your willingness to help

However I seem to be having the same problem as others in that the buttons appear as bullet points and the code appears as text.
I think I have code safe within the and tags, so I assume I’m missing something obvious

Here’s one of the offending pages
http://park99.co.nz/dr1.html

Appreciate it if you have time to check it

 
MyAvatars 0.2
2010-02-03 21:42:34

I’m glad to help Mike. I took a look at your post and see the problem and yes it’s a simple one.

You added the css code directly below the html. Unfortunately the css won’t work there. My bad for not mentioning this in the post, but your css needs to be located either in the head section of your html document or in a separate css file that you include in the document.

I see you’re linking to a style.css file so the easiest thing for you would be to remove the css for the menu from the html file and place it in the style.css file.

The code starting with <ul id=”list-nav”> and ending with </ul> can stay right where it is. All the code that follows is the css and you’ll want to move it into style.css.

Also I’m looking at your style.css file and you have code in it that you don’t want or need. You’ve added html tags in the css file. You can remove the opening and closing html, head, and style tags in the css file. You also have a closing body tag that should be removed too.

Then you should be able to add the css code I’ve provided here in the file and the menu should work. If it doesn’t just let me know and I’ll take another look.

 
MyAvatars 0.2
Erika
2010-02-12 22:59:24

Hi there,

I was wondering if you could help me. I have the code exactly as you show us, but I seem to have this space inbetween each link. I don’t know what I did wrong…

Here’s my website link so you can see:
http://www.freewebs.com/xxwritten-deathxx/

MyAvatars 0.2
2010-02-13 01:16:52

Hi Erika. I’m guessing you’re looking at the page in Internet Explorer. Just so you know the extra space isn’t there in Safari or Firefox.

IE has a weird bug where it places extra whitespace in the lists. One way to fix it is to remove the whitespace in the code. Instead of having the list items be on new lines, you can place them all next to each other on the same line in your code. It’s not the best solution though.

It’s been awhile since I’ve had to deal with this issue, but I think what you need to do is set a height on:

ul#list-nav li

If I remember correctly I used to set the height to be something very small like 1px or 1%.

Try that and see if it works.

MyAvatars 0.2
Erika
2010-02-13 11:57:55

Yes, I was looking at it in Internet Explorer, but your help fixed the problem. Thank you very much

 
 
 


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

Subscribe

Search TheVanBlog

Recommended

Small Business Forum Teaching Sells Free Report

SEO Book Training

Popular Posts