PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » CSS » CSS Triangle

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

  10. Soony says:

    Now perfect in IE

  11. Derek says:

    You can use those triangles to create Real-time 3D objects in IE6. Yes, IE6!
    http://www.uselesspickles.com/triangles/

    Borders can also build a house:
    http://www.designdetector.com/tips/3DBorderDemo2.html

  12. Ted says:

    I noticed a small issue with this technique in Firefox 5.0 (Win). FF 5.0 renders a light stroke on the front sides of the triangle. Here’s a link to a jsFiddle that replicates the issue:

    http://jsfiddle.net/brightlight/4DmFk/

    Obviously, you’ll need to view that in FF 5.0 (Win) to see what I’m talking about.

    • Ted says:

      Update: Changing the border style to “inset” on the transparent sides of the element seems to fix this issue in Firefox 5 and 6. Other browsers continue to render the triangles fine even after this border-style adjustment.

Speak, my friend

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 ~