Building a horizontal menu in just a couple of lines of CSS.

About Christopher Ross

I solve problems for people on the web. Sometimes that involves being a designer or a developer and other times it means I coach people, help people or market them online. You're welcome to contact me by email or by calling me at (416) 840-5828.

0 Responses | Share on Facebook Twitter Digg StumbleUpon

picture 11 300x192 Using CSS to build a horizontal menu imageOne of the most common questions I get asked is how to build a simple menu like the one at the top of my website, 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.

0 Responses | Share on Facebook Twitter Digg StumbleUpon

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  1. Infographics

    Percentage of People Who Prefer Pie Charts to Bar Charts, as an Infographic.

    pie chart info graphic

    July 30, 2010 (no comments)

  2. Make Money

    Make More Money With Fewer Ads

    Here's a little code snippet for WordPress users, designed to help increase both the click through rates of your ads and the amount each click is worth.

    July 28, 2010 (no comments)

  3. WordPress

    Will WordPress 3.2 Work on Your Web Host?

    This week, WordPress announced an end to support for PHP 4 and MySQL 4 in the upcoming release of WordPress 3.2. How will that affect your self hosted WordPress?

    July 27, 2010 (no comments)

  4. Marketing

    Get a Free PR7 Backlink and Protect Your Reputation!

    Wouldn't it be awesome if you could get a free link from a PageRank 7 website for simply protecting your own reputation? You can!

    July 24, 2010 (no comments)

  5. Great Sites

    Macleans.ca

    Macleans.ca - Canada News, World News, Politics, Business, Culture, Health, Environment, Education (20100603)

    July 21, 2010 (no comments)