弹性盒的异型布局

引言

本章我们使用 flex 弹性盒实现一个异型布局小案例:

在这里插入图片描述

思路分析

可以看到最终实现效果为一个流式布局,在流式布局的基础上又增加了每一列中两个不定高度图片的垂直方向居中对齐。
那么我们首先整理出一套 html 分割思路:
在这里插入图片描述

有了分割思路,接下来实现就很容易了,最终保证两张图片的上层容器在当前列垂直居中就可以了

源码展示

<body>
  <div class="container">
    <div class="contentBox">
      <div class="content">
        <img src="./image/异形布局/yx001.png" alt="" />
        <img src="./image/异形布局/yx002.png" alt="" />
      </div>
    </div>
    <div class="contentBox">
      <div class="content">
        <img src="./image/异形布局/yx003.png" style="height: 120px;" alt="" />
        <img src="./image/异形布局/yx004.png" style="height: 350px;" alt="" />
      </div>
    </div>
    ...
  </div>
</body>

css 部分:

.container {
  width: 90%;
  height: 100vh;
  margin: 0 auto;
  display: flex;
  gap: 20px;
}
.contentBox {
  flex: 1 1 0;
}
.contentBox:nth-child(2n - 1) img {
  height: 170px;
}
.contentBox:nth-child(2n) img:nth-child(1) {
  height: 300px;
}
.content {
  display: flex;
  height: 100%;
  flex-direction: column;
  justify-content: center;
}
img {
  width: 100%;
  margin-bottom: 10px;
  border-radius: 5px;
  cursor: pointer;
}
img:hover {
  transition: transform 0.3s;
  transform: scale(1.1);
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值