If I have the following HTML markup:
<div class="wrapper">
<div id="container-1">
<div class="child-1-1"></div>
<div class="child-1-2"></div>
</div>
<div id="container-2">
<div class="child-2-1"></div>
<div class="child-2-2"></div>
</div>
</div>
I can apply a display of flex to wrapper, and then make container-2 appear above container-1 using order.
I would like the final order of the elements to be:
What I basically want is for the children of the two containers to pretend as if their parents are not there, and instead take their ordering from the outer wrapper.
This could of course be achieved via either duplicating the components and hiding/showing depending on the breakpoint, or via JS - so no points for suggesting those.
The flex order
property applies only to siblings in the same container.
So using order
to re-arrange a series of flex items spread across different containers will not work.
But based on your requirements, it doesn't appear you need multiple containers. All items can exist in a single container.
.wrapper {
display: flex;
flex-wrap: wrap;
}
.child-1-1 { order: 1; }
.child-1-2 { order: 3; }
.child-2-1 { order: 2; }
.child-2-2 { order: 4; }
.wrapper > div {
flex: 1 0 40%;
height: 50px;
margin: 5px;
border: 1px solid #ccc;
background-color: lightgreen;
display: flex;
align-items: center;
justify-content: center;
}
@media ( max-width: 600px) {
.wrapper {
flex-direction: column;
}
div.child-2-1 {
order: -1;
background-color: orange;
}
}
<div class="wrapper">
<div class="child-1-1">child-1-1</div>
<div class="child-1-2">child-1-2</div>
<div class="child-2-1">child-2-1</div>
<div class="child-2-2">child-2-2</div>
</div>
Learn more about the flex order
property here:
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