首页 > 解决方案 > 让图像在其他图像下方对齐

问题描述

我正在尝试使用 Flexbox 创建一个图片库,类似于 AirBnb 网站上的图片库。画廊由 5 张图片组成。第一个图像占用容器宽度的 50% 和高度的 100%。其余图像 (4) 分别占容器的 25% 宽度和 50% 高度。

我尝试使用下面的代码,但我无法让图像 4 和 5 在图像 2 和 3 的正下方对齐。以及图像 1 的一侧。

我需要帮助才能完成这项工作。我当前的代码如下

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  margin: 0 auto;
}

.row {
  width: 100%;
  height: 100%;
  max-width: 1000px;
  margin: 10px 0;
  display: flex;
  flex-wrap: wrap;
}

.img-1 {
  background: url("https://images.unsplash.com/photo-1422205512921-12dac7b3b603?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2255&q=80")
    no-repeat center center;
  background-size: cover;
  width: 100%;
}

.img-2 {
  background: url("https://images.unsplash.com/photo-1528696892704-5e1122852276?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80")
    no-repeat center center;
  background-size: cover;
  width: 100%;
}

.img-3 {
  background: url("https://images.unsplash.com/photo-1524678606370-a47ad25cb82a?ixlib=rb-1.2.1&auto=format&fit=crop&w=2250&q=80")
    no-repeat center center;
  background-size: cover;
  width: 100%;
}

.img-4 {
  background: url("https://images.unsplash.com/photo-1563305145-b64e0c2184aa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80")
    no-repeat center center;
  background-size: cover;
  width: 100%;
}

.img-5 {
  background: url("https://images.unsplash.com/photo-1559136555-9303baea8ebd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80")
    no-repeat center center;
  background-size: cover;
  width: 100%;
}

.row {
  height: 600px;
}

.img-1 {
  height: 100%;
  width: 50%;
}

.img-2,
.img-3,
.img-4,
.img-5 {
  width: 25%;
  height: 50%;
}
<div class="container">
  <div class="row">
    <div class="img-1"></div>
    <div class="img img-2"></div>
    <div class="img img-3"></div>
    <div class="img img-4"></div>
    <div class="img img-5"></div>
  </div>
</div>

标签: htmlcssflexbox

解决方案


一种使用 flexbox 且没有额外 div 的方法:

.row {
  max-width: 1000px;
  height: 500px;
  margin:  auto;
  display: flex;
  flex-direction:column;
  flex-wrap: wrap;
}
.img {
  width: 25%;
  height:50%;
  background:center/ cover no-repeat;
}
.img-1 {
  background-image: url("https://images.unsplash.com/photo-1422205512921-12dac7b3b603?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2255&q=80");
  width: 50%;
  height: 100%;
}

.img-2 {
  background-image: url("https://images.unsplash.com/photo-1528696892704-5e1122852276?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80");
}

.img-3 {
  background-image: url("https://images.unsplash.com/photo-1524678606370-a47ad25cb82a?ixlib=rb-1.2.1&auto=format&fit=crop&w=2250&q=80");
  order:4;
}

.img-4 {
  background-image: url("https://images.unsplash.com/photo-1563305145-b64e0c2184aa?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80");
  order:3;
}

.img-5 {
  background-image: url("https://images.unsplash.com/photo-1559136555-9303baea8ebd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2250&q=80");
  order:5;
}
<div class="row">
    <div class="img img-1"></div>
    <div class="img img-2"></div>
    <div class="img img-3"></div>
    <div class="img img-4"></div>
    <div class="img img-5"></div>
</div>


推荐阅读