Wednesday, August 31, 2011

CSS3 and HTML5 Example 1: Navigation Bar


Having good and easy to navigation is important for any site.

With CSS you can transform boring HTML menus into good-looking navigation bars.

Navigation Bar = List of Links

A navigation bar needs standard HTML as a base.
In this example we build the navigation bar from a standard HTML list.
A navigation bar is basicly a list of link, using <ul> and <li> elements makes perfect sense:

<ul>
<li><a href="default.html">Home</a></li>
<li><a href="news.html">News</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a hreft="about.html">About</a></li>
</ul>

Now let's remove the bullets and the margins and padding from the list:

ul
{
list-style-type:none;
margin:0;
padding:0;
}

The code in the example is the standard code used in both vertical and horizontal navigation bars.

Vertical Navigation Bar

To build a vertical navigation bar we only need to style the <a> elements, in addition to the code above:

a
{
display:block;
width:60px;
}

Horizontal Navigation Bar

There are two ways to create a horizontal navigation bar. Using inline or floating list items.

Both methods work fine, but if you want the links to be the same size, use the floating method.

Inline List Items

One way to build a horziontal navigation bar is to specify the <li> elements as inline, in addition to the "standard" code above:

li
{
display:inline;
}

Floating List Items

In the example above the links have different widths.

For all the links to have equal width, float the <li> elements and specify a width for the <a> elements:

li
{
float:left;
}

a
{
display:block;
width:60px;
}


8 comments:

  1. I need to get an update, I was left behind on HTML4

    ReplyDelete
  2. Good stuff, I'm learning so much by visiting your blog daily.

    ReplyDelete
  3. Nice tutorial bro.

    ReplyDelete
  4. Dig it. I might try to use this...

    ReplyDelete
  5. Excellent stuff! I can surely use this.

    ReplyDelete
  6. Useful information. This is my favorite blog about computer languages.

    ReplyDelete
  7. ahhh, that doesnt seem too bad... gonna try it!

    ReplyDelete