OK so what would happen if I have 2 divs (one containing text, the other an image). The image always has a static width but the text varies. hence making its containing div variable.
I can make it work for all other browsers (except IE6 and IE7) by using CSS display:table. IE6 and 7 don’t have that so I can’t find a workable solution to center them all.
… so you know what I’m talking about…
.container{text-align:center; width:100%}
.container .centered{display:table; margin:0 auto}
<div class="container">
<div class="centered">
<div id="text">varying length text</div>
<div id="image">IMAGE</div>
</div>
</div>
Quite apart from the lack of IE support, setting
display: tableas you have without its children usingdisplay: table-row/table-cellresults in undefined behaviour. It doesn’t make sense to put block elements directly inside a table element and the browser might do anything at all.What you are trying to do is get shrink-to-fit width behaviour without using
float, which is a normal way of getting shrink-width but requires that the block in question goes to the left or right not centre. Probably a better way of saying that would be to use an inline-block:(You have to use a naturally-
inlineelement likespanto make it work under IE<8;divwould fail. There is also-moz-inline-boxif you need to target Firefox 2.)