在div上使用百分比transformY可能会导致相邻div之间出现接缝

发布于 2025-01-17 22:36:00 字数 896 浏览 2 评论 0原文

我一直在使用 CSS3 转换在我的网站中创建一个弹出窗口,但我注意到该弹出窗口中相邻 div 之间存在接缝。
我做了一个最小的例子来演示。

#wrapper {
  width: 50px;
  transform: translateY(50%);
}

#a,
#b {
  background: red;
}

#a {
  height: 50px;
  width: 100px;
}

#b {
  height: 51px;
  width: 150px;
}
<div id="wrapper">
  <div id="a">
  </div>
  <div id="b">
  </div>
</div>

result

您可以看到 #a 和 #b 之间有一个接缝。

我深入研究了这个问题,发现在 chrome 中使用具有一定高度的百分比变换Y可能会导致这个问题(我没有在 Firefox 中测试它)。

在我的示例中,如果 #wrapper 的高度是奇数,translateY(50%) 将在子元素中创建接缝。

你可以尝试将 #b 的高度设置为 52px,你会看到接缝消失。
在我的网站中,我通过为该弹出窗口设置固定高度来修复它,但有时你无法控制高度,我想知道这是否是 chrome 中的错误,或者我没有做对。

抱歉我的英语不好。

I've been using CSS3 transform to create a popup in my website, but I noticed there's a seam between adjacent divs in that popup.
I made a minimal example to demonstrate.

#wrapper {
  width: 50px;
  transform: translateY(50%);
}

#a,
#b {
  background: red;
}

#a {
  height: 50px;
  width: 100px;
}

#b {
  height: 51px;
  width: 150px;
}
<div id="wrapper">
  <div id="a">
  </div>
  <div id="b">
  </div>
</div>

result

You can see there's a seam between #a and #b.

I've digged into that problem and found out using percentag transformY with some certain height may cause this problem in chrome(I didn't tested it in firefox).

In my example, if the #wrapper's height is an odd number, translateY(50%) will create a seam in child elements.

You can try to set #b's height to 52px and you'll see that seam disappear.
In my website I fixed it by setting a fixed height for that popup, but sometimes you just can't control height, I want to know if this is a bug in chrome or I didn't make it right.

Sorry for my bad English.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何止钟意 2025-01-24 22:36:00

添加Backface-visibily:hidden; <代码> #wrapper 。那将完成这项工作。

#wrapper {
  width: 50px;
  transform: translateY(50%);
   backface-visibility: hidden; 
}

#a,
#b {
  background: red;
}

#a {
  height: 50px;
  width: 100px;
}

#b {
  height: 51px;
  width: 150px;
}
<div id="wrapper">
  <div id="a">
  </div>
  <div id="b">
  </div>
</div>

Add backface-visibility: hidden; to #wrapper . that will do the job.

#wrapper {
  width: 50px;
  transform: translateY(50%);
   backface-visibility: hidden; 
}

#a,
#b {
  background: red;
}

#a {
  height: 50px;
  width: 100px;
}

#b {
  height: 51px;
  width: 150px;
}
<div id="wrapper">
  <div id="a">
  </div>
  <div id="b">
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文