I have a flex container with 2 columns.
Each column is also a flex container with a number of boxes inside.

<div class="flex-container">
  <div class="column left-column">
    <div class="box boxA">Box A</div>
    <div class="box boxB">Box B</div>
  </div>
  <div class="column right-column">
    <div class="box boxC">Box C</div>
    <div class="box boxD">Box D</div>
    <div class="box boxE">Box E</div>
  </div>
</div>

I want that, in mobile view, the 2 columns become 1.
Right now I achieve this by adding flex-direction: column to the flex-container, which makes the 2 columns be one on top of each other (vertically, not z-axis).

.flex-container {
  display: flex;
  gap: 10px;
  padding: 10px;
  max-width: 800px;
}

.column {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 10px;
}

.left-column {
  flex: 2;
}

.right-column {
  flex: 1;
}

.box {
  border: 1px solid lightgrey;
  border-radius: 8px;
  padding: 8px;
}

@media (max-width: 800px) {
.flex-container {
   flex-direction: column;
  }
}

But now I need additionally to rearrange the order of the boxes so that, in mobile view, it shows as A, C, D, E, B.

我认为这不能仅用css来实现,因为它需要"打破"弹性列.

这是一个Sandbox ,里面有我目前拥有的:https://codepen.io/marcysutton/pen/ZYqjPj

Btw, this is in a React app, so I will probably have to rearrange the boxes programmatically.
I just preferred to do that with CSS, if possible.

推荐答案

在较低的宽度处使用display: contents来"打破"包装div,然后在.boxB处使用order.

.flex-container {
  display: flex;
  gap: 10px;
  padding: 10px;
  max-width: 800px;
}

.column {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 10px;
}

.left-column {
  flex: 2;
}

.right-column {
  flex: 1;
}

.box {
  border: 1px solid lightgrey;
  border-radius: 8px;
  padding: 8px;
}

@media (max-width: 800px) {
  .flex-container {
    flex-direction: column;
  }
  .column {
    display: contents;
  }
  .boxB {
    order: 2;
  }
}
<div class="flex-container">
  <div class="column left-column">
    <div class="box boxA">Box A</div>
    <div class="box boxB">Box B</div>
  </div>
  <div class="column right-column">
    <div class="box boxC">Box C</div>
    <div class="box boxD">Box D</div>
    <div class="box boxE">Box E</div>
  </div>
</div>

Html相关问答推荐

值更新时旋转数字动画

VBA从公共Google Drive地址下载文档-.Click事件(?)

在窄屏幕上显示表格,每个单元格占一行

窗口视图之外的下拉菜单位置

如何用等宽字体固定数字在有序列表中的位置

Html视频标签:圆角像素化

如何使div填充父项的剩余高度

如何使背景图像在面包屑中相互重叠

使用单个粗体字符串向段落添加页边空白

Swift 以编程方式在 YouTube 嵌入视频中进入全屏

UL 的行为就像它不属于任何类别

如何检测输入字段是否没有必填且没有占位符?

如何在 flex 项目中忽略子元素的宽度

如何让一个 div 元素粘在另一个元素上?

如何在 css 和 html 中创建花状 struct

CSS:第一个类型的伪类没有按预期工作

如何使用 CSS Flex 设计一个导航栏,其中一组项目左对齐而另一组项目右对齐?

使用 CSS 样式化进度条

如何创建带有偏移边框线的双色边框?

无法使用 HTML 中的 Bootstrap 自上而下居中