Fun With CSS Shapes
videos

Fun With CSS Shapes

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.

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.


Tags: CSSVideos
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://axiologicaldesign.com Will

    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.

  • http://www.mutantweb.com/ Roosevelt P.

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

    Thanks.

  • http://www.jeff-way.com Jeffrey Way
    Author

    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.

    • http://laranzjoe.blogspot.com lawrence77

      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

    • http://www.eraxa.com Sirwan

      i like your new avatar

      • rrr

        хы

  • http://scyberspace.com Rene

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

  • http://another-perfect-world.org/ Vasili

    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?

  • http://www.jeff-way.com Jeffrey Way
    Author

    @Vasili – It’s a Snowball condenser.

  • Nouman Saleem

    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

    • http://www.jeff-way.com Jeffrey Way
      Author

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

      • Nouman Saleem

        thank a lot Jeff, looks sweet :)

  • Mike T.

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

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

    :)

    • http://jarodtaylor.com Jarod

      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?

      ;)

    • http://www.jeff-way.com Jeffrey Way
      Author

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

      • ec

        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!

      • ec

        woops

        Here’s the link

        CSS Play

        by Stu Nichols

  • http://www.dev.my dev.My

    Really good tutorial, love this

  • http://nguyenhuydung.com Huy Dũng

    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.

  • http://zencarttuts.com Meshach

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

    I love the new avatar BTW.

  • http://blog.insicdesigns.com insic

    nice tut Jeff.

    • sammu

      yea nice

  • Pingback: Bookmarks for April 3rd, 2009 | vitali software

  • Pingback: popurls.com // popular today

  • http://www.complexcompulsions.com Jeremy Worboys

    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.

  • Hector

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

  • http://rcthegreatblog.com Rahul Chowdhury

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

  • http://laminbarrow.com Lamin Barrow

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

  • Jordy

    Thanks

  • http://www.chezchris.de chris

    Nice little tut! Thx

  • http://www.quizzpot.com crysfel

    i didn’t know this! thanks dude!

  • http://shaunbutler.co.uk Shaun Butler

    Man, Thats smokin!

  • http://www.endyoursearch.com Vincent D’Amico

    clever

  • http://twitter.com/teddy/ Teddy Zetterlund

    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.

  • http://www.freshclickmedia.com Shane

    Good stuff. Thanks for posting.

  • Snorri – Css

    hum…. interesting

  • http://jeffhui.net Jeff Hui

    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

    • http://www.jeff-way.com Jeffrey Way
      Author

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

  • http://www.livegreentoday.co.uk/ Live Green

    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!

  • http://yeqing.woxihuanni.com Little

    nice job.

  • http://twitter.com/teddy/ Teddy Zetterlund

    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.

  • bruno

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

  • http://patrickburke.de Patrick

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

  • rap

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

    thanks for the tut

  • Pingback: cssのみでblockquoteを「吹き出し風」にする方法 | Designeo

  • http://www.crearedesign.co.uk Martyn

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

  • cris

    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.

  • http://www.vagrantradio.com Jason

    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;
    }

    • Marc

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

  • http://www.designtank.ws Chris Raymond

    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?

    • http://www.jamessmith.co.uk James

      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!

  • http://www.prop-14.com Randy

    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!

  • http://www.jamessmith.co.uk James

    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!

  • Michelle

    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.

  • http://blogs.oc.edu/ee/?/sbell Stephen

    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…

  • James

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

  • http://www.dsaportfolio.com.br/ Diego SA

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

  • Pingback: Вопросы и ответы по Wordpress - выпуск 2 - Продвижение сайтов, веб-дизайн и креатив

  • Pingback: links for 2009-04-03 « Minesa IT

  • |Walter|

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

  • |Walter|

    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|

  • http://www.pixelelement.com/ CarlosB

    Nice turorial!

  • http://bryankwon.net Bryan Kwon

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