CSS 网格和变换:图像溢出和重叠

CSS Grid and transform: images overflow and overlap

我还是个初学者,抱歉,如果这是一个愚蠢的问题,我会尽量保持简短和具体。

所以,我想使用 CSS 网格显示和变换比例属性在图像网格中创建缩小效果。

我得到的结果与预期的结果不符,我将在代码后 post 两者的图像。

这是我使用的代码:

.meals-gallery {
  background-color: #000;
  display: grid;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(2, 1fr);
  overflow: hidden;
  padding: 0;
}

.meals-gallery img {
  opacity: 0.7;
  transform: scale(1.15);
  transition: 0.5s;
  width: 100%;
}

.meals-gallery img:hover {
  opacity: 1;
  transform: scale(1.03);
}
<section class="meals-gallery">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
</section>

图像在 70% 的不透明度下重叠,所以它们看起来很难看,即使我添加了溢出 属性 来隐藏..

这是想要的效果:

这是我取得的成就:

提前致谢。

我认为我们需要将 img 元素包装在容器中,例如 div,并在 div 上使用 overflow: hidden;。然后我们可以 transform: scale img 个元素放在 div:hover 上。我认为这就是您想要的效果:

.meals-gallery {
  background-color: #000;
  display: grid;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(2, 1fr);
  overflow: hidden;
  padding: 0;
}

.meals-gallery > div {
  overflow: hidden;
  position: relative;
}

.meals-gallery img {
  opacity: 0.7;
  transform: scale(2);
  transform-origin: 50% 50%;
  transition: all 0.5s ease-in-out;
  display: block;
  width: 100%;
}

.meals-gallery div:hover img {
  opacity: 1;
  transform: scale(1.1);
}
<section class="meals-gallery">
  <div><img src="http://placeholder.pics/svg/600x500" alt=""></div>
  <div><img src="http://placeholder.pics/svg/600x500" alt=""></div>
  <div><img src="http://placeholder.pics/svg/600x500" alt=""></div>
  <div><img src="http://placeholder.pics/svg/600x500" alt=""></div>
  <div><img src="http://placeholder.pics/svg/600x500" alt=""></div>
  <div><img src="http://placeholder.pics/svg/600x500" alt=""></div>
  <div><img src="http://placeholder.pics/svg/600x500" alt=""></div>
  <div><img src="http://placeholder.pics/svg/600x500" alt=""></div>
</section>

由此blog

grid-gap property to create a gap between my columns and rows of 10px. This property is a shorthand for grid-column-gap and grid-row-gap so you can set these values individually.

.meals-gallery {
  background-color: #000;
  display: grid;
  grid-template-columns: repeat(4, 35%);
  grid-template-rows: repeat(2, 1fr);
}

.meals-gallery img {
  display: grid;
  grid-template-columns: 100px 100px 100px;
  grid-gap: 10px;
  width: 100%;
  background-color: #fff;
  color: #444;
}

.meals-gallery img:hover {
  opacity: 1;
  transform: scale(1.03);
}
<section class="meals-gallery">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
  <img src="http://placeholder.pics/svg/600x500" alt="">
</section>