I have this html:
<div class="object-box">
<img ... />
<span class="caption">This is the caption</span>
</div>
Which is accompanied with this CSS:
.object-box .caption, .object-box img {
display: block;
}
.object-box {
border: 1px solid;
}
I would like the surrounding div to shrink-wrap to its contents. I can achieve this by using float: ... or display: inline-block. However, I’d also like it to be centered on the page, using margin: auto. The two approaches don’t seem to be compatible.
Is it possible to create such a shrink-wrapped, centered container, without adding a wrapper element?
EDIT:
jsFiddle here
Simple explanation is..
display:table;causes it to shrinkwrap in modern browsers, thats the only way to center widthless block level in modern browsers with margin:0 auto;.In IE it’s a matter of using parent element to set text-align:center on your shrinkwrapped
display:inlineblock level element.For floats its just 50% math using containers for IE.