Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break line in a flex layout?

Tags:

html

css

flexbox

I have my first, second, and third items and then I want the forth item to go to the next line no matter how wide is the space.

.box {    display: flex;    flex-flow: row wrap;    justify-content: space-around;    align-items: flex-start;  }  .it {    max-width: 420px;  }
<div class="box">    <div class="it">1</div>    <div class="it">2</div>    <div class="it">3</div>    <div class="it">4</div>  </div>
like image 383
kptlronyttcna Avatar asked Feb 27 '15 00:02

kptlronyttcna


People also ask

How do you break a line in Flex?

Using 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. This produces a layout with two vertically stacked full-width items (I've added a border to the .

How do you have content break to next line with flex when content reaches edge?

To have content break to next line with flex when content reaches edge with Flex React Native, we can set flexWrap to 'wrap' . to set flexDirection to 'row' to set the flexbox direction to horizontal. Then we set flexWrap to 'wrap' to make the child items wrap when it reaches the right edge of the screen.

How do you push the flex item to the next row?

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.


2 Answers

You can insert a wide pseudo-element at the right position:

.flex-container {    display: flex;    flex-wrap: wrap;  }  .flex-container::after {    content: '';    width: 100%;  }  .flex-item:last-child { /* or `:nth-child(n + 4)` */    order: 1;  }
<div class="flex-container">    <div class="flex-item">1</div>    <div class="flex-item">2</div>    <div class="flex-item">3</div>    <div class="flex-item">4</div>  </div>
like image 147
Oriol Avatar answered Sep 19 '22 23:09

Oriol


I have solved this by:

.flex-container{     display:flex;     flex-direction:column;     /* how you want your lines */     text-align:center; }  <div class="flex-container">     <p>Line1</p>     <p>Line2</p> </div> 
like image 33
sunil Prajapat Avatar answered Sep 21 '22 23:09

sunil Prajapat