View Full Version : horizontal <UL> navbar without floated LIs?

05-13-2008, 10:16 PM
the traditional way to make a horizontal navigation menu involves setting the UL to list-style: none; and the LIs to display: inline or inline-block and float: left or right.

without the floats, there is space between the LI items once they are arranged inline. Also when you float the LIs, the UL container needs to be resized manually to match the height/width of the LI elements (in the case when you want the menu to have a uniform background), and this is never guaranteed to be pixel perfect.

is there a way to get rid of the horizontal space between LI elements without having to float them?


05-13-2008, 10:24 PM
The spaces with inline elements is caused by the line breaks of the list. You can avoid that by putting ending and starting tags of adjacent items right next to each other like so:


However, you can also have the ul resize along with floated lis. Either apply a float to the list itself (→ containing floats (http://complexspiral.com/publications/containing-floats/)) or apply overflow: hidden or auto to it (IE 6 needs layout (http://satzansatz.de/cssd/onhavinglayout.html), too).

05-13-2008, 10:26 PM
also i think i just found that display: table-cell; applied to LIs does something similar.

05-13-2008, 10:27 PM
In Firefox the gaps appear to be created by whitespace in the source between li elements. Remove the whitespace to get rid of the gaps. This results in less manageable/readable code, but that's the trade-off. In IE(6), just setting display:inline results in no gaps.

<style type="text/css">
ul { list-style: none; margin: 0; background: red; }
li { display: inline; background: black; margin: 0; padding: 0px; }
a { color: white; }
<li><a href="#">Home</a></li>
<li><a href="#">Link One</a></li>
<li><a href="#">Somewhere Else</a></li>
<li><a href="#">End</a></li>
<ul><li><a href="#">Home</a></li><li><a href="#">Link One</a></li><li><a href="#">Somewhere Else</a></li><li><a href="#">End</a></li></ul>
Ack! I'm slow. Hope the example helps.

05-13-2008, 10:29 PM
also i think i just found that display: table-cell; applied to LIs does something similar.

That doesn’t work in IE, though.

05-14-2008, 04:12 AM
here is my specific example:

<div style="width: 950px; height: 100px; border: 1px solid red;">
<ul id="nav_top">
<li><a href="home.php">Home</a></li>
<li><a href="products.php" class="current">Products</a></li>
<li><a href="shop.php">Shop Online</a></li>
<li><a href="about.php">About Us</a></li>
<li><a href="support.php">Support</a></li>
<li><a href="testimonials.php">Testimonials</a></li>

ul#nav_top {
width: 100&#37;;
float: left;
font-size: 10.5pt;
margin: 0;
padding: 0;
list-style: none;
border-top: 1px solid #646464;
border-bottom: 1px solid #646464;
background: url(nav_bg.png);
#nav_top li {
display: inline;
padding: 0;
#nav_top li a {
float: left;
width: 158px;
padding: 3px 0;
border-right: 1px solid gray;
color: black;
font-weight: bold;
text-align: center;
text-decoration: none;
text-transform: uppercase;

#nav_top li a:hover, #nav_top li a.current {
background: url(nav_bg_hov.png);
color: maroon;

how would i prevent wrapping of the last list item? the goal is to hide/overflow the right border of the last element just a tad so that when it is selected, the list item coloring/background goes all the way to the edge, like the first menu item on the left side.

setting overflow: hidden on the nav_top did not help :(.