1

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>

  • 3
    [CSS 'order' property MDN documentation](https://developer.mozilla.org/en-US/docs/Web/CSS/order) – DBS Nov 21 '22 at 16:25
  • [Also: Ordering Flex-Items in general MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Ordering_Flex_Items) – ColdIV Nov 21 '22 at 16:29

3 Answers3

3

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

Sito8943
  • 44
  • 5
2

You can use order attribute example:

order: 1;
order: 2;
order: 4;
order: 3;

You can find more here in MDN documentation

Moussa Bistami
  • 929
  • 5
  • 15
2

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.

o1dskoo1
  • 409
  • 2
  • 9