简体   繁体   English

CSS 旋转的 div 边框显示奇怪的轮廓

[英]CSS rotated div border shows odd outline

I'm trying rotate a div with a border.我正在尝试旋转带边框的 div。 The border has the same color as the background.边框与背景颜色相同。 A very thin line appears between the border outline and the background.边框轮廓和背景之间出现一条很细的线。 Here is my code below.下面是我的代码。 I'm trying to get rid of the weird line.我试图摆脱奇怪的线。

 body { background-color: black; } div { width: 50px; height: 50px; background-color: white; border: 50px solid black; transform: rotate(45deg); }
 <div></div>

I tried multiple browsers.我尝试了多种浏览器。

I could fix this by using another div instead of a border, but I'm more interested in getting the border to work as expected.我可以通过使用另一个 div 而不是边框来解决这个问题,但我更感兴趣的是让边框按预期工作。

A simple fix of this is by using backface-visibility: hidden .一个简单的解决方法是使用backface-visibility: hidden

When an element rotates, it seems that the rendering of transform: rotate() may cause the back face of it to be shown, as if in 3D perspective.当一个元素旋转时,看起来transform: rotate()的渲染可能会导致它的背面显示出来,就像在 3D 透视中一样。

This possibly lead to the background-color of the back face (a mirror image the element's front face) overflowing the border edge in this case.在这种情况下,这可能会导致背面的background-color (元素正面的镜像)溢出边界边缘。

backface-visibility: hidden fix it by rendering the back face invisible, as shown in below example. backface-visibility: hidden通过使背面不可见来修复它,如下例所示。

On side note, MDN did mention that backface-visibility has no effect on 2D transforms, which indicates that this behavior of transform: rotate() to have perspective is more accidental than expected.在旁注中, MDN确实提到backface-visibility对 2D 变换没有影响,这表明transform: rotate()具有透视的这种行为比预期的更偶然。

Example:例子:

 body { background-color: black; display: flex; gap: 100px; } div { width: 50px; height: 50px; background-color: white; border: 50px solid black; transform: rotate(45deg); } div + div { /* fixed here */ backface-visibility: hidden; }
 <div></div> <div></div>

That's an interesting one as it only appears with the rotate transformation.这很有趣,因为它只出现在旋转变换中。 You can remove it using outline to paint over the thin line with a border which will also not affect the positioning of it as follows:您可以使用 outline 将其移除以在带有边框的细线上绘制,这也不会影响它的定位,如下所示:

 body { background-color: black; } div { width: 50px; height: 50px; background-color: white; border: 50px solid black; transform: rotate(45deg); outline-offset:-1px; outline: 2px solid black; }
 <div></div>

Firefox only仅限Firefox

My best guess is that there are rounding issues with the 3D rotation on the Z-axis in browser graphics functions.我最好的猜测是浏览器图形函数中Z-axis的 3D 旋转存在舍入问题。 Rotation on the X-axis and Y-axis show no issues. X-axisY-axis显示没有问题。 It seems the size of the element drawn is rounded down (smaller), while the browser calculates with the actual or rounded up values.绘制的元素的大小似乎向下舍入(更小),而浏览器使用实际值或向上舍入的值进行计算。 The difference will be a narrow gap where the element background-color is able to bleed through.不同之处在于元素background-color能够渗透的狭窄间隙。 I used red in the snippet to show this.我在片段中使用red来展示这一点。

Both transform: rotate(..) and transform: rotateZ(..) show the same issue when rotation is other than 0deg (usually the purpose of rotation).当旋转不是0deg (通常是旋转的目的)时,transform transform: rotate(..)transform: rotateZ(..)显示相同的问题。

To resolve the issue (Firefox only!!) use transform-style: preserve-3d with the element being rotated.要解决此问题(仅限 Firefox!!),请将transform-style: preserve-3d与正在旋转的元素一起使用。

 body { background-color: black; padding: 3rem; } div { width: 50px; height: 50px; background-color: red; border: 50px solid black; transform: rotate(45deg); }.test { transform-style: preserve-3d; /* voila. background colored border is gone..: */ margin-top; 4rem; /* create some distance */ }
 <div></div> <div class="test"></div>

try like below, no need border, just simply use margin.像下面这样尝试,不需要边框,只需使用边距即可。

 body { background-color: black; } div{ width: 50px; height: 50px; background-color: white; transform: rotate(45deg); border: 0px; /* border: 50px solid black; */ margin: 50px; }
 <div></div>

 body { background-color: black; } div { width: 50px; height: 50px; background-color: white; transform: rotate(45deg); border: 0px; /* border: 50px solid black; */ margin: 50px; }
 <div></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM