Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox item wrap to a new line [duplicate]

Tags:

There is a flexbox grid.

.flex {    display: flex;    flex-wrap: wrap;    border: 2px solid red;  }    .item {    width: 50px;    height: 50px;    margin: 5px;    border: 2px solid blue;  }
<div class="flex">    <div class="item"></div>    <div class="item"></div>    <div class="item new-string"></div>  </div>

How to transfer .new-string to a new line, along with the elements that follow it?

like image 384
Yuri Avatar asked Jul 13 '17 16:07

Yuri


People also ask

How do you break a new line in flexbox?

Inserting a line-breaking flex itemUsing an element to break to a new flex row comes with an interesting effect: we can skip specifying the width of any item in our flex layout and rely completely on the line breaks to define the flow of our grid.

How do you wrap a flex item?

Making things wrap If you want to cause them to wrap once they become too wide you must add the flex-wrap property with a value of wrap , or use the shorthand flex-flow with values of row wrap or column wrap . Items will then wrap in the container.

What does flex-wrap Nowrap do?

nowrap. The flex items are laid out in a single line which may cause the flex container to overflow. The cross-start is either equivalent to start or before depending on the flex-direction value. This is the default value.

How do you wrap text in a Flex container?

As you only want the text itself to wrap you need to use flex-wrap: nowrap; to keep . right on the same line. The text will automatically wrap when there is not enough space.


1 Answers

If you look at this great answer you'll notice that the only cross-browser way (without 2 line break limit) is inserting 100%-width empty blocks ("line-breaks"). So for similar markup this will look like

.flex {    display: flex;    flex-wrap: wrap;    border: 2px solid red;  }    .item {    width: 50px;    height: 50px;    margin: 5px;    border: 2px solid blue;  }    .line-break {    width: 100%;  }
<div class="flex">    <div class="item"></div>    <div class="item"></div>    <div class="item"></div>    <div class="line-break"></div>    <div class="item"></div>    <div class="item"></div>    <div class="line-break"></div>    <div class="item"></div>    <div class="line-break"></div>    <div class="item"></div>    <div class="item"></div>    <div class="item"></div>  </div>

If you want to preserve your markup style, you'll have to insert this line-break blocks via JavaScript:

var items = document.querySelectorAll(".flex > .item.new-string");    for (var i = 0; i < items.length; i++) {    var lineBreak = document.createElement('div');    lineBreak.className = "line-break";      items[i].parentNode.insertBefore(lineBreak, items[i]);  }
.flex {    display: flex;    flex-wrap: wrap;    border: 2px solid red;  }    .item {    width: 50px;    height: 50px;    margin: 5px;    border: 2px solid blue;  }    .line-break {    width: 100%;  }
<div class="flex">    <div class="item"></div>    <div class="item"></div>    <div class="item"></div>    <div class="item new-string"></div>    <div class="item"></div>    <div class="item new-string"></div>    <div class="item new-string"></div>    <div class="item"></div>    <div class="item"></div>  </div>
like image 172
Vadim Ovchinnikov Avatar answered Sep 21 '22 09:09

Vadim Ovchinnikov