tags:

views:

4975

answers:

4

I'm trying to create a horizontal navigation bar in css with 5 evenly spaced links. The html hopefully will remain like this:

<div id="footer">
 <ul>
  <li><a href="one.html">One</a></li>
  <li><a href="two.html">Two</a></li>
  <li><a href="three.html">Three</a></li>
  <li><a href="four.html">Four</a></li>
  <li><a href="five.html">Five</a></li>
 </ul>
</div>

So with CSS, I want to space them evenly within the footer div. So far I'm using this:

div#footer{
 height:1.5em;
 background-color:green;
 clear:both;
 padding-top:.5em;
 font-size:1.5em;
 width:800px;
}
div#footer ul{
 margin:0;
 padding:0;
 list-style:none;
}
div#footer li{
 width:155px;
 display:inline-block;
 text-align:center;
}

This works pretty well, but there is spacing between the li's that I do not want. That is why I've used the 155px instead of 160px for their width, there is about 5px of space being put in between each li. Where is that spacing coming from? How can I get rid of it? If I increase the fontsize, the spacing increases as well.

+4  A: 

If you use this:

div#footer li{
  width:160px;
  display:block;
  float: left;
  text-align:center;
}

It all looks lovely :D

mercutio
I suggest adding #footer {overflow:hidden} for easy clearing of floats (it won't actually hide anything).
porneL
+5  A: 

I've had this happen to me. Unfortunately it is caused by the browser taking the line breaks between the list items and rendering them as spaces. To fix, change your HTML to:

<div id="footer">
  <ul>
    <li><a href="one.html">One</a></li><li><a href="two.html">Two</a></li><li><a href="three.html">Three</a></li><li><a href="four.html">Four</a></li><li><a href="five.html">Five</a></li>
  </ul>
</div>
Andrew G. Johnson
Isn't IE the only one with that behaviour?
mercutio
I'm seeing it in Firefox
sblundy
It's standard behavior caused by display:inline.
porneL
I don't recall which browser(s) caused this, but frankly if it happens in either MSIE or FF then its something you have to find a fix for. At least that's how I look at it.
Andrew G. Johnson
+2  A: 

The spaces between your <li> elements are due to the whitespaces and carriage returns between them, due to the inline style. If you write :

<li><a href="one.html">One</a></li><li><a href="two.html">Two</a></li><li><a href="three.html">Three</a></li><li><a href="four.html">Four</a></li><li><a href="five.html">Five</a></li>

You'll see no more space between them.

I'm not sure if inline-block's will display nicely on IE6, so you may want to try the float approach.

vincent
inline-block works since IE5 - it's Microsoft's invention. It's totally broken in Firefox 2 though.
porneL
So, that was that part of inline-block that was broken! Not because IE didn't support it, but because IE invented it ;-)
vincent
A: 

div#footer ul{ margin:0; padding:0; list-style:none; }

div#footer li{ width:155px; float:left; display:block;
}

This code positioned li horizontally while the spaces between them. If you want to adding space between li elements:

div#footer li{ width:155px; margin-right: 5px; //5px Space between li elements float:left; display:block;
}