2

Is it possible somehow to center a left-aligned list? Like, I want the list itself centered, but want the bullet points to line up below each other.

I know I could sort of do this if I give the list a fixed width and margin auto, but I really don't want to set a width on it since I don't know how wide the content will be.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Svish
  • 152,914
  • 173
  • 462
  • 620

1 Answers1

10

Use display: inline-block on the ul, and text-align: center on a parent element.

See: http://jsfiddle.net/thirtydot/E3Yjr/

CSS:

.list-container {
    text-align: center;
}
ul {
    display: inline-block;
    text-align: left;
}

HTML:

<div class="list-container">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item Looooooooooooooooooooooooonggg</li>
    </ul>
</div>

If you need it to work in IE7, here's how: http://jsfiddle.net/thirtydot/E3Yjr/1/

display: inline-block;
*display: inline;
zoom: 1;
Community
  • 1
  • 1
thirtydot
  • 224,678
  • 48
  • 389
  • 349
  • What a smart idea, kudos. [Won’t work in IE 6 & 7 or Firefox 2](http://www.browsersupport.net/CSS/display%3Ainline-block), but other than that, you’re golden. – Paul D. Waite Jan 26 '12 at 13:50
  • 2
    @PaulD.Waite: Cheers. `display: -moz-inline-stack; display: inline-block; zoom: 1; *display: inline;` [would make it work](http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/) in IE6/7 + Firefox 2 (but nobody cares about Firefox 2 any longer). – thirtydot Jan 26 '12 at 13:55
  • aha, yes — I remember reading that article and not being able to get `-moz-inline-stack` to work, but I don’t think I noticed how to get `inline-block`-style behaviour in IE 7 and 6. Great stuff. (And I entirely agree re Firefox 2.) – Paul D. Waite Jan 26 '12 at 14:50