triangles in pure css

25 March, 2009

There's some interesting information online in regards to creating triangles and other polygonal shapes purely using css. Tantek Çelik's article A Study of Regular Polygons and Scott Jehl's Image-free CSS Tooltip Pointers — A Use for Polygonal CSS? served as the main inspiration for this tutorial.

I was curious to play with these concepts and see how they function cross-browser.

Test 1 » Simple Triangle
<div class="triangle basic"></div>
The CSS
.triangle {
  width:                    0;
  height:                   0;
  
  border-bottom:            0;
}

  .triangle.basic {
    border-top:             40px solid red; 
    border-left:            25px solid transparent;
    border-right:           25px solid transparent;           
    
    /* IE6 specific hacks. See notes below. */
    _border-left-color:     black;
    _border-right-color:    black;
   
    _filter:                chroma(color=black);
  }
Internet Explorer Bugs and Fixes

Internet Explorer 6 does not comprehend setting a border colour to transparent. It seems to instead treat transparent as being equal to inherit, with the result being your left and right borders being the colour set in the body declaration of your css file.

Tervel Peykov has an article, Emulating border-color: transparent in Internet Explorer 6, which explains how IE6 can be "hacked" to display transparent borders using the propriatary filter attribute in css.

As noted in the article, the thing to be careful of when applying the filter is to make sure to choose a colour that is not applied or nested anywhere inside the parent you're applying it to.

For example, if your default font colour is black then set the filter in the css to something like pink or orange, or else your black text may vanish!

Cross-browser Compatability Chart

Based on tests I conducted, these are just some quick notes on how the popular browsers handle rendering polygon shapes.

Test 1 Results
  • Works perfectly. No major issues.
  • Work adequately. Edges appear slightly jagged & aliased.
  • Fails. Does not render correctly.
Results of css triangles for Test 1
WinXP + OS X OS X WinXP
Firefox 3 Safari 3 Opera 9 Firefox 2 Chrome 2 IE 8 IE 7 IE 6
Pass
Partially
Partially
Partially
Pass
Partially
Partially
Partially
Test 2 » Triangle with inner triangle
<div class="triangle basic outer">
  <div class="triangle basic inner"></div>
</div>
The CSS
.triangle.outer {
  position:           relative;
}

.triangle.inner {
  position:           absolute;
}

  .triangle.inner.basic {
    border-left:      24px solid transparent;
    border-right:     24px solid transparent;
    
    top:              39px solid yellow;
    bottom:           1px;  
    left:             -24px; 
  }
Internet Explorer Bugs and Fixes

Flat out, overlaying two polygons on top of each will just not work in Internet Explorer 6. Applying the filter attribute to the inner triangle has the weird result of making the triangle below basically invisible.

There really isn't a good workaround to fix this, so the best suggestion would be not to use this technique and instead use the tried and tested method of using a background image.

Cross-browser Compatability Chart
Test 2 Results
  • Works perfectly.
  • Work adequately.
  • Fails.
Results of css triangles for Test 2
WinXP + OS X OS X WinXP
Firefox 3 Safari 3 Opera 9 Firefox 2 Chrome 2 IE 8 IE 7 IE 6
Pass
Partially
Partially
Partially
Pass
Partially
Partially
Fails
Bonus » Integration with modal boxes

Here's just a simple example of how you can combine triangles with other elements to produce some simple models with a call-out.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.

In closing…

Depending on what the situation requires, using triangles and polygons rendered in css could easily reduce load times or provide a more versatile option to implementing a desired effect.

Nesting triangles may not be the best idea in the overall, as IE6 tends to break badly, but if you need to make simple shapes to accent an area of a design, this may be the perfect solution.