DEV Community

Cover image for Drawing a triangle with CSS

Drawing a triangle with CSS

Alvaro Montoro on March 24, 2021

In this post, we are going to see different methods to draw a triangle in CSS. We will focus on three in particular: the traditional method using b...
Collapse
 
robole profile image
Rob OLeary •

Coincidence --> dev.to/dailydevtips1/css-shapes-tr...

Same topic, same day!

Collapse
 
alvaromontoro profile image
Alvaro Montoro •

It can be. There has been some talk about triangles/shapes in CSS lately (at least 1 or 2 posts in the past week).

My post started with something I wrote on Twitter that got me roasted yesterday, mainly for making an absolute statement that people didn't agree with, so I wanted to add a longer explanation:

Collapse
 
robole profile image
Rob OLeary •

😅 The perils of expressing yourself on Twitter!

Collapse
 
afif profile image
Temani Afif •

one line triangle using conic-gradient : jsfiddle.net/mwfskaLx/1/.. useful if we want to think angles instead of dimension
Also with mask jsfiddle.net/mwfskaLx/2/ and jsfiddle.net/mwfskaLx/3/

Collapse
 
alvaromontoro profile image
Alvaro Montoro •

These are great options, and they would simplify considerably the background-image method. I had conic-gradient aside because Firefox didn't support it (not to mention IE). But that has changed now in FF, and this is a great use of it.

Collapse
 
robole profile image
Rob OLeary •

Another con for clip-path is that you can't use box-shadow to create a shadow (you can use filter: drop-shadow() instead).

Outside of the realm of CSS art, I reach for SVG most of the time.

Collapse
 
alvaromontoro profile image
Alvaro Montoro •

I agree, but one thing: clip-path will hide the shadow from a filter too.

Collapse
 
robole profile image
Rob OLeary •

If you extend the clip-path beyond the base, you can include it.

Something like this gives you a 2px drop shadow..

 .triangle {
        background-color: red;
        width: 200px;
        height: 100px;
        margin: 0 auto;
        clip-path: polygon(50% 0%, 0% 200%, 100% 200%);
        filter: drop-shadow(0px 2px 0 black);
      }
Enter fullscreen mode Exit fullscreen mode

jsfiddle.net/robjoeol/68d350zr/9/

Collapse
 
xr0master profile image
Sergey Khomushin •

Thank you for the article!
Using CSS borders, you can create any triangles of different angles (but not more than 90). It is not at all necessary to divide by 2 and be limited to only this.
jsfiddle.net/m0zwLdju/

Collapse
 
alvaromontoro profile image
Alvaro Montoro •

You are correct, that's not the only way to draw a triangle using the borders. I updated the article to add a note indicating that those are the steps for that specific triangle and other triangles will be slightly different:

Note: The steps above are for a specific triangle. The actual steps will change (slightly) depending on the type of triangle that we want to draw. In general, the idea is to set 2-3 borders as transparent and 1-2 borders with color and then adjust the border sizes.

Collapse
 
calinbaenen profile image
Calin Baenen •

This is a very creative post. I like it.

Collapse
 
alvaromontoro profile image
Alvaro Montoro •

Thanks!