首页 > 解决方案 > 使用 CSS Grid/Flexbox 的专门布局

问题描述

我希望我能获得一些关于实现我一直在尝试重新创建的某个图片库布局的提示,这是布局: 在此处输入图像描述

链接到该网站https://www.styleshout.com/templates/preview/Sublime10/index.html

正如您所看到的,每一行上的一个图像都比另一行高一点,并且其他行完全适合彼此。

这是我所做的一个微不足道的代码笔尝试,但我完全不知道如何获得这种布局:

body {
  margin: 0;
  border-sizing: border-box;
  display: flex;
  justify-content: center;
  width: 100%;
}

figure {
  margin: 0;
  display: inline;
}

.gallery-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    width: 600px;
    

    figure {
      height: 350px;
      picture img {
        width: 350px;
        height: 350px;
      }
    }
  }
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Flexible Image Gallery</title>
</head>
<body>
  <div class="gallery-container">
      <figure>
        <picture>
          <img src="https://source.unsplash.com/random/204x200" alt="" />
        </picture>
      </figure>
      <figure>
        <picture>
          <img src="https://source.unsplash.com/random/209x200" alt="" />
        </picture>
      </figure>
      <figure>
        <picture>
          <img src="https://source.unsplash.com/random/205x200" alt="" />
        </picture>
      </figure>
      <figure>
        <picture>
          <img src="https://source.unsplash.com/random/202x200" alt="" />
        </picture>
      </figure>
      <figure>
        <picture>
          <img src="https://source.unsplash.com/random/201x200" alt="" />
        </picture>
      </figure>
      <figure>
        <picture>
          <img src="https://source.unsplash.com/random/206x200" alt="" />
        </picture>
      </figure>
    </div>
</body>
</html>

谢谢你的帮助!

标签: cssflexboxcss-grid

解决方案


这是我使用 flex 的解决方案:

  • 使用类创建 2 个 div 以.column在一列中制作 3 个图像
  • 添加类.grow使其flex: 1.2比其他类高 1.2 倍
  • 设置图像flex: 1以使其高度在容器中增长

看看我的演示:

body {
    margin: 0;
    border-sizing: border-box;
    display: flex;
    justify-content: center;
    width: 100%;
}

figure {
    margin: 0;
    display: inline;
    flex: 1;
    display: flex;
    flex-direction: column;
}
figure > img {
    flex: 1;
    object-fit: cover;
}
.column {
    display: flex;
    flex-flow: column wrap;
    height: 800px;
    width: 200px;
    overflow: hidden;
}

.grow {
    flex: 1.2;
}

.gallery-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    width: 600px;
}
<body>
    <div class="gallery-container">
        <div class="column">
            <figure>
                <img src="https://source.unsplash.com/random/204x200" alt="" />
            </figure>
            <figure>
                <img src="https://source.unsplash.com/random/209x200" alt="" />
            </figure>
            <figure class="grow">
                <img src="https://source.unsplash.com/random/205x200" alt="" />
            </figure>
        </div>
        <div class="column">
            <figure class="grow">
                <img src="https://source.unsplash.com/random/202x200" alt="" />
            </figure>
            <figure>
                <img src="https://source.unsplash.com/random/201x200" alt="" />
            </figure>
            <figure>
                <img src="https://source.unsplash.com/random/206x200" alt="" />
            </figure>
        </div>
    </div>
</body>

</html>
希望这有助于解决您的问题。


推荐阅读