【发布时间】:2019-09-03 03:25:32
【问题描述】:
这只是其中的一天,由于某种原因,我遇到了这个问题。这个想法是,画廊将获得 100% 的页面宽度,画廊中的一行将获得 80% 的宽度。将有三幅图像将在每行中的列中,每列是 33.3%。我希望它们如下图所示显示,但由于某种原因,两行只是彼此相邻显示。
Image of layout 我的代码如下:
.gallery-wrap {
width: 100%;
display: flex;
}
@media only screen and (max-width: 500px) {
.gallery-wrap {
width: 100%;
display: inline;
}
}
@media only screen and (max-width: 500px) {
.gallery-row {
width: 80%;
margin-left: auto;
margin-right: auto;
}
}
.gallery-row {
width: 80%;
display: flex;
margin-left: auto;
margin-right: auto;
}
.gallery-col {
width: 33.3%;
float: left;
}
@media only screen and (max-width: 500px) {
.gallery-col {
width: 100%;
}
}
.gallery-img {
width: 90%;
margin-left: auto;
margin-right: auto;
}
<div class="gallery-wrap">
<!-- Row 1 Example -->
<div class="gallery-row">
<div class="gallery-col">
<img class="gallery-img " src="https://via.placeholder.com/1500" alt="Gallery image">
</div>
<div class="gallery-col">
<img class="gallery-img " src="https://via.placeholder.com/1500" alt="Gallery image">
</div>
<div class="gallery-col">
<img class="gallery-img " src="https://via.placeholder.com/1500" alt="Gallery image">
</div>
</div>
<!-- Row 2 Example -->
<div class="gallery-row">
<div class="gallery-col">
<img class="gallery-img " src="https://via.placeholder.com/1500" alt="Gallery image">
</div>
<div class="gallery-col">
<img class="gallery-img " src="https://via.placeholder.com/1500" alt="Gallery image">
</div>
<div class="gallery-col">
<img class="gallery-img " src="https://via.placeholder.com/1500" alt="Gallery image">
</div>
</div>
</div>
在桌面上,每一行都应显示为 1x3 网格。在移动设备上,列宽变为 100%,因此列占 80%,每行只有一张图像。
不知何故,我设法迷失了方向,因此我们将不胜感激。
UPDATE
我变了
.gallery-wrap {
width: 100%;
display: flex;
}
到
.gallery-wrap {
width: 100%;
display: block;
}
解决了桌面问题,我只需要像 Mobile Layout 那样正确堆叠它们
任何帮助将不胜感激。
【问题讨论】:
-
gallery-wrap 设置为 display:flex。将其更改为显示:块。
-
这适用于桌面@FaisalRashid。关于如何让它们为移动设备堆叠的任何想法?
-
在gallery-row的媒体查询中,添加flex-direction: column。
-
已解决@FaisalRashid,发布答案,我会为您接受。非常感谢:)
-
你为什么要在三个相同的媒体查询中定义样式。你可以只用一个。作为旁注。