我想在一个网页上显示20张左右的图像。图像可以具有纵向或横向。我想在更大的屏幕上以每行3种的方式构建它们,在手机上减少到每行1片,对于平板电脑类设备则可能是每行2片。但是布局也可能取决于视口的方向。我有这些要求:
我读过很多文章,而不是与表格解决方案有关,而是与div解决方案有关。任何帮助将不胜感激。
答案 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>