CSS Triangles

CSS Triangles

I was recently redesigning my website and wanted to create tooltips.  Making that was easy but I also wanted my tooltips to feature the a triangular pointer.  I'm a disaster when it comes to images and the prospect of needing to make an image for every color tooltip I wanted made me rethink my redesign.  Lucky for me, MooTools Core Developer Darren Waddell shared with me a great trick:  CSS triangles.  Using pure CSS you can create cross-browser compatible triangles with very little code!

The CSS

/* create an arrow that points up */
div.arrow-up {
	width:0px; 
	height:0px; 
	border-left:5px solid transparent;  /* left arrow slant */
	border-right:5px solid transparent; /* right arrow slant */
	border-bottom:5px solid #2f2f2f; /* bottom, add background color here */
	font-size:0px;
	line-height:0px;
}

/* create an arrow that points down */
div.arrow-down {
	width:0px; 
	height:0px; 
	border-left:5px solid transparent;
	border-right:5px solid transparent;
	border-top:5px solid #2f2f2f;
	font-size:0px;
	line-height:0px;
}

/* create an arrow that points left */
div.arrow-left {
	width:0px; 
	height:0px; 
	border-bottom:5px solid transparent;  /* left arrow slant */
	border-top:5px solid transparent; /* right arrow slant */
	border-right:5px solid #2f2f2f; /* bottom, add background color here */
	font-size:0px;
	line-height:0px;
}

/* create an arrow that points right */
div.arrow-right {
	width:0px; 
	height:0px; 
	border-bottom:5px solid transparent;  /* left arrow slant */
	border-top:5px solid transparent; /* right arrow slant */
	border-left:5px solid #2f2f2f; /* bottom, add background color here */
	font-size:0px;
	line-height:0px;
}

The secret to these triangles is creating giant borders to the two perpendicular sides of the direction you'd like the triangle to point. Make the opposite side's border the same size with the background color of whatever color you'd like the tooltip to be.  The larger the border, the larger the triangle.  You can color your triangles any color, any size, and in any direction. The best part is that there's very little code needed to achieve this effect.

I don't know how I didn't know about this technique sooner!  This neat trick will surely help me in the future and opens up a world of possibilities for me to improve existing tooltip elements.


Comments

  1. Alex Simon

    whoa! what am I missing here? – where are the browser hacks, the js, etc?
    Surly it can’t be that easy!

  2. iamunr

    @Alex Simon: That’s exactly what I was thinking! This is weird, incredible, and I can’t figure out why it’s working.

  3. Jacob Rask

    It’s a 0 pixel element in the middle, and browsers render border edges slanted which creates the border. It’s a great trick indeed. You can also do it with pseudo-elements, something like this:


    .tooltip:before {
    content: '';
    display: block;
    width: 0;
    height: 0;
    border: 5px solid;
    border-color: #2f2f2f transparent transparent;
    }

  4. digibomb

    Awesomeness!!! Can’t wait to try this with a slider i’m working on. Perfect for the next, prev buttons…

  5. Krystian

    It seems that CSS is more and more self-sufficient. Great example, thanks.

  6. Binyamin

    For IE6 you need to use in css color, transparent does not work.

  7. Dr. Azrael Tod

    WTF?
    Way better then my previous Solution (before-content set to unicode-Triangles), although i have to admit its pretty hard to understand why it works that way.
    Still.. have to try this in my private projects.

  8. Eric Wendelin

    Wait a minute Walsh, so you DIDN’T read my blog post about this months ago? Now I’m just a bit sad…

  9. David Walsh

    @Eric Wendelin: Mine is more focused on the triangle itself, plus you wrote that in Feb. I dont’ remember what *I* wrote in February.

  10. Sunny Singh

    I’ve been using this for at least a year now so it’s nothing new and requires no special CSS3 or anything. Nice to have a good resource for them though, I always use them for dropdowns and what not.

  11. Jesse G. Donat

    I’ve been using CSS triangles on my site http://donatstudios.com for quite a while – notice on the right under the headers, the little triangle – I hate images and that was a nice way around it.

  12. Jesse G. Donat

    Also I, based http://jsfiddle.net/Fk3Kj/ on the concept a back in February

    @Jesse G. Donat:

  13. fakedarren

    It’s easier to visualise when you see this:

    http://jsfiddle.net/ctCXY/

    The font-size of 0px (or line-height: 0px, or overflow: hidden) may be needed for IE6 in some cases.

  14. Danz

    I looked at how the borders are created and it makes sense as to why it would result in a triangular look. I continuously get designs with a lot triangles. Ingenious! Thanks for the tip!

  15. Eric M Wendelin

    @David Walsh: Heh, no worries :)

    Do you get the idea that CSS is getting much more verbose lately?

    • David Walsh

      A bit. I’m just glad the we seem to be moving forward.

  16. David Workman

    Great :) Took me a minute or so to figure out why it worked like that but not a trick I’ll be forgetting in a hurry :)

    For those still wondering why it works, do the following:

    - Draw a square on a piece of paper
    - Draw in the diagonals of the square so you have a box made of 4 triangles.

    What you have on the paper in front of you now is the border regions of a 0×0 element in the CSS model.

  17. TheToolman

    I saw this technique quite a long time ago on this 3D javascript demo:

    http://www.uselesspickles.com/triangles/demo.html

    The link above shows how triangles of arbitrary shape can be made from a group of these right angle triangles.

  18. cancel bubble

    There’s more info on this here (courtesy of Jon Rohan), http://www.dinnermint.org/css/creating-triangles-in-css/

    “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”

    Hedger Wang also recently posted this, http://hedgerwow.appspot.com/demo/arrows which looks like you no longer need a filter for transparency in IE.

  19. Cipa

    How about some css shadows?

  20. Cipa

    Do css shadows work on those triangles?

  21. Dan

    Wow, that’s neat

    Thanks for the great tip

  22. Matt Barnes

    For anyone who has to deal with IE6, this technique will work in that browser if you give the borders a color instead of having them be transparent.

  23. dougwig

    Nice! —the same idea could be extended to create a “V” shape ( for tooltips, etc.) by positioning a second smaller triangle on top if the first ( say a white triangle on top of a larger black triangle ).

  24. Michael

    You should add line-height: 0; font-size: 0; for IE, especially if you are supporting v6

  25. Jonathan

    I can’t believe no-one’s mentioned CSS Play yet – Stu’s done some crazy stuff with borders in the page, check out this flag! http://www.cssplay.co.uk/menu/flag

    If you’ve not been there before, check out his demos – they’ve been around for about 4+ years now I think but are still really fun to dissect with Firebug.

  26. Dutchie

    Yea, CSS FTW I say, f#ck JS

  27. Jean-Philippe HALIMI

    Still, there is a problem with this technique ; it seems there is no way to add borders to the triangle. Anyway, nice trick, indeed :o)

  28. Alejandro Briz

    Amazing !!
    ¡¡ Increíble tío !!

  29. Pedro Duarte

    /* IE6 */
    .chat-bubble-arrow {
    _border-left-color: pink;
    _border-bottom-color: pink;
    _border-right-color: pink;
    _filter: chroma(color=pink);
    }

  30. abidibo

    Also take a look at the control bar of this html5 player
    http://videojs.com/
    all html+css, same methods.. bye!

  31. Lachlann

    Great trick, has any one else tried creating an isosceles triangle by adjusting the border sizes? works fine in Firefox but looks very pixelated in chrome safari

    Any ideas?

    L

  32. Ben Atkin

    It says div.arrow-left twice in your example css.

  33. Juan Mendes

    This is cool, but not new. A co-worker of mine came up with this technique about 4 years ago, he even has a workaround for versions of IE that don’t support transparent borders. Check out his example of 3d rendering using triangles http://www.uselesspickles.com/triangles/ His blog also has a better visual explanation of how the transparent borders make the div become a triangle, this post keeps it a mistery.


Be Heard!

Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!

Name*:
Email*:
Website: