Search code examples
cssgeometrycss-shapessmoothingedges

How do I make a CSS triangle with smooth edges?


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.)


Solution

  • Even in pure CSS we can get the smooth diagonals.

    .triangle {
        width: 0;
        height: 0;
        border-top: 0;
        border-bottom: 30px solid #666699;
        border-left: 20px solid rgba(255, 255, 255, 0); 
        border-right: 20px solid rgba(255, 255, 255, 0);
    }
    

    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