tags:

views:

50

answers:

4

I'd like to center a list of left-aligned items.

This is what I currently have:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
  <head>
    <title>Shrinkwrapped List</title>
    <style type="text/css">
      #wrapper {
        margin: auto;
        width: 500px;
        border: solid 1px #CCCCCC;
      }
      #header {
        text-align: center;
       font-size: 200%;
      }
      #content {
        text-align: center;
      }
      #content ul {
        text-align: left;
        font-size: 150%;
        list-style-type: none;
        margin: 20px auto;
        padding: 0px;
        border: solid 1px #666666;
      }
    </style>
  </head>
  <body>
    <div id="wrapper">
      <div id="header">
        Shrinkwrapped List
      </div>
      <div id="content">
        <ul>
          <li>Lorem ipsum</li>
          <li>Dolor sit amet</li>
          <li>Consectetur</li>
          <li>Adipiscing elit</li>
          <li>Morbi odio</li>
          <li>Mi blandit vehicula</li>
        </ul>
      </div>
    </div>
  </body>
</html>

Which produces a page that looks like:

Not really what I want

What I really want looks like this:

This is the look I want

I can accomplish this by adding width: 200px; to #content ul but the problem is that I have a lot of lists like this and they all have various widths.

I'd like the <ul> to shrink to the content so it can be centered correctly. Or am I going about this the wrong way?

Thanks for any help you can provide.


Solution

Thanks to KennyTM and Magnar, here is the solution:

Add these four lines to #content ul's CSS rules:

display:-moz-inline-stack;
display:inline-block;
zoom:1;
*display:inline;

I've tested this in IE6, IE7, IE8 and Firefox 3.6. The results looks like the second image above and the list always fits to the content of the items.

A: 

I'd look at using CSS and putting a margin: 0 auto on the <ul> with a maximum width container.

DavidYell
If you do this, I think that if your ul is only 100px for example, but your maximum width container is 200px, the li's will not look centered in the page, but rather left aligned inside the 200px width ul. see what i'm talking about here http://jsbin.com/ubege
Catfish
Both of those are already done in the example provided in the question. `#content ul` has `margin: 20px auto` and its container div `#content` has is maximum width by default.
T Pops
Ah sorry, my bad :)
DavidYell
+1  A: 

Set the <ul> to use display: inline-block;. See http://jsbin.com/atizi4.

Note that inline-block is not supported (completely) for IE ≤7.

KennyTM
`display: inline-block` works **perfectly** here. Unfortunately, most of my users are using either IE6 or 7. Otherwise, great answer.
T Pops
Then you'll be happy to know that IE supports inline-block if you add hasLayout to an element, and then set it to display: inline after. Here's the hack: http://foohack.com/2007/11/cross-browser-support-for-inline-block-styling/
Magnar
T Pops
A: 

You can probably adopt my previous answer. Inline-block for IE, display:table for modern browsers. Might need to explicitly specify a list-style-type.

Edit: Since this is a list, may be able to get away with inline-block on the ul and not display:table. You need to declare in a separate rule, display:inline; after inline-block for IE.

meder
A: 

Try this. It involves in incompatibilities, but I don't really know how older browsers handle margins and padding and all that anymore since I only work with new ones. It only involves some minor changes to your CSS.

/* I didn't style the other parts, so I'm taking them out to save space. */

#content {
    margin: 0 auto;
}

#content ul {
    border-top: solid 1px #666666;
    border-bottom: solid 1px #666666;
    text-align: left;
    font-size: 150%;
    list-style-type: none;
    margin: 20px auto;
    padding: 0px;
    width: 202px;
}

#content li {
    border-left: solid 1px #666666;
    border-right: solid 1px #666666;
    margin: 0 auto;
    padding: 0;
    width: 200px;
}

Edit: I want to clarify what I changed. I changed how content is aligned, but honestly, you can change that back, I don't think it has an effect.

What I originally did was set a fixed width and centered your li element, which you had no styling for. That just centered the content. The border you placed was on the ul so it was very wide, but if we placed it in the li then we would have many boxes. So, I split the border style. The reason why #content ul has a 202px width is because borders count on the outside of the width.

I hope the explanation made it clear to why it worked. Good luck! I tested this out in Google Chrome.

Tarik
I read your question wrong, I am working on a new solution.
Tarik