If I have something like the following:
<div id="flex">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
Assuming flex-direction: row
and that three <div></div>
elements fit on one line, is it possible for the browser to display 2 on each line, for two lines, instead of displaying 3 on one line, and then 1 on the next?
You can set the col-bordered elements display to flex as well and use flex-flow of column nowrap . Setting flex to 1 on the child elements of col-bordered will force them to have equal height. I have also set the flex property of col-bordered to 0 1 50% to give them an equal width. Here is the example.
For 3 items per row, add on the flex items: flex-basis: 33.333333% You can also use the flex 's shorthand like the following: flex: 0 0 33.333333% => which also means flex-basis: 33.333333% .
Justify Content Flex 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.
Assuming
flex-direction: row
and that three<div></div>
elements fit on one line, is it possible for the browser to display 2 on each line, for two lines, instead of displaying 3 on one line, and then 1 on the next?
Implicit in that statement is that each of the divs is equal to or less than 1/3 width of a complete row.
Thus normally your result would be say....
#flex {
display: flex;
flex-wrap: wrap;
background: orange;
}
#flex div {
border: 1px solid white;
height: 100px;
width: 33%;
background: rebeccapurple;
}
<div id="flex">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
However, from the request you seem to wish to break the line/row after the second element.
As explained by @Oriol here this can only be done by either changing the structure or using clever methods to insert either invisible actual or pseudo-elements to force a linebreak.
For example:
#flex {
display: flex;
flex-wrap: wrap;
background: orange;
justify-content: space-around;
}
#flex div {
border: 1px solid white;
height: 100px;
width: 33%;
background: rebeccapurple;
}
#flex::before {
content: "";
width: 100%;
order: 1;
}
div:nth-child(n + 3) {
order: 2;
}
<div id="flex">
<div></div>
<div></div>
<div></div>
<div></div>
</div
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With