Isn't there a way to change the order of the children of a flex box with just CSS?
<div> flex 2 </div> <div>
flex 3
</div>
<div style="flex-order:-1"> flex 1 </div>
Isn't there a way to change the order of the children of a flex box with just CSS?
<div> flex 2 </div> <div>
flex 3
</div>
<div style="flex-order:-1"> flex 1 </div>
you can use the property order
.div1 { order: 0; }
But to make it work you should set order to all childrens
Also you should now: 0
for first position, and 1
, 2
and so on for other positions
You can use order
attribute example:
order: 1;
order: 2;
order: 4;
order: 3;
You can find more here in MDN documentation
Having an HTML code like this:
<div class="wrapper">
<div class="item-2">Flex 2</div>
<div class="item-3">Flex 3</div>
<div class="item-1">Flex 1</div>
</div>
If you want to change the order of a specific item, you can use the order attribute like this:
.wrapper {
display: flex;
}
.item-1 {
order: 1;
}
.item-2 {
order: 2;
}
.item-3 {
order: 3;
}
This code will reorder elements: Flex 1 - Flex 2 - Flex 3.
But, if you want to change the order of all items at same time, simply use flex-direction on the wrapper:
.wrapper {
display: flex;
flex-direction: row-reverse;
}
This code will reorder elements: Flex 1 - Flex 3 - Flex 2.