【问题标题】:flex direction column reverse for top and bottom children顶部和底部子项的 flex 方向列反转
【发布时间】:2022-01-26 08:43:05
【问题描述】:

我尝试使用 flex-direction: column-reverse 进行此布局,其中按钮 1 和按钮 2 的顺序在不更改 html 的情况下交换:

<button>2</button>
<button>1</button>

但我不能应用 flex,因为它变成了 1 班轮。

.flex {
   display: flex;
}

button {
   width: 100%;
}

.wrap {
   width: 200px;
}
<div class="wrap">
   <div class="flex">
      <button>2</button>
      <button>1</button>
   </div>
</div>

【问题讨论】:

  • 弹性方向:列;然后 order:1 到按钮 1 ?
  • @TemaniAfif display flex make 2 row变成了1 row,这不是我想要的
  • 您的代码没有flex-direction: column-reverse

标签: html css flexbox


【解决方案1】:

您需要为 flexbox 添加flex-direction 以使其元素垂直对齐。默认情况下,它水平排列它们。使用column-reverse,元素垂直排列倒序

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

button {
   width: 100%;
}

.wrap {
   width: 200px;
}
<div class="wrap">
   <div class="flex">
      <button>2</button>
      <button>1</button>
   </div>
</div>

【讨论】:

    【解决方案2】:

    弹性容器中的默认设置是flex-direction: row,这意味着项目将排成一行。

    如果您希望项目垂直堆叠(在一列中),请将 flex-direction: columncolumn-reverse 添加到容器中。

    .flex {
      display: flex;
      flex-direction: column-reverse; /* new */
    }
    
    button {
      width: 100%;
    }
    
    .wrap {
      width: 200px;
    }
    <div class="wrap">
      <div class="flex">
        <button>2</button>
        <button>1</button>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2022-01-15
      • 2017-04-25
      • 1970-01-01
      相关资源
      最近更新 更多