I have been experimenting with creating a container div that adjusts its width based on the size of its contents. To achieve this, I have successfully used float: left
on the container. Here's what my current setup looks like:
HTML
<div class='container'>
<div class='floater' style='background-color: #880000'>1</div>
<div class='floater' style='background-color: #008800'>2</div>
<div class='floater' style='background-color: #000088'>3</div>
</div>
CSS
.container {
background-color: #123456;
float: left;
}
.floater {
width: 300px;
height: 100px;
float: left;
}
The issue arises when the floater divs wrap inside the container. While I want them to wrap, I also want the container div to resize accordingly. For example, if the browser window is 1000px wide, everything works as expected and the container is 900px. However, when the browser width is reduced to 750px, the third div wraps to the next line but the container remains at 750px instead of the desired 600px.
Is it possible to achieve the behavior I am looking for? If so, how can it be done?
I have created a Fiddle showcasing the problem here.