动态显示图像多行多列

时间:2018-12-07 08:11:38

标签: css multiple-columns viewport image-scaling landscape-portrait

我想在一个网页上显示20张左右的图像。图像可以具有纵向或横向。我想在更大的屏幕上以每行3种的方式构建它们,在手机上减少到每行1片,对于平板电脑类设备则可能是每行2片。但是布局也可能取决于视口的方向。我有这些要求:

  • 行高应该是该行中任何照片的最大高度。例如:连续两张肖像照片和一个风景,行高即成为肖像照片的高度。
  • 风景照片应放置在其单元格的中央,并在顶部/底部留出空白。
  • 肖像照片应完全占据其单元格。
  • 列应在可用宽度上平均分配(屏幕宽度为33.33%,50%或100%)。
  • 我还希望页面能够响应,即图像必须根据浏览器屏幕的宽度/高度缩小/放大。
  • 每张照片的标题,位于图片中央,紧靠图像顶部下方,以显示反色或小描述性文字。

我读过很多文章,而不是与表格解决方案有关,而是与div解决方案有关。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

body {
  margin: 0;
  padding: 0;
}

.gallery {
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  list-style: none;
}
.gallery > li {
  flex-basis: calc(100% / 3);
  display: flex;
  align-items: center;
  justify-content: stretch;
}
@media (min-width: 768px) and (max-width: 1024px) {
  .gallery > li {
    flex-basis: calc(100% / 2);
  }
}
@media (max-width: 767px) {
  .gallery > li {
    flex-basis: calc(100% / 1);
  }
}
.gallery > li > figure {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  position: relative;
  margin: auto;
}
.gallery > li > figure img {
  max-width: 100%;
  height: auto;
  display: block;
  order: 1;
}
.gallery > li > figure figcaption {
  order: 0;
  background-color: rgba(255, 255, 255, 0.5);
  width: 100%;
  text-align: center;
  position: absolute;
}
<ul class="gallery"> 
  <li><figure><img src="https://picsum.photos/g/200/300?image=0" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/200?image=10" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/200/300?image=20" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/300?image=30" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/g/300/300?image=40" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/200?image=50" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/200?image=60" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/g/200/300?image=70" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/300?image=80" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/200/300?image=90" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/200?image=100" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/200/300?image=110" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/g/200/300?image=120" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/200?image=130" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/300?image=140" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/300?image=151" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/200?image=160" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/200/300?image=170" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/300?image=180" alt=""><figcaption>This is a caption</figcaption></figure></li>
  <li><figure><img src="https://picsum.photos/300/300?image=190" alt=""><figcaption>This is a caption</figcaption></figure></li>
</ul>