spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / css_lists / 1 To page 1To page 2current page
[previous]

Data Center Architect
The Computer Merchant, Ltd
US-MA-chelsea

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


How to Use CSS to Position Horizontal Unordered Lists

The <a> tag

.menu a {
  display:block;
  padding:0.2em 1em; 
  background:#fc6; 
  color:#000; 
  text-decoration:none;
  border:1px solid #000;
  }

The <a> tag is almost identical to the 'normal' method. The only difference is that the float:left; of the 'normal' method had been replaced with display:block;.

Alternative a styling

All browsers except Internet Explorer have the links horizontal with the red border a snug fit to the outside of the list. Internet Explorer has the list vertically aligned instead of horizontal.

Again, we need to feed an alternative style to Internet Explorer.

* html .menu a {
  display:inline-block;    /* for IE only */ 
  margin:0 -2px;           /* to correct an IE bug that doubles the border width */  
  }

Alternative a styling (IE)

Internet Explorer is now back in line with the rest of the browsers.

The <a:hover> style

.menu a:hover {
  color:#fff; 
  background:#08c;
  }

This is identical to the 'normal' styling and is suitable for all browsers.

Alternative a:hover styling

Positioning

To show that this styling can now be used to position the menu as desired (without the need for extra surrounding divs or any other hack), I'll alter the <ul> styling to position the menu centrally using margin:0 auto; (which would not work with the 'normal' styling method). I'll also remove the red outer border as it is not necessary.

The <ul> tag

.menu {
  display:table;
  padding:0;
  margin:0 auto;
  font-family: arial, helvetica, sans-serif;
  white-space:nowrap;
  list-style-type:none;
  }

This will work in all browsers except Internet Explorer pre IE6 which will require the usual centering hack using text-align:center;

Alternative centered styling

Conclusion

I would recommend that the 'normal' method of styling horizontal unordered lists to be used, but if you're having problems with positioning your lists, this alternative method might be the answer. It does require hacks for Internet Explorer, but the final styling is not too complicated (although it does use 'non-standard' css).

To put the finishing touches to the styling of this menu, I'll demonstrate the 'easy way' to add a 'current' selected menu item to the styling.

If you need to show your visitors which menu item has been selected (current) then add the extra style requirements as follows:

The 'selected' style

.current {
  color:#fff; 
  background:#08c;
  cursor:default;
  }

The (X)HTML can then be modified to add this class to the current list item as shown below...

The (X)HTML

<ul id="menu">
<li><a class="current" href="#nogo">Item one</a></li>
<li><a href="#nogo">Item two</a></li>
<li><a href="#nogo">Item three</a></li>
<li><a href="#nogo">Item four</a></li>
<li><a href="#nogo">Item five</a></li>
</ul>

...and the navigation list will look like this:

Alternative centered styling with current selected item

Using the same colors as the :hover state will make it appear that :hover is not having any effect on the menu item and using cursor:default; will change the normal link indicator (hand) into the default arrow, giving the impression that this is not a link.

This is an easy solution. A better solution would be to remove the link tags and replace them with a span or em tag and style this as necessary.

About the Author

Stu's website documents his attempts at understanding and exploring the possibilities of CSS. From standard navigation links to his more bizarre experimental techniques. All of his examples are produced with JUST CSS--no javascript, or any other language, has been used in any of the examples. [Editor's note: Prepare to be amazed!] http://www.stunicholls.myby.co.uk/

home / programming / css_lists / 1 To page 1To page 2current page
[previous]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: August 26, 2005

URL: http://webreference.com/programming/css_lists/1