【发布时间】:2018-07-06 19:15:58
【问题描述】:
我正在制作 WordPress 主题,并使用 CSS 网格制作布局。而且我想不出为什么第一张图片和底部的图片之间有额外的差距。
更新:添加了 sn-p,将 grip-gap 更改为 1%,因为 1px 无法正常工作
.wrapper {
display: grid;
grid-template-columns: 33.3% 33.3% 33.3%;
grid-template-rows: 33.3% 33.3% 33.3%;
margin: auto;
width: 80%;
grid-gap: 1%;
}
.item img{
width: 100%;
padding: 4px;
background-color: black;
display: block;
}
.item:first-of-type{
grid-column: 1/3;
grid-row: 1/3;
}
<div class="wrapper">
<div class="item"><img src="http://static1.squarespace.com/static/5898e29c725e25e7132d5a5a/58aa11bc9656ca13c4524c68/58aa11e99656ca13c45253e2/1487540713345/600x400-Image-Placeholder.jpg?format=original" alt=""></div>
<div class="item"><img src="http://static1.squarespace.com/static/5898e29c725e25e7132d5a5a/58aa11bc9656ca13c4524c68/58aa11e99656ca13c45253e2/1487540713345/600x400-Image-Placeholder.jpg?format=original" alt=""></div>
<div class="item"><img src="http://static1.squarespace.com/static/5898e29c725e25e7132d5a5a/58aa11bc9656ca13c4524c68/58aa11e99656ca13c45253e2/1487540713345/600x400-Image-Placeholder.jpg?format=original" alt=""></div>
<div class="item"><img src="http://static1.squarespace.com/static/5898e29c725e25e7132d5a5a/58aa11bc9656ca13c4524c68/58aa11e99656ca13c45253e2/1487540713345/600x400-Image-Placeholder.jpg?format=original" alt=""></div>
<div class="item"><img src="http://static1.squarespace.com/static/5898e29c725e25e7132d5a5a/58aa11bc9656ca13c4524c68/58aa11e99656ca13c45253e2/1487540713345/600x400-Image-Placeholder.jpg?format=original" alt=""></div>
<div class="item"><img src="http://static1.squarespace.com/static/5898e29c725e25e7132d5a5a/58aa11bc9656ca13c4524c68/58aa11e99656ca13c45253e2/1487540713345/600x400-Image-Placeholder.jpg?format=original" alt=""></div>
</div>
【问题讨论】:
-
在您的
first-of-type下尝试将您的间距设置为 0px 并查看它的行为。正如 Cale 所说,虽然我们更难使用 PHP,但是如果您获得渲染 HTML 的 sn-p,我们可以检查一下。 -
@cale_b 该图片来自网络前端。但这是为什么呢?如果我删除 .box:first-of-type{ grid-column: 1/3;网格行:1/3;看起来不错。当 gap 为 0 时,该 gap 仍然存在
-
@whothatisShake 如果您检查页面本身的元素,它将为您提供呈现的 HTML,您可以复制和粘贴来代替 PHP。 :) 我们无法访问与您相同的内容,因此它限制了我们真正可以使用的内容。发布 HTML,我们中的一个人应该能够告诉你发生了什么!
-
如果我猜的话,根据你所说的,似乎通过将第一张图像拉伸到第三行,它没有到达间隙的结尾,它到达了开头两边的缝隙。
-
box-sizing:border-box是您在 img 元素上缺少的内容