Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make flex item wrap to the next row with following items continuing the flow

Tags:

html

css

flexbox

I want the red box to be on the 2nd row, but then i want the divs that are defined after it, to continue on the first line, so that all the black boxes essentially wrap around the black box. Can this be achieved with flexbox?

It should look like this:

+---+---+---+---+---+
| 1 | 2 | 3 | 5 | 6 |
+---+---+---+---+---+
|         4         |
+---+---+---+---+---+
| 7 | 8 | 9 |10 |11 |
+---+---+---+---+---+
|12 |13 |
+---+---+

* {
  box-sizing: border-box;
}

.container {
  display: flex;
  flex-wrap: wrap;
}

.a {
  border: 1px solid black;
  height: 50px;
  width: 20%;
}

.b {
  border: 1px solid red;
  height: 50px;
  width: 100%;
}
<div class="container">
  <div class='a'>1</div>
  <div class='a'>2</div>
  <div class='a'>3</div>
  <div class='b'>4</div>
  <div class='a'>5</div>
  <div class='a'>6</div>
  <div class='a'>7</div>
  <div class='a'>8</div>
  <div class='a'>9</div>
  <div class='a'>10</div>
  <div class='a'>11</div>
  <div class='a'>12</div>
  <div class='a'>13</div>
</div>

See https://codepen.io/anon/pen/OqqeJN

like image 325
Bart van den Burg Avatar asked Mar 25 '19 19:03

Bart van den Burg


2 Answers

You will have better luck using CSS grid for this task:

* {
  box-sizing: border-box;
}

.container {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-auto-flow: dense; /* this will make the elements to flow around*/
}

.a {
  border: 1px solid black;
  height: 50px;
}

.b {
  border: 1px solid red;
  height: 50px;
  /*full width row*/
  grid-column: 1/-1;
}
<div class="container">
  <div class='a'>1</div>
  <div class='a'>2</div>
  <div class='a'>3</div>
  <div class='b'>4</div>
  <div class='a'>5</div>
  <div class='a'>6</div>
  <div class='a'>7</div>
  <div class='a'>8</div>
  <div class='a'>9</div>
  <div class='a'>10</div>
  <div class='a'>11</div>
  <div class='a'>12</div>
  <div class='a'>13</div>
</div>

dense

If specified, the auto-placement algorithm uses a “dense” packing algorithm, which attempts to fill in holes earlier in the grid if smaller items come up later. This may cause items to appear out-of-order, when doing so would fill in holes left by larger items.ref

like image 155
Temani Afif Avatar answered Oct 19 '22 03:10

Temani Afif


You can use the order property along with the nth-child() pseudo-class.

.container {
  display: flex;
  flex-wrap: wrap;
}

.a {
  flex: 0 0 20%;
  border: 1px solid black;
}

.b {
  flex: 0 0 100%;
  border: 1px solid red;
}

.a:nth-child(1) { order: -5; }
.a:nth-child(2) { order: -4; }
.a:nth-child(3) { order: -3; }
.a:nth-child(5) { order: -2; }
.a:nth-child(6) { order: -1; }
/* the default value all items is 0 */

.container > div {
  height: 50px;
}

* {
  box-sizing: border-box;
}
<div class="container">
  <div class='a'>1</div>
  <div class='a'>2</div>
  <div class='a'>3</div>
  <div class='b'>4</div>
  <div class='a'>5</div>
  <div class='a'>6</div>
  <div class='a'>7</div>
  <div class='a'>8</div>
  <div class='a'>9</div>
  <div class='a'>10</div>
  <div class='a'>11</div>
  <div class='a'>12</div>
  <div class='a'>13</div>
</div>
like image 33
Michael Benjamin Avatar answered Oct 19 '22 04:10

Michael Benjamin