How do CSS triangles work?

Stanislav Shabalin picture Stanislav Shabalin · Aug 16, 2011 · Viewed 174.5k times · Source

There're plenty of different CSS shapes over at CSS Tricks - Shapes of CSS and I'm particularly puzzled with a triangle:

CSS Triangle

How and why does it work?

Answer

sdleihssirhc picture sdleihssirhc · Aug 16, 2011

CSS Triangles: A Tragedy in Five Acts

As alex said, borders of equal width butt up against each other at 45 degree angles:

borders meet at 45 degree angles, content in middle

When you have no top border, it looks like this:

no top border

Then you give it a width of 0...

no width

...and a height of 0...

no height either

...and finally, you make the two side borders transparent:

transparent side borders

That results in a triangle.