Fun With CSS Shapes

Fun With CSS Shapes

Apr 2nd in Screencasts by Jeffrey Way

I bet a bunch of you don't realize that we can create the illusion of shapes by using 100% CSS. By utilizing the border property in creative ways, we can make some really interesting polygons. In this week's screencast, we'll figure out a way to create speech-bubbles without resorting to background images.

PG

Author: Jeffrey Way

Hi, I'm Jeff. I'm the editor of Nettuts+, and the Site Manager of Theme Forest. I spend too much time in front of the computer and find myself telling my fiance', "We'll go in 5 minutes!" far too often. I just can't go out to dinner while I'm still producing FireBug errors...drives me crazy. During my free time, I sporadically write articles for my own personal blog. If it will keep you in the good graces of the church, follow us on Twitter.

Most of us, by now, are familiar with using the "-moz-border-radius" and "-webkit-border-radius" properties to create rounded corners - or even complete circles - but there are other techniques to create more complicated shapes.

Traditional Speech-Bubble

css shapes
blockquote#one {
 width: 250px;
background: #e3e3e3;
padding: 25px;
position: relative;
}

#one .arrow {
 width: 0; 
 height: 0;
 line-height: 0;
 border-top: 30px solid #e3e3e3;
 border-left: 60px solid white;
 border-right: 10px solid white;
 position: absolute;
 bottom: -30px;
 right: 30px;
}

Note how the "bottom" property is the reciprocal of the "border-top" property.

Alternate

css shapes
blockquote#two {
 width: 250px;
background: #e3e3e3;
padding: 25px;
position: relative;
}

blockquote#two .arrow {
 width: 0;
height: 0;
line-height: 0;
border-top: 40px solid #e3e3e3;
border-left: none;
border-right: 30px solid white;
position: absolute;
bottom: 60%;
right: -30px;
}

Yet Another

css shapes
blockquote#three {
 width: 450px;
 background: #e3e3e3;
 padding: 25px;
 position: relative;
}

#three .arrow {
 width: 0;
 height: 0;
 line-height: 0;
 border-bottom: 25px solid #e3e3e3;
 border-right: 50px solid white;
 position: absolute;
 top: -24px;
 left: 20px;
}
css shapes

Interesting Note

I'm typing this paragraph minutes before posting the tutorial. I learned something tonight. Did you know that, if you want your document to validate, you can't place text directly into a blockquote? It needs to be nested within a parent element - like the <p> tag. Additionally, the validator didn't like the fact that I used a span tag within the blockquote. It allows for a plethora of elements, but unfortunately not the span! Oh well; no big deal. Just replace it with a different element. It hardly makes a difference.

Span tag within a blockquote

Pretty Nifty

I'm really eager to see how you guys have implemented this unique trick into your own projects. Send us links to your examples, via the comments. Thanks for watching. Next week, plan on learning something much more advanced. ...I just need to figure out what! Until then.


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    Will April 2nd

    hmm, a lot easier than the way i was doing it! Thanks a lot, Jeffrey. This is a simple, but well-written tutorial that even the best of us can benefit from.

    ( Reply )
  2. PG

    Roosevelt P. April 2nd

    Super! Very useful technique bro :) . I actually skipped around and got into the good stuff XD.

    Thanks.

    ( Reply )
  3. PG

    Jeffrey Way April 2nd

    Hey everyone. Sorry for the “bottombottom” and “rightright” issues in the source code. It’s an issue with the plugin. I can’t change that.

    ( Reply )
    1. PG

      lawrence77 April 2nd

      Your New Avatar is Good!
      I love ur screencast series, caz its more clear than these Texts…..

      I’m a newbie to CSS too, so its helpful
      thanks JW

      ( Reply )
    2. PG

      Sirwan April 3rd

      i like your new avatar

      ( Reply )
      1. PG

        rrr April 5th

        хы

  4. PG

    Rene April 2nd

    Hey great tutorial, this surely comes in handy and saves a lot of time!

    ( Reply )
  5. PG

    Vasili April 2nd

    Woah, you finally use your Mac! Once you showed the #trap example, I knew exactly what to use it with, comments. A tornado warning, and you’re making a screencast? You crazy man. :P

    Just wondering, is that the built in mic or one you bought and hooked up?

    ( Reply )
  6. PG

    Jeffrey Way April 2nd

    @Vasili – It’s a Snowball condenser.

    ( Reply )
  7. PG

    Nouman Saleem April 2nd

    Hey Jeff,

    is the demo linked correctly? I haven’t seen the video, so this may be premature, but it is supposed to be a flickr gallery?

    thanks,
    Nouman

    ( Reply )
    1. PG

      Jeffrey Way April 2nd

      Whoops. Thought I fixed that! The site has been updated.

      ( Reply )
      1. PG

        Nouman Saleem April 2nd

        thank a lot Jeff, looks sweet :)

  8. PG

    Mike T. April 2nd

    Hmm….these techniques look a little familiar, *cough* *cough*:

    http://www.filamentgroup.com/lab/image_free_css_tooltip_pointers_a_use_for_polygonal_css/

    :)

    ( Reply )
    1. PG

      Jarod April 3rd

      Well, it’s a good thing Jeffrey created a tutorial on it, cause I don’t subscribe to filamentgroup.com and would have not seen this technique, huh?

      ;)

      ( Reply )
    2. PG

      Jeffrey Way April 3rd

      I never stated that this technique was my discovery. There are many sites that teach this method.

      ( Reply )
      1. PG

        ec April 4th

        of course… another great one:

        CSS Play

        by Stu Nichols

        Then there’s always Eric Meyers, whom I think you once interviewed or something on one of the TUTS sites, if not this one!

      2. PG

        ec April 4th

        woops

        Here’s the link

        CSS Play

        by Stu Nichols

  9. PG

    dev.My April 2nd

    Really good tutorial, love this

    ( Reply )
  10. PG

    Huy Dũng April 2nd

    Interesting.

    I’ve just finished a html template yesterday (http://ftk.vn/templatenew/index.html). The content area and sidebar use this technique heavily.

    Personally, I thought this technique is just for fun and not useful in practical, until I got the requirement that the design of ftk.vn must identical to fastrackids.com, and found that this technique has a room for it.

    And today you’ve showed me an unique way to use it extensively. Thanks for the tut.

    ( Reply )
  11. PG

    Meshach April 2nd

    Jeffrey, you’ve done it again!!! WOOHOO! :D

    I love the new avatar BTW.

    ( Reply )
  12. PG

    insic April 2nd

    nice tut Jeff.

    ( Reply )
    1. PG

      sammu April 29th

      yea nice

      ( Reply )
  13. PG

    Jeremy Worboys April 2nd

    Thanks, this gave me a cool idea for a jquery drop-down nav.

    I think I’ll have a play and post the results if I get something working nicely.

    ( Reply )
  14. PG

    Hector April 2nd

    Damn! How I love your tutorials! :S
    I’m from Argentina, and I always visit you.
    I learn so much!!
    Thanks!!

    ( Reply )
  15. PG

    Rahul Chowdhury April 2nd

    Awesome Tutorial, really helpful. I will give it a go!

    ( Reply )
  16. PG

    Lamin Barrow April 2nd

    I have thought of writing a tutorial like this a long time ago.. but hey good to see stuff like this. :D

    ( Reply )
  17. PG

    Jordy April 2nd

    Thanks

    ( Reply )
  18. PG

    chris April 2nd

    Nice little tut! Thx

    ( Reply )
  19. PG

    crysfel April 3rd

    i didn’t know this! thanks dude!

    ( Reply )
  20. PG

    Shaun Butler April 3rd

    Man, Thats smokin!

    ( Reply )
  21. PG

    Vincent D'Amico April 3rd

    clever

    ( Reply )
  22. PG

    Teddy Zetterlund April 3rd

    This is how I did it about a month ago when I played around with it for a administration theme: http://gist.github.com/74865

    As you can see I didn’t perfect the technique for IE6 (since IE6 wouldn’t be used by the administrators I didn’t matter). But now, thanks to you, I don’t have to figure out it out by myself since I noticed your “line-height” usage above.

    Nice one.

    ( Reply )
  23. PG

    Shane April 3rd

    Good stuff. Thanks for posting.

    ( Reply )
  24. PG

    Snorri - Css April 3rd

    hum…. interesting

    ( Reply )
  25. PG

    Jeff Hui April 3rd

    Very nice tutorial :)

    I was just wondering, is there any problems for setting those white border colors to transparent?

    As a side note, span isn’t a valid self-closing tag. It might mess up some browser’s DOM trees.

    http://stackoverflow.com/questions/97522/what-are-all-the-valid-self-closing-tags-in-xhtml-as-implemented-by-the-major-b

    ( Reply )
    1. PG

      Jeffrey Way April 3rd

      Yeah – I wrote about that at the end of the article. Interesting tip.

      ( Reply )
  26. PG

    Live Green April 3rd

    I have been looking for an easy way around this, and this is it. Thanks for the very useful post and I hope they keep coming still!

    ( Reply )
  27. PG

    Little April 3rd

    nice job.

    ( Reply )
  28. PG

    Teddy Zetterlund April 3rd

    Jeff Hui: Only in Internet Exporer 6. I went with transparent borders in the solution I mentioned a few posts ago. Then just a fix for IE6 if you want it.

    ( Reply )
  29. PG

    bruno April 3rd

    Great tutorial.
    I would like to see more css tutorials.

    ( Reply )
  30. PG

    Patrick April 3rd

    Hey Jeff!
    Great tut as usual.
    transparent border color works well with texture background:
    http://patrickburke.de/css_shapes
    Just wanted to mention that ;)

    ( Reply )
  31. PG

    rap April 3rd

    very nice! it can be used..
    <– here :)

    thanks for the tut

    ( Reply )
  32. PG

    Martyn April 3rd

    Never realised that was possible, I always used images and set it up just like your site. Great tutorial though.

    ( Reply )
  33. PG

    cris April 3rd

    Am I the only one who can’t see what he is doing because the colors are too light in the video? You are pointing out changes you are making, and I am seeing nothing. If you had used a darker color for the bubble, or whatever you are doing, it would help a bunch.

    ( Reply )
  34. PG

    Jason April 3rd

    Nice tutorial Jeff! You can also add border-radius to round off those corners and make them look like real speech “bubbles”. Doesn’t work in IE, but…

    blockquote#two {
    border:none;
    -moz-border-radius:8px;
    -webkit-border-radius:8px;
    -khtml-border-radius:8px;
    border-radius:8px;
    }

    ( Reply )
    1. PG

      Marc April 3rd

      Yeah, you can if you want a CSS file that doesn’t validate.

      ( Reply )
  35. PG

    Chris Raymond April 3rd

    This is great but I still can’t get my head around how these proiperties lead to an angled edge of the arrow…can anyone enlighten me?

    ( Reply )
    1. PG

      James April 3rd

      Hi Chris, this technique is actually quite old – it was coined “border slants” or “border art” a long time ago (possibly by Eric Meyer): The basic concept exploits the fact that at the point where two css borders meet each other, there is diagonal line (you’ll obviously need to use two different coloured borders to see that diagonal line). If the borders are of equal width, that diagonal line will be at 45 degrees. Different border widths will give different angles… Full explanation: http://www.infimum.dk/HTML/slantinfo.html

      Mix that together with some positional black-magic and you can create entire shapes: See Tantek’s page here – http://www.tantek.com/CSS/Examples/polygons.html – no images anywhere there!

      ( Reply )
  36. PG

    Randy April 3rd

    I played around a bit with all these and did you happen to notice when you use the various border styles, really cool things happen!

    For instance, changing the first bubble to this:

    border-top: 30px dotted #e3e3e3; /* added dotted */
    border-left: 60px solid white; /* changed nothing */
    border-right: 0px solid white; /* or remove this */

    Creates a perfect rounded effect on the right of the arrow! (at least in firefox)

    Thanks so much for this tut Jeff! Though, now I’ll be playing around with this all day and get nothing done!

    ( Reply )
  37. PG

    James April 3rd

    Using a similar logic you can also create giant CSS coloured circles – even in IE, just by using an enormously wide dotted border. I’ve still never found a practical use for that one though!

    ( Reply )
  38. PG

    Michelle April 3rd

    This was a great tutorial back in December when SitePoint did it: http://www.sitepoint.com/article/css-angles-the-edge-you-need/

    But, still holds up well.

    ( Reply )
  39. PG

    Stephen April 3rd

    I keep thinking how cool it would be to animate these things. With jQuery (or jQuery UI?), it would be fairly simple to animate the border widths, which would then change the angle of the arrow. With a bit of trig, you could probably figure out how to always have the arrow pointed at the middle of your element, or at the mouse pointer…

    ( Reply )
  40. PG

    James April 3rd

    Damn CSS! Why do you still teach me new tricks?!

    ( Reply )
  41. PG

    Diego SA April 3rd

    Cool! I’ll use it in future wordpress layouts!

    ( Reply )
  42. PG

    |Walter| April 3rd

    btw source code is the same as demo!
    plz try to pack it ;) for better download ;)
    |W|

    ( Reply )
  43. PG

    |Walter| April 3rd

    http://net.tutsplus.com/videos/screencasts/slice-and-dice-that-psd/

    my first seen tut and after seen that i love whole net tuts site :)
    btw. Jeffrey you ROCKS and the Jeffrey/Joefrey is really cool tut and nice one for me as beginer in pds to css (XHTML) design.

    |W|

    ( Reply )
  44. PG

    CarlosB April 3rd

    Nice turorial!

    ( Reply )
  45. PG

    Bryan Kwon April 3rd

    never thought about this, it is amazing!! :o

    ( Reply )
  46. PG

    Chesham April 4th

    Wow, totally awesome. I’ll have to start using this.

    ( Reply )
  47. PG

    Nykeri April 4th

    and we see why i luv css and this site Jeffery my friend ur a boss

    ( Reply )
  48. PG

    Adrian 7 April 5th

    That’s Cool Man!

    ( Reply )
  49. PG

    TheWitcher April 5th

    Nice tut Jeff, ya’ve done great job (again!).
    But one little thing I want to correct: In the Video you said, this technique is not gonna work in front of some kind of textured backgrounds; but that’s not true I think. You just need to give one border the color of the box at which the arrows is sticking to (just as you did it in the tut) and the other one ist not explicit the same color as the background but “transparent” (the span or whatever which is styled as the arrow then has to have background-color:none or transparent.
    To me that works fine with textured backgrounds.

    ( Reply )
  50. PG

    Saro April 5th

    Exactly what i needed!

    ( Reply )
  51. PG

    Anton Agestam April 5th

    This is just unsemantic, ugly code! How can you recommend people to use this? THIS IS NOT GOOD CODING!

    ( Reply )
    1. PG

      Jeffrey Way April 5th

      Care to elaborate? Also, be sure to let me know how using a class of “textcenter” on your own website is a smart idea.

      Thanks!

      ( Reply )
      1. PG

        Anton Agestam April 6th

        What I mean is that it to me seams very “unsemantic” to use an element just to the purpose of styling. Wouldn’t that look pretty nasty, or at least not “as it should look like” if you turn off css style sheets in the web developer tool bar? I’ve always been tought to code semantic and to use the elements to what they are meant to be used to.

        The reason I use a textcenter class on page is simply that I often have more than one element in my pages that have centred text. If I can define one less ID, I will keep my css file smaller, will I not?

      2. PG

        Anton Agestam April 6th

        You explain to me how not using and index file on your website is a smart idea! ;)

  52. PG

    (zoe) April 5th

    nice

    ( Reply )
  53. PG

    Brian April 5th

    Great post.
    This looks very simple and easy.
    Thanks.

    ( Reply )
  54. PG

    Gaurav M April 5th

    this is awesome man great!! stuff
    the Homer was also nice …
    http://www.romancortes.com/blog/homer-css/

    ( Reply )
  55. PG

    Poonam April 6th

    Hey very nice………Got new thing to learn :) cheers !

    ( Reply )
  56. PG

    Adrian Rodriguez April 6th

    Every time I am working on #three .arrow the arrow is not showing up. When I look at it in firefox firebug isn’t showing me border-bottom: 25px solid #e3e3e3;
    and only shows the border-right style. The weird thing is that I have the style set in my css, but it’s not showing up. Know why this is happening?

    ( Reply )
    1. PG

      Adrian Rodriguez April 6th

      My bad, found one little colon that looked like a semicolon, lol.

      ( Reply )
  57. PG

    John April 6th

    very good stuff, i’ve been looking for a useful way of using this technique for a while… the best I could come up with was my CSS Heart

    ( Reply )
  58. PG

    AlfredN April 7th

    I have a hard time with these myself, this tutorial will prove useful in the future.

    ( Reply )
  59. PG

    romain April 9th

    <= Why don’t use it for your own comments ??? :p

    ( Reply )
  60. PG

    Abdullah alhourani April 9th

    Thanks

    ( Reply )
  61. So thats how you do it!!! Been looking for something like that for a very long time.

    ( Reply )
  62. PG

    Davor April 9th

    Excellent tutorial! It’s going to be very useful for my current project. Thank you ;)

    ( Reply )
  63. PG

    omprakash April 10th

    no need for go for an image

    ( Reply )
  64. PG

    dan April 10th

    a very useful tutorial thanks should come in handy

    ( Reply )
  65. PG

    Mikael Carrara April 17th

    Thanks ;D

    ( Reply )
  66. PG

    Erik Groda April 19th

    A very useful tutorial, thankyou very much!

    ( Reply )
  67. PG

    csseyah April 21st

    very nice bro.. i learn a lot to your tutorial. God Bless You bro

    ( Reply )
  68. PG

    Terry April 22nd

    You can position the shape in the corner of the container element and make it look like the box is folded on the corner.

    ( Reply )
  69. PG

    Uğur Özyılmazel April 24th

    woow… realy good

    ( Reply )
  70. PG

    jake walker April 24th

    excellant tut

    ( Reply )
  71. PG

    Dean Craig April 25th

    Very interested in the last part, about the tags on the validator, never knew that.

    Brilliant tut as usual!

    ( Reply )
  72. PG

    Bala April 27th

    Looks Very Cool!!!

    ( Reply )
  73. PG

    Radoslav April 28th

    Strange but very cool!

    ( Reply )
  74. Some excellent CSS resouce info here. Keep up the good work.

    ( Reply )
  75. PG

    EDimNETZ May 6th

    Thanks for this great tutorial!
    Very helpful to a noob like me.

    Two questions though:
    So which tag to take instead of to make the CSS validate?
    And how do make it work in IE6?

    ( Reply )
  76. PG

    EDimNETZ May 6th

    Hm … apparantly I can’t write this “<” here.

    I meant:
    Which tag to use instead of “span” to make it validate?

    ( Reply )
  77. PG

    Crazyguy Reegan June 27th

    Simply amazing.

    ( Reply )
  78. PG

    Ali Naqvi July 25th

    hello there !! i just dicovered this site and i found it very helpful, will be coming back to it in the future

    ( Reply )
  79. PG

    mojitopl August 1st

    Jeff – You just make thigs eeeeeeasy. I saw sam tutorial for this arrow stuff, but people get too comlicated. You’re The Shit! ;) thanks

    ( Reply )
  80. PG

    adrusi August 19th

    why use a color for the invisible borders? _px solid transparent works well, so does none, and 0;

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    August 19th