How to force a flexbox item to a new row
At some point you might end up with a challenge like this. You've got several items (we'll take an example of 3) laid out with flexbox.
```html
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
```
The items have fixed width and they all fit on one row, but you want the third to jump on the second row.
Like this:
```
====item====item====
========item========
```
So how do you make them stack 2 on the first row, and the third on the second row, centered to the middle?
If you try flex-wrap: wrap, depending on the device resolution, it might do the trick or not. If all 3 fit on the same row, they won't wrap.
The solution is to force them by adding a collapsed row (height 0) which takes up the entire width of the container, making it occupy an entire row, thus pushing the 3rd item on the next row. Think of it like a ```<br>``` tag.
```html
<div class="container">
<div class="item"></div>
<div class="item"></div>
<div class="break"></div>
<div class="item"></div>
</div>
```
```css
.break {
flex-basis: 100%;
height: 0;
}
```
Neat trick, right?
And it can be adapted to other situations as well, not necessarily flexbox.
Zeno Popovici
09 Jul 2021
« Back to post