Reputation:
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.
Upvotes: 2529
Views: 1925726
Reputation: 1535
The answer for your question lays in the future my friend ...
namely "intrinsic" is coming with the latest CSS3 update
width: intrinsic;
unfortunately IE is behind with it so it doesn't support it yet
More about it: CSS Intrinsic & Extrinsic Sizing Module Level 3 and Can I Use?: Intrinsic & Extrinsic Sizing.
For now you have to be satisfied with <span>
or <div>
set to
display: inline-block;
Upvotes: 46
Reputation: 91605
A CSS2 compatible solution is to use:
.my-div
{
min-width: 100px;
}
You can also float your div which will force it as small as possible, but you'll need to use a clearfix if anything inside your div is floating:
.my-div
{
float: left;
}
Upvotes: 41
Reputation: 3286
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:
div {
display: inline-flex;
}
This is also widely supported across browsers.
Upvotes: 36
Reputation: 790
Seems it's a 13 years old question.
.div{
display:inline-block;
}
or
.div{
display:inline-flex;
}
would work now a days without any compatibility issues.
Upvotes: -3
Reputation: 1795
You can try this code. Follow the code in the CSS section.
div {
display: inline-block;
padding: 2vw;
background-color: green;
}
table {
width: 70vw;
background-color: white;
}
<div>
<table border="colapsed">
<tr>
<td>Apple</td>
<td>Banana</td>
<td>Strawberry</td>
</tr>
<tr>
<td>Apple</td>
<td>Banana</td>
<td>Strawberry</td>
</tr>
<tr>
<td>Apple</td>
<td>Banana</td>
<td>Strawberry</td>
</tr>
</table>
</div>
Upvotes: 27
Reputation: 576
just set the width and height to fit-content. it is very simple.
div {
width: fit-content;
height: fit-content;
padding: 10px;
}
I am adding padding: 10px;. if it is left out, the div element will completely stick with the table and it will look a bit clumsy. Padding will create the given space between the border of the element and it's contents. But it is your wish not compulsory.
Upvotes: 31
Reputation: 260
.outer{
width:fit-content;
display: flex;
align-items: center;
}
.outer .content{
width: 100%;
}
<div class=outer>
<div class=content>
Add your content here
</div>
</div>
Upvotes: 10
Reputation: 15649
Try to use width: max-content
property to adjust the width of the div by it's content size.
Try this example,
<!DOCTYPE html>
<html>
<head>
<style>
div.ex1 {
width:500px;
margin: auto;
border: 3px solid #73AD21;
}
div.ex2 {
width: max-content;
margin: auto;
border: 3px solid #73AD21;
}
</style>
</head>
<body>
<div class="ex1">This div element has width 500px;</div>
<br>
<div class="ex2">Width by content size</div>
</body>
</html>
Upvotes: 23
Reputation: 1
You can use height: 100%
and width for your choice. This makes the div not larger than its content.
Upvotes: -5
Reputation: 615
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<div id="content_lalala">
this content inside the div being inside a table, needs no inline properties and the table is the one expanding to the content of this div =)
</div>
</td>
</tr>
</table>
I know people don't like tables sometimes, but I gotta tell you, I tried the css inline hacks, and they kinda worked in some divs but in others didn't, so, it was really just easier to enclose the expanding div in a table...and...it can have or not the inline property and still the table is the one that's gonna hold the total width of the content. =)
Upvotes: 20
Reputation: 137
I've a span
inside a div
and just setting margin: auto
to the container div
worked for me.
Upvotes: 3
Reputation: 2125
There are two better solutions
display: inline-block;
OR
display: table;
Out of these two display:table;
is better, because display: inline-block;
adds an extra margin.
For display:inline-block;
you can use the negative margin method to fix the extra space
Upvotes: 108
Reputation: 534
What if we define a global variable and use that for both.
:root {
--table-width: 400px;
}
.container{
width:var(--table-width);
border: 1px solid black; // easy visualization
}
.inner-table {
width:var(--table-width);
border: 1px solid red; // easy visualization
}
<div class="container">
<table class="inner-table">
<tr>
<td>abc</td>
</tr>
</table>
</div>
Upvotes: 1
Reputation: 24762
display: inline-block
adds an extra margin to your element.
I would recommend this:
#element {
display: table; /* IE8+ and all other modern browsers */
}
Bonus: You can also now easily center that fancy new #element
just by adding margin: 0 auto
.
Upvotes: 254
Reputation: 104870
OK, in many cases you even don't need to do anything as by default div has height
and width
as auto, but if it's not your case, applying inline-block
display gonna work for you... look at the code I create for you and it's do what you looking for:
div {
display: inline-block;
}
<div>
<table>
<tr>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ultrices feugiat massa sed laoreet. Maecenas et magna egestas, facilisis purus quis, vestibulum nibh.</td>
<td>Nunc auctor aliquam est ac viverra. Sed enim nisi, feugiat sed accumsan eu, convallis eget felis. Pellentesque consequat eu leo nec pharetra. Aenean interdum enim dapibus diam.</td>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ultrices feugiat massa sed laoreet. Maecenas et magna egestas, facilisis purus quis, vestibulum nibh.</td>
</tr>
</table>
</div>
Upvotes: 30
Reputation: 403
You could use display: flex
for parent element
#parentElement {
display: flex;
flex-direction: column;
align-items: flex-start;
}
Upvotes: 4
Reputation: 3345
You can use inline-block
as @user473598, but beware of older browsers..
/* Your're working with */
display: inline-block;
/* For IE 7 */
zoom: 1;
*display: inline;
/* For Mozilla Firefox < 3.0 */
display:-moz-inline-stack;
Mozilla doesn’t support inline-block at all, but they have -moz-inline-stack
which is about the same
Some 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/
Upvotes: 20
Reputation: 335
Try display: inline-block;
. For it to be cross browser compatible please use the below css code.
div {
display: inline-block;
display:-moz-inline-stack;
zoom:1;
*display:inline;
border-style: solid;
border-color: #0000ff;
}
<div>
<table>
<tr>
<td>Column1</td>
<td>Column2</td>
<td>Column3</td>
</tr>
</table>
</div>
Upvotes: 11
Reputation: 1222
<div class="parentDiv" style="display:inline-block">
// HTML elements
</div>
This will make parent div width same as the largest element width.
Upvotes: 11
Reputation:
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.
body {
font-family: sans-serif;
}
.top {
display: -webkit-inline-flex;
display: inline-flex;
}
.top, .bottom {
background-color: #3F3;
border: 2px solid #FA6;
}
/* bottomwrapper will take the rest of the width */
.bottomwrapper {
display: -webkit-flex;
display: flex;
}
table {
border-collapse: collapse;
}
table, th, td {
width: 280px;
border: 1px solid #666;
}
th {
background-color: #282;
color: #FFF;
}
td {
color: #444;
}
th, td {
padding: 0 4px 0 4px;
}
Is this
<div class="top">
<table>
<tr>
<th>OS</th>
<th>Version</th>
</tr>
<tr>
<td>OpenBSD</td>
<td>5.7</td>
</tr>
<tr>
<td>Windows</td>
<td>Please upgrade to 10!</td>
</tr>
</table>
</div>
what you are looking for?
<br>
Or may be...
<div class="bottomwrapper">
<div class="bottom">
<table>
<tr>
<th>OS</th>
<th>Version</th>
</tr>
<tr>
<td>OpenBSD</td>
<td>5.7</td>
</tr>
<tr>
<td>Windows</td>
<td>Please upgrade to 10!</td>
</tr>
</table>
</div>
</div>
this is what you are looking for.
Upvotes: 11
Reputation: 9067
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:
width: -moz-fit-content;
Although it only works on Firefox, I couldn't find any equivalent for other browsers such as Chrome.
Upvotes: 11
Reputation: 1578
Revised (works if you have multiple children): You can use jQuery (Look at the JSFiddle link)
var d= $('div');
var w;
d.children().each(function(){
w = w + $(this).outerWidth();
d.css('width', w + 'px')
});
Do not forget to include the jQuery...
Upvotes: 5
Reputation: 558
We can use any of the two ways on the div
element:
display: table;
or,
display: inline-block;
I prefer to use display: table;
, because it handles, all extra spaces on its own. While display: inline-block
needs some extra space fixing.
Upvotes: 7