Read my latest blog entry: Automatically Title Casing Titles in WordPress.
Subscribe to my RSS Feed or by Email.
  • Home
  • About
  • Services
  • Blog
  • Contact
Christopher Ross Building better web sites, one pixel at a time.

Using CSS to Build a Horizontal Menu


Welcome to my blog, please feel free to subscribe to my RSS feed or leave a comment.

One of the most common questions I get asked is how to build a simple menu like the one at the top of my web site, using pure HTML and CSS. Actually it’s wonderfully easy and I’m happy to share the process with you.

The HTML

First, you’ll need to write the proper source HTML for the menu to work. I use unordered lists <ul> with list item <li> tags to build the menu items, this is in keeping with web standards and ensures compatibility across multiple devices.

The code a typical menu should look something like this:

<div class=’menu’>
<ul>
<li><a href=’index.php’ title=’Home’>Home</a></li>
<li><a href=’about.php’ title=’About Me’>About Me</a></li>
<li><a href=’contact.php’ title=’Contact Me’>Contact Me</a></li>
</ul> 
</div> 

The CSS

Now, with the basic HTML out of the way we have to take a look at the CSS codes we need to make the menu work, and how each of the does it.

We want to define the menu as a class, not an ID since we might want to reuse the code in both the header and the footer. An ID can only be used once on a page, were a class can be used multiple times. There is no actual code for the menu tag, it’s just a holder for us.

The <ul> tag on the other hand needs to be altered. To do this, we add the following code:

.menu ul {
list-style: none;
text-align:center;
}

Simply put, let’s get rid of the bullet (•) and center the content. Next we need to tell the <li> tags to sit side by side instead of appearing as a list, and we need to make sure they don’t bump into each other.

.menu li {
display: inline;
margin: 0px 10px 0px 10px;
}

There we have it, a table-less menu which appears in the center of your site in just a few lines of CSS.

Other Posts of Interest

  • Turning your menu list into menu buttons
  • Using CSS to highlight a link
  • Using CSS to disguise a link.
  • Adding Rollovers to your HTML without JavaScript
  • How to center content with CSS and HTML
  • How to make a web site look good in multiple browsers.
  • Placing page content in Adobe Dreamweaver
  • Structuring your web site with Adobe Dreamweaver
  • Better HTML Helps Search Engine Results
  • Blog Roundup for the 9th of October 2008
Posted on: Wednesday, October 29th, 2008
Tags: css, html, menu
Posted in Learning | | Read more

Did you find this article useful? You're welcome to post a link to this website along with the title but please don't copy the whole article. You can also link back using the following code:

<a http://www.thisismyurl.com/tutorials/using-css-to-build-a-horizontal-menu/" rel="bookmark" title="Using CSS to Build a Horizontal Menu">Using CSS to Build a Horizontal Menu</a>

About the Author

Christopher Ross About the author.
Thanks for taking the time to read my blog. If you don't already know me, my name is Christopher Ross and I'm a web evangelist living in Fredericton Canada. When I'm not writing about the Internet, I'm helping small businesses improve their search engine performance or building better WordPress web sites for clients.

Please feel free to leave a comment below, or contact me if you have any questions.

1 Comment

|
comments rss [?] | trackback uri [?]

One Response to “Using CSS to Build a Horizontal Menu”

  1. Pingback: Turning your menu list into menu buttons :: Christopher Ross

Leave a Reply

TrackVia
iTunes
iTunes
Total Training DVDs
iTunes
iTunes
Total Training DVDs
iTunes
Tiger Direct HP
Best Of
Best Of
LEGO
Recent Comments
  • Geoserv: STUMBLED! I have done these things, have a great readershi, usually 1000+ pageviews per post but still no...
  • Ikki at SEO Blog: One step at the time - that’s the greatest phrase of all times! If you want to achieve...
  • Ikki at SEO Blog: Hey, congratulations!! Mine jumped from PR2 to PR3 - yes, not much but it is still an improvement :P
  • RNB Love Songs: The power of social marketing. It’s scary though and it really makes you question the...
  • PizzaForADream: Upsetting, really. The public is already jaded and untrusting enough with all the BS we’re fed...
  • PizzaForADream: Thanks for the insight. I’m new to this entire game and your insights are greatly appreciated.
  • Joe Blough: Interesting that the author (Christopher Ross) is Canadian, but he didn’t mention (or point out, or...
  • The Moneyac: Wow PR5 ? Are you serious ? Indeed that is really amazing, you should maybe make a report t about, and...
  • Ari Lestariono: Hi Chris, I love reading yourarticle on PR 5, mine is only PR 3 within 4 months, perhaps you can...
  • Donace: I always assumed All in one SEO etc did this automatically? (or so it looked like to me). btw how would one...
Top Commentators
  • Make Money Online (3)
  • RNB Love Songs (3)
  • Mike Collins (2)
  • PizzaForADream (2)
  • Webkinz (2)
  • Donace (2)
  • Blogging Tips (2)
  • Ikki at SEO Blog (2)
  • Technology and Gadgets (1)
  • Ari Lestariono (1)
© Copyright 2009 Christopher Ross. All rights reserved.
  • Follow my posts:
  • RSS
  • Comments
  • Email
  • Other:
  • Privacy