正方形在CSS网格中旋转会产生空白

发布于 2025-01-23 18:46:08 字数 1548 浏览 2 评论 0原文

在CSS网格内旋转正方形div会产生间隙。为什么?

这是一个简单的例子:

div {
    height:100%;
    width: 100%;
    margin:0;
    display:block;
}

.square{ 
    background: skyBlue;
}

.rotate{
    transform:rotate(180deg);
}

#grid {
    display: grid;
    height:100vmin;
    width: 100vmin;
    grid-template-rows: repeat(6, 1fr);
    grid-template-columns: repeat(6, 1fr);

    background: white;
    border: 1px solid black;
    margin: auto;
}
<div id=grid>
     <div style="grid-row: 1; grid-column:2">normal</div>
     <div class="square" style="grid-row: 2; grid-column:2"></div>
     <div class="square" style="grid-row: 3; grid-column:2"></div>
     <div class="square" style="grid-row: 4; grid-column:2"></div>


     <div style="grid-row: 1; grid-column:4">rotated (180deg)</div>
     <div class="square rotate" style="grid-row: 2; grid-column:4"></div>
     <div class="square rotate" style="grid-row: 3; grid-column:4"></div>
     <div class="square rotate" style="grid-row: 4; grid-column:4"></div>
</div>
In the snipped code, one can see small gaps between the right column grids. Only the rotated grids apear to have gap between them, while the left column of "normal" (not rotated) grids seems smooth.

为什么旋转后发生间隙? 我该如何解决?

Rotating a square div inside a css grid create gaps. Why?

Here is a simple example:

div {
    height:100%;
    width: 100%;
    margin:0;
    display:block;
}

.square{ 
    background: skyBlue;
}

.rotate{
    transform:rotate(180deg);
}

#grid {
    display: grid;
    height:100vmin;
    width: 100vmin;
    grid-template-rows: repeat(6, 1fr);
    grid-template-columns: repeat(6, 1fr);

    background: white;
    border: 1px solid black;
    margin: auto;
}
<div id=grid>
     <div style="grid-row: 1; grid-column:2">normal</div>
     <div class="square" style="grid-row: 2; grid-column:2"></div>
     <div class="square" style="grid-row: 3; grid-column:2"></div>
     <div class="square" style="grid-row: 4; grid-column:2"></div>


     <div style="grid-row: 1; grid-column:4">rotated (180deg)</div>
     <div class="square rotate" style="grid-row: 2; grid-column:4"></div>
     <div class="square rotate" style="grid-row: 3; grid-column:4"></div>
     <div class="square rotate" style="grid-row: 4; grid-column:4"></div>
</div>

In the snipped code, one can see small gaps between the right column grids.
Only the rotated grids apear to have gap between them, while the left column of "normal" (not rotated) grids seems smooth.

Why are gaps occuring after rotation?
How can I fix it?

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

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

发布评论

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

评论(1

挽你眉间 2025-01-30 18:46:08

是否可以看到细线取决于浏览器/屏幕/缩放级别。

例如,在Chrome上,我在您的片段上放大了,这条线来了。

我认为问题就像是舍入错误/边缘效应。该系统正在尝试解决如何处理零件像素的方法 - 在这种情况下,蓝色元素的高度是通过计算然后旋转获得的网格空间的百分比。每次都不是确切的整数。在现代屏幕上,必须转换为一个CSS像素可以是几个屏幕像素。有时,有些屏幕像素会“落后”。 (请注意,细线非常细,不如一个CSS像素)。

查看您的代码是一种艰难的方式,就是将旋转DIV的大小增加几个像素,并相应地重置其位置。这可能适合您的现实生活状况。

div {
  height: 100%;
  width: 100%;
  margin: 0;
  display: block;
}

.square {
  background: skyBlue;
}

.rotate {
  transform: rotate(180deg);
}

#grid {
  display: grid;
  height: 100vmin;
  width: 100vmin;
  grid-template-rows: repeat(6, 1fr);
  grid-template-columns: repeat(6, 1fr);
  background: white;
  border: 1px solid black;
  margin: auto;
}

.rotate {
  height: calc(100% + 2px);
  width: calc(100% + 2px);
  margin-top: -1px;
  margin-left: -1px;
}
<div id=grid>
  <div style="grid-row: 1; grid-column:2">normal</div>
  <div class="square" style="grid-row: 2; grid-column:2"></div>
  <div class="square" style="grid-row: 3; grid-column:2"></div>
  <div class="square" style="grid-row: 4; grid-column:2"></div>


  <div style="grid-row: 1; grid-column:4">rotated (180deg)</div>
  <div class="square rotate" style="grid-row: 2; grid-column:4"></div>
  <div class="square rotate" style="grid-row: 3; grid-column:4"></div>
  <div class="square rotate" style="grid-row: 4; grid-column:4"></div>
</div>

Whether the thin lines are seen can depend on the browser/screen/zoom level.

For example on Chrome I zoomed on your snippet and the line came and went.

I think the problem is like a rounding error/edge effect. The system is trying to work out how to deal with part pixels - in this case with the height of a blue element being a % of a grid space which is obtained by calculation and then rotated. It's not come up with an exact integer every time. On modern screens it has to convert as one CSS pixel can be several screen pixels. Sometimes some screen pixels 'get left behind'. (Notice that the thin line is very thin, not as much as one CSS pixel).

Looking at your code a hacky way round is to increase the size of the rotated div by a couple of pixels and reset its position accordingly. This may or may not be suitable for your real life situation.

div {
  height: 100%;
  width: 100%;
  margin: 0;
  display: block;
}

.square {
  background: skyBlue;
}

.rotate {
  transform: rotate(180deg);
}

#grid {
  display: grid;
  height: 100vmin;
  width: 100vmin;
  grid-template-rows: repeat(6, 1fr);
  grid-template-columns: repeat(6, 1fr);
  background: white;
  border: 1px solid black;
  margin: auto;
}

.rotate {
  height: calc(100% + 2px);
  width: calc(100% + 2px);
  margin-top: -1px;
  margin-left: -1px;
}
<div id=grid>
  <div style="grid-row: 1; grid-column:2">normal</div>
  <div class="square" style="grid-row: 2; grid-column:2"></div>
  <div class="square" style="grid-row: 3; grid-column:2"></div>
  <div class="square" style="grid-row: 4; grid-column:2"></div>


  <div style="grid-row: 1; grid-column:4">rotated (180deg)</div>
  <div class="square rotate" style="grid-row: 2; grid-column:4"></div>
  <div class="square rotate" style="grid-row: 3; grid-column:4"></div>
  <div class="square rotate" style="grid-row: 4; grid-column:4"></div>
</div>

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