I've got a question for which I didn't find any easy solution.
Using only HTML and CSS,
is there a nice way to fits the parent div
to "match" the total sizes of its contained elements?
Note that the box
elements all have the same fixed, mandatory sizes. But this size may vary, so I'd like to not use @media
queries.
Here is a commented snippet to illustrate what I mean:
#page{
width: 500px;
}
.container {
display: inline-block;
border: 1px solid black;
background: #ddd;
height: auto;
}
.box {
width: 200px;
height: 30px;
background: #fff;
line-height: 30px;
text-align: center;
border: 1px solid gray;
float: left;
}
#con1{
width: 404px;
}
<div id="page">
<p>How is it possible to have the container sized to fit its children?</p>
<div class="container">
<div class="box">x</div>
<div class="box">x</div>
<div class="box">x</div>
</div>
<br><br>
<p>Here is what I want, but without setting width in the CSS!</p>
<div class="container" id="con1">
<div class="box">x</div>
<div class="box">x</div>
<div class="box">x</div>
</div>
</div>
Doing this snippet, it appears to me I've got another little question…
Why is there some space between the boxes in my snippet?
Thanks in advance for any helpful answer.