I have a layout similar to:
<div>
<table>
</table>
</div>
I would like for the div
to only expand to as wide as my table
becomes.
I have a layout similar to:
<div>
<table>
</table>
</div>
I would like for the div
to only expand to as wide as my table
becomes.
The solution is to set your
div
todisplay: inline-block
.You can use
inline-block
as @user473598, but beware of older browsers..Mozilla doesn’t support inline-block at all, but they have
-moz-inline-stack
which is about the sameSome cross-browser around
inline-block
display attribute: https://css-tricks.com/snippets/css/cross-browser-inline-block/You can see some tests with this attribute in: https://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/
I would just set
padding: -whateverYouWantpx;
works fine for me :)
I have solved a similar problem (where I didn't want to use
display: inline-block
because the item was centered) by adding aspan
tag inside thediv
tag, and moving the CSS formatting from the outerdiv
tag to the new innerspan
tag. Just throwing this out there as another alternative idea ifdisplay: inline block
isn't a suitable answer for you.You want a block element that has what CSS calls shrink-to-fit width and the spec does not provide a blessed way to get such a thing. In CSS2, shrink-to-fit is not a goal, but means to deal with a situation where browser "has to" get a width out of thin air. Those situations are:
when there are no width specified. I heard they think of adding what you want in CSS3. For now, make do with one of the above.
The decision not to expose the feature directly may seem strange, but there is a good reason. It is expensive. Shrink-to-fit means formatting at least twice: you cannot start formatting an element until you know its width, and you cannot calculate the width w/o going through entire content. Plus, one does not need shrink-to-fit element as often as one may think. Why do you need extra div around your table? Maybe table caption is all you need.