
Signature
Using Web Standards in your Web Pages (Updated Apr. 2007)
http://developer.mozilla.org/en/docs/Using_Web_Standards_in_your_Web_Pages
> biff wrote :
>>> I am trying to center a DIV horizonally, but I want the DIV fluid so
[quoted text clipped - 28 lines]
> div {display: inline-block; display: -moz-inline-block; margin-left:
> auto; margin-right: auto;}
Inline-block is the best way to do this, or would be if it were
supported by more browsers. But the way to centre it is not with auto
margins, but with text-align: center on the container.
See CSS 2.1 10.3.9-- auto left and right margins on inline blocks are
treated as zero. You only get that centering behaviour with block boxes.
Inline blocks are like inlines from the point of view of their
container, but like blocks from the point of view of their descendents.
So you center them like inlines, with text-align: center on their
container, rather than like blocks, which you centre with auto margins.
e.g.
body { text-align: center; }
div
{
display: inline-block;
display: -moz-inline-box; /* seems to work here */
border: 2px solid green;
}
<body>
<div>hello</div>
</body>
works in Opera.
The problem with inline-block is that IE (I heard) doesn't do the one
feature you need here, which is shrink-to-fit width. I don't know about
-moz-inline-box (it's -moz-inline-box, not -moz-inline-block) but it
seems to work OK in this example.
Gérard Talbot - 24 May 2007 15:36 GMT
Ben C wrote :
>> div {display: inline-block; display: -moz-inline-block; margin-left:
>> auto; margin-right: auto;}
>
> Inline-block is the best way to do this, or would be if it were
> supported by more browsers. But the way to centre it is not with auto
> margins, but with text-align: center on the container.
You're right.
> See CSS 2.1 10.3.9-- auto left and right margins on inline blocks are
> treated as zero. You only get that centering behaviour with block boxes.
>
> Inline blocks are like inlines from the point of view of their
> container, but like blocks from the point of view of their descendents.
Correct!
> So you center them like inlines, with text-align: center on their
> container, rather than like blocks, which you centre with auto margins.
[quoted text clipped - 18 lines]
> feature you need here, which is shrink-to-fit width. I don't know about
> -moz-inline-box (it's -moz-inline-box, not -moz-inline-block)
Yes again! Great post from you, Ben :)
> but it
> seems to work OK in this example.
Gérard

Signature
Using Web Standards in your Web Pages (Updated Apr. 2007)
http://developer.mozilla.org/en/docs/Using_Web_Standards_in_your_Web_Pages