A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Home » Code Snippets » CSS » CSS Triangle Submit one!

CSS Triangle

HTML

You can make them with a single div. It's nice to have classes for each direction possibility.

<div class="arrow-up"></div>
<div class="arrow-down"></div>
<div class="arrow-left"></div>
<div class="arrow-right"></div>

CSS

The idea is a box with zero width and height. The actual width and height of the arrow is determined by the width of the border. In an up arrow, for example, the bottom border is colored while the left and right are transparent, which forms the triangle.

.arrow-up {
	width: 0;
	height: 0;
	border-left: 5px solid transparent;
	border-right: 5px solid transparent;

	border-bottom: 5px solid black;
}

.arrow-down {
	width: 0;
	height: 0;
	border-left: 20px solid transparent;
	border-right: 20px solid transparent;

	border-top: 20px solid #f00;
}

.arrow-right {
	width: 0;
	height: 0;
	border-top: 60px solid transparent;
	border-bottom: 60px solid transparent;

	border-left: 60px solid green;
}

.arrow-left {
	width: 0;
	height: 0;
	border-top: 10px solid transparent;
	border-bottom: 10px solid transparent; 

	border-right:10px solid blue;
}

Examples

Subscribe to The Thread

  1. Trippy! I can’t get my head around it!

  2. TeMc says:

    It’s basicly like a giant 3D border-corner. (when the left and top border-color are different the edge is diagonal, that’s being used to make this triangle).

    • Thanks, TeMc, now it all makes sense.

      By the way, it’s fun to play with the inner div’s right border width, and change the shape and size of the triangle!

  3. Ben says:

    Clever. I like seeing CSS doing things it shouldn’t

  4. 40a says:
    div {
       height:0px;
       width:0px;
       border-top:100px dashed transparent;
       border-right:100px solid #955;
    }

    http://www.cssplay.co.uk/menu/flag

  5. Tim says:

    Like it. Thanks.

  6. davyne says:

    Cool trick, I tried it first after seeing it elsewhere.
    Then I came across the ivy-leaf trick on this website, and if you want the triangle to scale with the text, change the ivy leaf to a triangle.

    down: \25bc
    up: \25b2
    right: \25b6

    I couldn’t find a “map”, but if you play around changing the last 4 digits in the url, you can find all kinds of cool stuff.
    http://www.decodeunicode.org/en/u+25bc

  7. Micky says:

    Haha, funny trick.

  8. This is amazing!! Thanks for sharing..

  9. Justin says:

    Thanks,

    This is just want I was looking for :)

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---