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.
This has been mentioned in comments and is hard to find in one of the answers so:
If you are using
display: flex
for whatever reason, you can instead use:This is also widely supported across browsers.
Tampering around with Firebug I found the property value
-moz-fit-content
which exactly does what the OP wanted and could be used as follow:Although it only works on Firefox, I couldn't find any equivalent for other browsers such as Chrome.
I think using
would work, however I'm not sure about the browser compatibility.
Another solution would be to wrap your
div
in anotherdiv
(if you want to maintain the block behavior):HTML:
CSS:
Foo Hack – Cross Browser Support for inline-block Styling (2007-11-19).
My CSS3 flexbox solution in two flavors: The one on top behaves like a span and the one at the bottom behaves like a div, taking all the width with the help of a wrapper. Their classes are "top", "bottom" and "bottomwrapper" respectively.
An working demo is here-