我有以下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: enter image description here

为了实现这一目标,我使用了以下风格:

.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%;
}

然而,我知道我这样做的方式有点混乱和不一致,例如,右侧的两幅图像占据了网格容器的50%,但我仍然需要一个我使用5px的间隙.

This is my Codepen and if I open it in Firefox I get a different result: enter image description here

看起来网格容器正在扩展其宽度,我不知道为什么,那么如何才能获得第一幅图像中的一致跨浏览器结果?

我之所以使用grid,是因为它具有响应性,我会在以后更改mobile的模板区域,所以使用Flexbox不是一个选项.

推荐答案

将每个图像包装在自己的容器/包装器(div或picture tag)中,并给它一个height of 100%,然后减go 每行和一半的间隙以适合主容器.

.container {
  position: absolute;
  bottom: 20px;
  left: 20px;
  display: grid;
  grid-template-areas:
    "area1 area2"
    "area1 area3";
  grid-template-rows: calc(50% - 2.5px) calc(50% - 2.5px); /* changed */
  height: 20%;
  gap: 5px;
}

/* new class */
.wrapper {
  height: 100%;
}

img {
  height: 100%;
}
<div class="wrapper image1">
   <img src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
</div>

.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: calc(50% - 2.5px) calc(50% - 2.5px);
  height: 20%;
  gap: 5px;
}

.wrapper {
  height: 100%;
}

.image1 {
  grid-area: area1;
}
.image2 {
  grid-area: area2;
}
.image3 {
  grid-area: area3;
}
img {
  height: 100%;
}
<div class="parent">
  <div class="container">
    <div class="wrapper image1">
      <img src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
    </div>
    <div class="wrapper image2">
      <img src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
    </div>
    <div class="wrapper image3">
      <img src="https://i.pinimg.com/474x/40/bf/41/40bf419913701440c78d66c7fd722e7e.jpg" />
    </div>
  </div>
</div>

Css相关问答推荐

将照片向下对齐至div并保持响应性

如何在容器查询体中重新定义:Root的CSS自定义属性值?

如何在横向和纵向布局上使用 CSS 网格中的纵横比来保持纵横比

不明白为什么 div 不均匀并且不填满页面

当滚动量较小时,在滚动问题上动画粘性导航

Puppeteer - 无法在wine 店网站中拉出正确的 CSS Select 器

在 React Native 中创建背景

为什么这个绝对定位的 ::after 伪元素会崩溃,除非父级有过滤器?

如何在 flex-wrap 中优先考虑第一行?

带有 gatsby-plugin-image 的 bootstrap 卡 ImgOverlay 未按预期调整大小

纯 CSS/JS 的 SVG 连续向内方形螺旋动画

使用 Flex Box Tailwind CSS 的元素不相等

如何制作一个div来包装两个浮动div?

svg 背景图像位置始终在 Internet Explorer 中居中,尽管 background-position: left center;

如何隐藏 Chrome 中 HTML5
元素上默认显示的箭头?

使用 CSS/HTML 更改悬停图像

溢出的文本可以居中吗?

thead 和 tbody 之间的间距

如何在浏览器的右下角放置一个div?

脉动的心脏 CSS 动画