html - Flexbox 在不改变 HTML 的情况下在上方保留一个元素,在下方保留两个元素

标签 html css

我有像这样的简单 div:

.container {
  width: 20rem;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.child {
  width: 50%;
}
<div class="container">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
</div>

这使得 child 1 和 child 2 在上方,而 child 3 在下方居中。我的问题是,在不改变 HTML 结构的情况下,有什么方法可以使 child 1 在上方居中,而 child 2 和 child 3 在下方?

最佳答案

您可以使用*-reverse 并更新订单:

.container {
  width: 20rem;
  display: flex;
  flex-wrap: wrap-reverse;
  flex-direction: row-reverse;
  justify-content: center;
}

.child {
  width: 50%;
  height: 50px;
  background: red;
  color:#fff;
  font-size:20px;
}

.child:nth-last-child(1) {order: 1;}
.child:nth-last-child(2) {order: 2;}
.child:nth-last-child(3) {order: 3;}
<div class="container">
  <div class="child">1</div>
  <div class="child">2</div>
  <div class="child">3</div>
</div>

关于html - Flexbox 在不改变 HTML 的情况下在上方保留一个元素,在下方保留两个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64996544/

相关文章:

css - 为什么我的绝对定位的 div 在 IE 中不会停留在顶层?

php - 如何使用边框半径调整图像大小?

html - CSS: float:left with a margin-right 不会将所有元素推开

css - 无法让文本框填充面板

jquery - IE10 - 需要将 CSS 光标更改为 "pointer"在 contenteditable <div> 中 <span> 上的鼠标输入

javascript - css 和 jquery 缺少元素背景颜色

html - CSS:字体语句导致行高不起作用

css - IE6 透明背景颜色不起作用

css - 为什么 W3C CSS Validator 在 "Ends-with"属性值选择器上显示解析错误

javascript - 启用浏览器滚动条工作