带有图像的CSS网格在Firefox中的行为不同
我有此html:
<div class="parent">
<div class="container">
<img class="image1" src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
<img class="image2" src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
<img class="image3" src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
</div>
</div>
我希望.container
div的大小由其.parent
的20%定义。这就是我在Chrome中得到的:
为了实现我使用这些样式:
.parent {
position: relative;
width: 800px;
height: 300px;
background-color: green;
margin: auto;
}
.container {
position: absolute;
bottom: 20px;
left: 20px;
display: grid;
grid-template-areas:
"area1 area2"
"area1 area3";
grid-template-rows: 50% 50%;
height: 20%;
gap: 5px;
}
.image1 {
grid-area: area1;
}
.image2 {
grid-area: area2;
}
.image3 {
grid-area: area3;
}
img {
height: 100%;
}
但是,我知道我的方式有点混乱,而不是一致的,例如,右边的2个图像占用了Grid容器的50%,但是我仍然需要一个我使用5px
的差距。
this 是我的codepen,如果我在firefox中打开它,我会得到不同的结果:
看起来网格容器正在扩展其宽度,我不知道为什么,那么在第一个图像中如何获得一致的交叉浏览器结果?
我使用网格是因为它会响应迅速,并且我将稍后更改移动设备的模板区域,因此使用FlexBox不是一个选择。
I have this HTML:
<div class="parent">
<div class="container">
<img class="image1" src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
<img class="image2" src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
<img class="image3" src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
</div>
</div>
I want the .container
div to have it's size defined by 20% of its .parent
. This is what I got in Chrome:
For achieving that I used these styles:
.parent {
position: relative;
width: 800px;
height: 300px;
background-color: green;
margin: auto;
}
.container {
position: absolute;
bottom: 20px;
left: 20px;
display: grid;
grid-template-areas:
"area1 area2"
"area1 area3";
grid-template-rows: 50% 50%;
height: 20%;
gap: 5px;
}
.image1 {
grid-area: area1;
}
.image2 {
grid-area: area2;
}
.image3 {
grid-area: area3;
}
img {
height: 100%;
}
However, I know the way I did is a bit messed and not consistent, for example, the 2 images on the right are taking 50% of the grid container, but I still need a gap which I used 5px
.
This is my Codepen and if I open it in Firefox I get a different result:
Looks like the grid container is expanding its width and I have no idea why, so how can I get a consistent cross browser result to look like in the first image?
I've used grid because it will be responsive and I'll change the template areas for mobile later, so using Flexbox isn't an option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将每个图像包装在其自己的容器 /包装器(DIV或图片标签)中,并给它一个100%< / code>的高度,然后减去每一行,一半的间隙以适合主容器中。
Wrap each image in its own container / wrapper (div or picture tag) and give it a
height of 100%
, then subtract each row and half the gap to fit in the main container.