A Web Developer's Field Guide

Creating Triangles in CSS


Last Update
18 January 2011
howto, effects

I’ve come across a few techniques and tips in my career, while working at my last gig a co-worker pointed me to this technique. I believe this was originally discovered by the legendary Eric Meyer, but I couldn’t find much documentation about it on the web so I thought I would describe it here.

How it works

Few people realize when a browser draws the borders, it draws them at angles. This technique takes advantage of that. One side of the border is colored for the color of the arrow, and the rest are transparent. Then you set the width of the border to something large, the ones above are 20px. To demonstrate here is a div with all sides colored.

1 .css-arrow-multicolor {
2   border-color: red green blue orange;
3   border-style:solid;
4   border-width:20px;
5   width:0;
6   height:0;
7 }

As you can see there are triangles hidden in that square. These triangles are right triangles with a little tweaking with the border sizes you can get acute triangles.

1 .css-arrow-acute {
2   border-color: red green blue orange;
3   border-style:solid;
4   border-width:25px 10px 15px 30px;
5   width:0;
6   height:0;
7 }

With a little creativity and tweaking there are lots of shapes that can be made.


Now for application:


This is a classic chat bubble, no images used.

1 <div class="chat-bubble">
2   Buongiorno!
3   <div class="chat-bubble-arrow-border"></div>
4   <div class="chat-bubble-arrow"></div>
5 </div>
 1 .chat-bubble {
 2   background-color:#EDEDED;
 3   border:2px solid #666666;
 4   font-size:35px;
 5   line-height:1.3em;
 6   margin:10px auto;
 7   padding:10px;
 8   position:relative;
 9   text-align:center;
10   width:300px;
11   -moz-border-radius:10px;
12   -webkit-border-radius:10px;
13   -moz-box-shadow:0 0 5px #888888;
14   -webkit-box-shadow:0 0 5px #888888;
15 }
16 .chat-bubble-arrow-border {
17   border-color: #666666 transparent transparent transparent;
18   border-style: solid;
19   border-width: 10px;
20   height:0;
21   width:0;
22   position:absolute;
23   bottom:-22px;
24   left:30px;
25 }
26 .chat-bubble-arrow {
27   border-color: #EDEDED transparent transparent transparent;
28   border-style: solid;
29   border-width: 10px;
30   height:0;
31   width:0;
32   position:absolute;
33   bottom:-19px;
34   left:30px;
35 }

This technique doesn’t work in ie6 as is, mainly because ie6 doesn’t allow transparent borders, but there is a fix for that. What you need to do is give the “transparent” sides a completely different color like pink and then use filter: chroma to turn that color transparent.

1 /* IE6 */
2 .chat-bubble-arrow {
3     _border-left-color: pink;
4     _border-bottom-color: pink;
5     _border-right-color: pink;
6     _filter: chroma(color=pink);
7 }