Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the position of one flex child without having to manually assign the "order" property to others

Tags:

css

flexbox

I was interested swapping the order of my first flex child with the second. The problem I have is that I could potentially be dealing with a dozen or more divs. Is there a way I can assign the order of one flex child without having to assigning the order property to each one? Otherwise the first div gets pushed to the bottom.

.container {
  display: flex;
  flex-direction: column;
}

.child:first-of-type {
  order: 2;
}
<div class="container">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
  <div class="child">5</div>
  <div class="child">6</div>
  <div class="child">7</div>
  <div class="child">8</div>
</div>
like image 398
Carl Edwards Avatar asked Feb 17 '17 16:02

Carl Edwards


People also ask

What Flexbox property changes the order of the Flex items?

Use of the order property has exactly the same implications for accessibility as changing the direction with flex-direction . Using order changes the order in which items are painted, and the order in which they appear visually.

How do you position flex items in CSS?

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.

Is the FLEX item only the direct child of the Flexbox container?

Only direct children of flex containers are flex items. In your example, "childOfChildClass" would only be a flex item if "childClass" was a flex container.


2 Answers

I was interested swapping the order of my first flex child with the second.

Give the second order: -1

.container {
  display: flex;
  flex-direction: column;
}

.child:nth-child(2) {
  order: -1;
}
<div class="container">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
  <div class="child">5</div>
  <div class="child">6</div>
  <div class="child">7</div>
  <div class="child">8</div>
</div>

Is there a way I can assign the order of one flex child without having to assigning the order property to each one

No, if you want to move one between two other, you can't do that with just 1 rule, you need 2

.container {
  display: flex;
  flex-direction: column;
}

.child:nth-child(n+8) {
  order: 2;
}
.child:nth-child(5) {
  order: 1;
}
<div class="container">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
  <div class="child">5</div>
  <div class="child">6</div>
  <div class="child">7</div>
  <div class="child">8</div>
</div>
like image 195
Asons Avatar answered Oct 09 '22 01:10

Asons


Change all of them to order 2, and only the second to order 1:

.container {
  display: flex;
  flex-direction: column;
}

.child:nth-child(2) {
  order: 1;
}

.child {
  order: 2;
}
<div class="container">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
  <div class="child">4</div>
  <div class="child">5</div>
  <div class="child">6</div>
  <div class="child">7</div>
  <div class="child">8</div>
</div>
like image 31
vals Avatar answered Oct 09 '22 00:10

vals