How do I make a CSS triangle with smooth edges?

后端 未结 8 709
再見小時候
再見小時候 2020-12-13 13:56

I have a triangle (JSFiddle) using this CSS:

.triangle {
    width: 0;
    height: 0;
    border-top: 0;
    border-bottom: 30px solid #666699;
    border-le         


        
8条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 14:34

    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:

    .triangle {
      width: 0;
      height: 0;
      border-top: 0;
      border-bottom: 20px solid #666699;
      border-left: 20px solid transparent; 
      border-right: 20px solid transparent;
      -moz-transform: scaleY(1.5); // optional: replace with Sass/SCSS/LESS mixin
      -moz-transform-origin: top; // optional: replace with mixin, too
    }
    

    This fixed the aliasing across the edge for me. JSFiddle here (Mozilla only right now). Hope this helps!

提交回复
热议问题