I'm using flex container to ensure that elements within the container are wrapped vertically, so when the elements reach the end of the container's border they they wrap into the next column.
When I try to group several of these containers horizontally with an outer container they overlap based on the width of the individual elements rather than based on the width of the container.
I know that this can be solved by specifying the width of first container as an absolute fixed value. However I don't want to specify the width absolutely since the elements within the container can vary.
Here is a simplified code snippet of the problem:
#wrapper {
display: inline;
}
.box-grouping-wrapper {
display: inline-block;
}
.box-grouping {
height: 500px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.box {
text-align: center;
color: white;
padding: 50px;
border: 1px solid black;
width: 50px
}
.red {
background: red;
}
.green {
background: green;
}
<div id="wrapper">
<div class="box-grouping-wrapper">
<div class="box-grouping">
<span class="red box">1</span>
<span class="red box">2</span>
<span class="red box">3</span>
<span class="red box">4</span>
<span class="red box">5</span>
<span class="red box">6</span>
</div>
</div>
<div class="box-grouping-wrapper">
<div class="box-grouping">
<span class="green box">1</span>
<span class="green box">2</span>
<span class="green box">3</span>
<span class="green box">4</span>
<span class="green box">5</span>
<span class="green box">6</span>
</div>
</div>
</div>
And here is the JS fiddle for it:
https://jsfiddle.net/nf4jvz1u/1/
Here is what I would like the solution to look like (but without a fixed width):
https://jsfiddle.net/nf4jvz1u/2/
The only difference is adding width:300px;
to the .box-grouping
element.
Edit: It's important that I need to add that the box element heights are varied and unknown until runtime. So I can't just fix them into predetermined columns. So one box might be 100px high another might be 50px. They will all have the same width though.
Edit: Solution
I've found the solution from another stackoverflow question. Here is final solution to the problem: