动态呈现图像多行多列

Presenting images dynamically multirow multicolumn

我想在网页上展示大约 20 张图片。图像可以纵向或横向。我想在更大的屏幕上以每行 3 个的方式构建它们,在手机上减少到每行 1 个,对于 tablet-like 设备可能每行 2 个。但布局也可能取决于视口的方向。我有这些要求:

我读过很多文章,但不使用 table-solution,而是使用 div-solution。任何帮助将不胜感激。

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>