My flex container oddly (in my opinion) ignores max-width
and wrap
.
The intention is to have a (consistently) large item to the left and zero, one, two or three smaller ones besides or below that one. If there's only one, it should display to the right, if there are two or three, they ought all to go to the next line (equally spaced).
0) -------------------------------
| large item |
-------------------------------
1) -------------------------------
| large item | small |
-------------------------------
2) -------------------------------
| large item |
-------------------------------
| small | small |
-------------------------------
3) -------------------------------
| large item |
-------------------------------
| small | small | small |
-------------------------------
I thought flexbox would "just have to do" exactly that. But now it won't wrap as expected. The items to the right (which should be on the second row) just overflow the flexbox.
Can anyone see what I do wrong? Thank you very much!
An Example is here: Codepen
.container {
display: flex;
flex-wrap: wrap; /* ? */
background-color: #ccc;
padding: 10px 0;
max-width: 1024px;
margin: 0 auto;
}
.item_large {
flex: 1 0 450px;
background-color: green;
height: 40px;
border-radius: 2px;
}
.container_2 {
display: flex;
flex-wrap: nowrap;
flex: 1 0 190px;
margin: 0 -12px;
}
.item_small {
flex: 1 0 190px;
background-color: blue;
height: 40px;
border-radius: 2px;
margin: 0 12px;
}
@media screen and (max-width: 719px) {
.container_2 {
flex-wrap: wrap;
}
.item_small {
flex: 1 0 320px;
}
}
@media screen and (min-width: 720px) {
.item_small:first-child:nth-last-child(1) {
margin-left: 36px;
}
}
<div class="container">
<div class="item_large"></div>
</div>
</div><br /><br />
<div class="container">
<div class="item_large"></div>
<div class="container_2">
<div class="item_small"></div>
</div>
</div><br /><br />
<div class="container">
<div class="item_large"></div>
<div class="container_2">
<div class="item_small"></div><div class="item_small"></div>
</div>
</div><br /><br />
<div class="container">
<div class="item_large"></div>
<div class="container_2">
<div class="item_small"></div><div class="item_small"></div><div class="item_small"></div>
</div>
</div>