tags:

views:

88

answers:

3

I tried:

margin-left: auto;
margin-right: auto;

But it doesn't center the elements in my table cells. I have a combination of text and <span> elements in the <td>s.

Once I set 'align="center"' in any of the <td> elements, it does center.

How do I achieve this in the stylesheet?

Btw, when I do text-align: center that works for text. But not for other elements like <span>s.

Edit:

The span has the following class, if that affects the alignment issue:

.dot {
    display: block;
    width: 10px;
    height: 10px;
    background: #333;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    -khtml-border-radius: 5px;
}
A: 

I did a quick search on google and found this:

span is an inline element and therefore can't be aligned. Make it a p or a div instead of a span, or explicitly make the span block-level by using display:block.

Found at: Velocity Reviews Forumn Link

PatrickV
Further down in that post there is more info, specifically: text-align centers the text within the span. As the span is only as wide as the text it contains, centering that text has no effect. To center the span you need to apply text-align: center; to whatever block level element is the span's parent.
PatrickV
+5  A: 

margin:0 auto; will work on

  • block level non-floated, static/relative positioned elements with an explicit width set
  • intrinsic width elements like images/objects/tables

text-align:center will work on

  • inline/inline-blocks

For your situation you can probably do..

#container { text-align:center; }
#container span.block-level-spans { margin: 0 auto; }

or make the spans inside inline-block instead of block.

EDIT:

Inline-block: This value causes an element to generate a block box, which itself is flowed as a single inline box, similar to a replaced element. The inside of an inline-block is formatted as a block box, and the element itself is formatted as an inline replaced element.

meder
Wonderful....making the span 'inline-block' and adding that span-block-level-spans selector seemed to have done the trick. Amazing! Can you provide a link as to what 'block-level-spans' does and why it works? Thanks.
marcamillion
It's worth noting that `margin: 0 auto` doesn't work on IE unless you force it into (so-called) standards compliant mode by using a `DOCTYPE`, which is something you should be doing anyway.
cletus
An inline-block is basically an inline element allowed to receive a width/height. It's much like a float except you don't have to worry about it clearing. It can be given a vertical align. Sometimes you have to specify `top` if it doesn't align properly to the top.
meder
The selector I typed out was just some random selector. You probably don't need it.
meder
A: 

Could you show when it fails? (may be with different display property, not sure). It would be interesting for me.

Could you show when it fails? (may be with different display property, not sure). It would be interesting for me.
The selected answer works. I am not sure why it fails, but I know that solution works. If you can figure out why the previous version failed, do let me know :)
marcamillion