html - 使图像在其他图像下方对齐

标签 html css flexbox

我正在尝试使用 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>

最佳答案

一种使用 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>

关于html - 使图像在其他图像下方对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61611150/

相关文章:

html - 如何组织 CSS - 表格列

html - Flexslider 使用 HREF 丢失填充

javascript - 在 Javascript 中,无法从数组中随机正确获取第二次值

javascript - 防止网站在 WebView 中加载并在浏览器中打开

css - WordPress 创世纪 : Move Header and Navigation Menu Above the Wrap

html - 如何使用 flexbox 使第一个元素包裹在其他元素之上?

html - 我做错了什么? CSS输入类型文件并提交

java - 如何让我的 css 在标签 GWT Java 上工作

html - 元素内的 Flex 容器和大宽度图像

html - 使用内部带有滚动 div 的 flex 容器