tags:

views:

46

answers:

1

I have

<div id="container">
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>

With the following CSS:

#container li {
float:left;
height:33px;
line-height:30px;
text-align:center;
width:auto;
}

#container{
 width:600px;
}

However I am having difficulty in horizontally centering the ul inside #container. It appears that I need a fixed width set on the ul for margin: 0 auto to work (needs text-align: center for IE). However I do not want to set a fixed width for the ul as the li items will be dynamic.

Please advise.

+3  A: 
#container {
    width: 600px;
    text-align: center;
}

#container ul {
    display: inline-block;
}

#container li {
    float: left;
    height: 33px;
    line-height: 30px;
}
reko_t
Thank you but doesn't inline-block not work in IE6 (and IE7?)?
cadaa
You can try this trick to make it work with IE (I can't verify whether it works, as I'm on a Mac right now). Add this to `#container ul`: `zoom: 1; *display: inline;` The rationale behind this is explained here: http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/
reko_t