Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move last element to next line in a flex container

Tags:

html

css

flexbox

I have this structure in my HTML code:

<div style="display:flex">
    <div class="div_first">1</div>
    <div class="div_second">2</div>
    <div class="div_third">3</div>
    <div class="div_next_line">
      <div class="div_first">4</div>
      <div class="div_second">5</div>
      <div class="div_third">6</div>
    </div>
</div>

Is it possible to have 4 5 6 in second line with the same columns as 1 2 3, like so:

1    2    3
4    5    6

The structure of the HTML code cannot be changed.

like image 505
yAnTar Avatar asked Jun 06 '16 14:06

yAnTar


People also ask

How do you position elements in Flex?

Justify ContentFlex Start positions element at the start of the page. Flex End sets the element to the end of the page. Space Around arranges the items evenly but with spaces between them. The spaces will be equal among all the elements inside a flex-box container, but not outside them.

How do you break a flex column in CSS?

We can set break-before: column; on each 'head' element, where the column value means: Always force a column break before the generated box.


1 Answers

Apply flex-wrap to the container.

Apply flex: 1 0 33% to the first three child divs.

To the fourth div apply flex-basis: 100%; display: flex.

To the children of the fourth div apply flex: 1 0 33%.

The idea is to force the fourth item to wrap, then have its children replicate the behavior of the children in the primary container.

like image 167
Michael Benjamin Avatar answered Sep 21 '22 04:09

Michael Benjamin