A Web Design Community curated by Chris Coyier

Code Snippets Gallery

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

8 Responses

  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

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.