Changing stack order between different rows flexbox

↘锁芯ラ 提交于 2021-01-27 12:39:12

问题


Goodmorning. I am trying to change the stacking order in a flexbox situation, where there are 2 columns, but the first column contains a spot where the second column needs to be put in between. So when on mobile I need them to be ordered different than source order.

This is on large

col 1     col 2
----------==============
[A]         [C]
[B]

Where A and B are in one column, and C is in the other

But on small breakpoint, it needs to be

[A]
[C]
[B]

Is this possible using just Flexbox?

So to clarify. The HTML structure is as such:

row
  column
    divA
    divB

  column
    divC

Codepen example

.a { background-color: green; }
.b { background-color: red; }
.c { background-color: blue; }

.row {
  display: flex;
  flex-direction: column;
}
.column {
  flex: 1;
}

@media (min-width: 768px) {
  .row {
     flex-direction: row;
  }
}
<div class="row">
  
  <div class="column">
      <div class="a">A</div>
      <div class="b">B</div>
  </div>
  <div class="column c">C</div>
</div>

回答1:


You can achieve what you want with media queries and order:

.a {
  background-color: green;
  order: 1;
}

.b {
  background-color: red;
  order: 3;
}

.c {
  background-color: blue;
  order: 2;
}

.row {
  width: 100%;
  display: flex;
  flex-direction: row; 
  flex-wrap: wrap;     /* add this so you don't need the extra wrapper div */
}

.row>div {
  width: 50%; /* start off width children being 50% width */
}

@media (max-width: 768px) {
  .row>div {
     /* for small screens */
    width: 100%;
  }
}
<div class="row">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
</div>


来源:https://stackoverflow.com/questions/43911787/changing-stack-order-between-different-rows-flexbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!