可以使用CSS的flex布局来实现。在父容器上设置display: flex和flex-wrap: wrap,子容器上设置flex-grow: 1和margin: 0即可。

HTML代码:

<div class="container">
  <div class="box box1"></div>
  <div class="box box2"></div>
  <div class="box box3"></div>
  <div class="box box4"></div>
  <div class="box box5"></div>
</div>

CSS代码:

.container {
  display: flex;
  flex-wrap: wrap;
}

.box {
  width: 100px;
  flex-grow: 1;
  margin: 0;
}

.box1 {
  height: 50px;
  background-color: red;
}

.box2 {
  height: 70px;
  background-color: blue;
}

.box3 {
  height: 90px;
  background-color: green;
}

.box4 {
  height: 60px;
  background-color: yellow;
}

.box5 {
  height: 80px;
  background-color: orange;
}

这样,不同高度但相同宽度的盒子就可以在父容器中自动排列,并且紧贴着布局。

如何让高度不一样但是宽度一样的盒子在一个大盒子里面紧贴着布局

原文地址: https://www.cveoy.top/t/topic/bkgF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录