CSS flex-wrap 如何让高度不拉伸(stretch)

标签 css flexbox

<分区>

box3, box1box4, box6 之间有很大的差距如何消除差距?所以每个 box 都可以有动态高度?

.wrap {
  display: flex;
  align-items: baseline;
  align-content:flex-start;
  justify-content: space-between;
  flex-wrap: wrap; 
  background-color: lightblue;
}

.box {
  display: flex;
  background-color: tomato;
  box-sizing: border-box;
  border: 1px solid #C4C4C4;
  height: 100px;
  width: 45%;
  margin-top: 15px;
}

.box1, .box4 {
  height: 20px;
}
  <div class="wrap">
    <div class="box box1">box1</div>
    <div class="box box2">box2</div>
    <div class="box box3">box3</div>
    <div class="box box4">box4</div>
    <div class="box box5">box5</div>
    <div class="box box6">box6</div>
  </div>

这是所需的布局。谢谢

enter image description here

最佳答案

flex 的默认方向是行,当你使用 flex-wrap: wrap 将溢出的元素向下推到另一行,行高将默认始终等于该行的最高元素,这就是为什么你看到元素有那个差距。

如果您将 flex 方向更改为 column 并为 wrap 元素提供固定高度,以便将溢出的元素从上到下推到右侧,就可以做到这一点。

.wrap {
  /*Added these*/
  height: 300px;
  flex-direction: column;
  /*-----------*/
  display: flex;
  align-items: baseline;
  align-content: space-around;
  flex-wrap: wrap; 
  background-color: lightblue;

}

.box {
  display: flex;
  background-color: tomato;
  box-sizing: border-box;
  border: 1px solid #C4C4C4;
  height: 100px;
  width: 45%;
  margin-top: 15px;
}

.box1, .box5 {
  height: 20px;
}
  <div class="wrap">
    <div class="box box1">box1</div>
    <div class="box box2">box2</div>
    <div class="box box3">box3</div>
    <div class="box box4">box4</div>
    <div class="box box5">box5</div>
    <div class="box box6">box6</div>
  </div>

关于CSS flex-wrap 如何让高度不拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56999956/

上一篇:javascript - 如何将表单验证错误更改反射(reflect)到 CSS?

下一篇:HTML CSS : Button- Wrap Container Directly Around it

相关文章:

html - 未应用 z-index 值时订单元素堆栈出现问题

html - 截面动态高度

css - 灵活的主着陆页

html - 手机上如何让div内容占据整个屏幕?

html - Bootstrap 将按钮对齐到 flexbox 列的底部

html - 如何垂直居中和右对齐图像?

css - 如何更改 Bootstrap 标签和输入之间的距离

css - 如何更改 Bootstrap 中的滚动条样式?

css - 当宽度大于 flex 容器时如何计算 flex 收缩?

html - flexbox 的百分比高度