如何制作边缘平滑的 CSS 三角形?
我有一个三角形(JSFiddle),使用这个CSS:
.triangle {
width: 0;
height: 0;
border-top: 0;
border-bottom: 30px solid #666699;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
}
和这个HTML:
<div class="triangle"></div>
这会形成一个三角形,但对角线呈锯齿状且像素化。我怎样才能使它们光滑? (我可以在 Safari 和 Chrome 中通过将它们加点来平滑它们,但这会破坏 Firefox 和 IE 中的三角形。)
I have a triangle (JSFiddle) using this CSS:
.triangle {
width: 0;
height: 0;
border-top: 0;
border-bottom: 30px solid #666699;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
}
And this HTML:
<div class="triangle"></div>
This makes a triangle, but the diagonal lines are jagged and pixelated. How can I make them smooth? (I was able to smooth them out in Safari and Chrome by making them dotted, but that broke the triangles in Firefox and IE.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于 Firefox,您可以添加 -moz-transform:scale(.9999);使边缘光滑。对于你的情况:
会成功。
For Firefox you can add -moz-transform: scale(.9999); to make smooth edges. In your case:
Will do the trick.
我刚刚偶然发现了同样的问题,并想出了这个技巧(至少,它在 Mac 上有效):
I have just stumbled upon the same problem, and figured out this trick (at least, it works on a Mac):
即使在纯 CSS 中我们也可以获得平滑的对角线。
您可以使用 rgba(255, 255, 255, 0),而不是提供透明。这再次提供透明。但 alpha=0 会使对角线变得平滑。
检查浏览器对 rgba 的支持 css-tricks.com/rgba-browser-support
谢谢
Even in pure CSS we can get the smooth diagonals.
Instead of giving transparent you can make use of rgba(255, 255, 255, 0). This again gives transparent. But the alpha=0 makes smooth diagonals.
Check the browser support for rgba css-tricks.com/rgba-browser-support
Thanks
在 Firefox 中使用边框样式
inset
实现透明边框可以得到更好的结果:Using border style
inset
for transparent borders gives much better results in Firefox:当我第一次遇到这个问题时,真正对我有帮助的是将一个均匀的三角形缩放一定的量。 Firefox 似乎对不等边三角形特别“前卫”。但有趣的是,渲染出的完美三角形没有锯齿状边缘。如果您的项目中可以进行 CSS 转换,请尝试:
这为我修复了边缘的锯齿问题。 JSFiddle 此处(目前仅限 Mozilla)。希望这有帮助!
What really helped me when first stumbling over this was to scale a uniform triangle by a certain amount. Firefox seems to be particularly 'edgy' with scalene triangles. Interesting though, perfect triangles get rendered without jagged edges. If CSS transforms are possible in your project, just try:
This fixed the aliasing across the edge for me. JSFiddle here (Mozilla only right now). Hope this helps!
一个非常hacky的方法是使用旋转的div
这里我使用两个div来显示一个三角形:
并旋转内部div以获得三角形的两个非右侧:
我没有试图找到数字之间的关系。
这是代码的小提琴:
http://jsfiddle.net/mohsen/HTMcF/
但我因此强烈建议您使用
canvas
元素。A very hacky way would be using a rotated div
Here I used two divs to show a triangle:
and rotated the inner div for two not right sides of triangle:
I didn't tried to find the relation between numbers.
Here is the fiddle of the code:
http://jsfiddle.net/mohsen/HTMcF/
BUT I would strongly suggest you to use
canvas
element for this reason.对我来说,使用
dashed
作为透明边框适用于大多数不会自动平滑边框的浏览器,而旋转 360 度则适用于旧的 Webkit:For me, using
dashed
for the transparent borders worked for most browsers that don't automatically smooth them and rotating 360 degrees worked for old Webkit:其他方法都不适合我,但我发现以下方法有效(偶然):
虚线/实线的混合和 rgba 修复在 FF31、IE11 和 Chrome36 中有效。
None of the others worked for me, but I found the following did (by accident):
The mixture of dashed/solid and the rgba fix worked in FF31, IE11, and Chrome36.