If you use a @media
query for the breakpoint, you could reorder the the flex children and force linebreaks by using flex-wrap: wrap
, percentage widths and margin-right
for child2
and child4
to have them in the same width as child1
body {
background: lightblue;
}
.flex {
display: flex;
flex-direction: column;
padding: 1em;
border:1px solid #000;
}
.flex .child {
font-size: 3em;
padding: 0.1em;
border: 1px solid #000;
margin-bottom: .5em;
box-sizing: border-box;
}
@media (min-width: 600px) {
.flex {
flex-direction: row;
flex-wrap: wrap;
}
.flex .child {
order: 3;
width: 33.33%;
}
.flex .child:nth-of-type(1) {
order: 1;
width: 33.33%;
}
.flex .child:nth-of-type(3) {
order: 2;
width: 66.66%;
}
.flex .child:nth-of-type(2), .flex .child:nth-of-type(4) {
margin-right: 66.66%;
}
}
<div class="flex">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
</div>
jsFiddle: https://jsfiddle.net/azizn/3snvjyp5/
Edit: dynamic heights / faux-masonry layout
If you want child3
to have a tall height without creating whitespace, you could apply position: absolute
so that it doesn't affect the flow while all other children are under each other with margin-right
equal to child3
's width:
body {
background: lightblue;
}
.flex {
display: flex;
flex-direction: column;
padding: 1em;
border:1px solid #000;
position: relative;
}
.flex .child {
font-size: 3em;
padding: 0.1em;
border: 1px solid #000;
margin-bottom: .5em;
box-sizing: border-box;
}
@media (min-width: 600px) {
.flex {
flex-direction: row;
flex-wrap: wrap;
}
.flex .child {
width: 33.33%;
margin-right: 66.66%;
}
.flex .child:nth-of-type(3) {
width: 62.66%;
position: absolute;
top: 2%; right: 2%;
margin-right: 0;
}
}
<div class="flex">
<div class="child" style="height: 100px;">1</div>
<div class="child" style="height: 200px;">2</div>
<div class="child" style="height: 600px;">3</div>
<div class="child" style="height: 400px;">4</div>
</div>
This code is more simplified since we do not need to reorder the children.
jsFiddle: https://jsfiddle.net/azizn/d0pk8p89/