-1

I have a container where

#test {
  display:flex;
  flex-direction:row;
}

p { 

 margin-left:3px;
}
<div id = "test">

<p> 1 </p>
<p> 1 </p>
<p> 1 </p>

</div>

How can I send the last element to the start of the next line?

vasilis 123
  • 585
  • 1
  • 12
  • 26

4 Answers4

5

you need to set wrap on the parent div and make last element's width 100%

 #test {
      display:flex;
      flex-direction:row;
      flex-wrap: wrap
    }
    
    p { 
     margin-left:3px;
    }
    p:last-child{
      width: 100%;
    }
1

Use last-child and order

#test {
  display:flex;
  flex-direction:row;
}

p { 

 margin-left:3px;
}

p:last-child{
   order: -1;
}
Kichirou
  • 11
  • 4
1

What you're probably looking for is the use of wrapping flexboxes.

Have a look at this answer which describes a similar problem, where you can say how much an item in a row "contributes" to a row using the flex css attribute.

#test {
  display:flex;
  flex-direction:row;
  flex-wrap: wrap;
  width: fit-content;
}

p {
  margin: 0 0 0 3px;
  padding: 0px;
  flex: 0 0 calc(50% - 3px); # here the - 3px come from the margin-left being 3px
}
<div id = "test">

<p> 1 </p>
<p> 1 </p>
<p> 1 </p>

</div>
dcronqvist
  • 81
  • 3
0

You can use grid instead of flexbox. Set every line with two columns and you can solve that issue.

#test {
  display: grid;
  grid-template-columns: repeat(2, 10px);
}
<div id="test">
  <p> 1 </p>
  <p> 1 </p>
  <p> 1 </p>
</div>
olep
  • 1
  • 1