jQuery Rounded Corners with Rounded Borders with Set Width

Dave Methvin wrote a plugin for jQuery to easily round corners. Then Mike Alsup improved on the plugin by adding more effects. Mike’s solution for adding a rounded border is great but he doesn’t give good copy and paste code. I had to dig through the source on the page to figure out some usage. He also misses (or I cannot find) an important piece – some of us want to specify the dimensions of our rounded box with rounded borders. Before using this code, you need to make sure you have downloaded jQuery and jquery.corner.js and obviously include them on your page.

Here is a simple explanation and example with code:

div.rounded_box{
    background-color: #ccdaff; 
    color: #333; 
    padding: 10px;
    border:0;
    margin: 0;
    zoom:1;
}
/* border with rounded corners */
div.rounded_border {  background: #6666ff; padding: 8px; }

That is the CSS for the rounded box and rounded border to go around the box. The only attributes you should need to change are anything color related and the padding (pretty obvious what they do but you can mess with the values after you get this example working).

Now you have 2 options for your HTML/Javascript set up (note that I have namespaced my jQuery functions to $j instead of $). The first option is probably a bit faster while the second option avoids an extra wrapper div in your code, allowing you to specify the width inline. Here is the first option:

<div style="width: your width here;">
     <div id="my_rounded_box_id1" class="rounded_box">
          <p>Content</p>
     </div>
</div>
 
<div style="width: your width here;">
     <div id="my_rounded_box_id2" class="rounded_box">
          <p>Content</p>
     </div>
</div>
     $j(function(){
         $j('div.rounded_box').wrap('<div class="rounded_border"></div>'); //apply border
	 $j('div.rounded_box').corner("round 8px").parent().css('padding', '2px').corner("round 10px"); //round box and border
     });

Here is the second option:

<div id="my_rounded_box_id1" class="rounded_box" style="width: your width here;">
     <p>Content</p>
</div>
 
<div id="my_rounded_box_id2" class="rounded_box" style="width: your width here;">
     <p>Content</p>
</div>
function roundDiv(id){
	var tmp_width = $j('#'+id+'').css("width"); //get the width of target el
	$j('#'+id+'').css("width",""); //clear width value (or else corners screws up)
	$j('#'+id+'').wrap('<div class="rounded_border" style="width: ' +tmp_width+ ';"></div>'); //apply border
	$j('#'+id+'').corner("round 8px").parent().css('padding', '2px').corner("round 10px"); //round box and border
}
 
$j(function(){
 	//round each box separately so that we can specify inline dimensions
	roundDiv('my_rounded_box_id1');
	roundDiv('my_rounded_box_id2');
});

Enjoy!

UPDATE: After some more testing, the second option seems to be more cross browser compatible. I highly recommend using that option.

No TweetBacks yet. (Be the first to Tweet this post)
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • MySpace
  • Slashdot
  • StumbleUpon
  • Technorati
  • TwitThis

If you enjoyed this post, make sure you subscribe to my RSS feed!

This entry was posted in Software and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

25 Comments

  1. Posted June 18, 2009 at 10:41 am | Permalink

    Are you using this with Rails and it’s default Prototype based rjs?

  2. Posted June 18, 2009 at 11:26 am | Permalink

    I am using this in my Rails app, but you can use it anywhere. All you need is jQuery and jquery.corner.js (links are above). This does not use the Prototype library. Let me know how this works out for you. It is working out great for me so far.

  3. Rocky
    Posted September 7, 2009 at 5:27 am | Permalink

    Hi Toni,

    Thanks for the update. I want to have a corner with 1px border along with a bit curved corners, so I did something like this:


    $('#'+id+'').corner("round 20px").parent().css('padding', '1px').corner("round 20px"); //round box and border

    The bottom corners are coming properly, but the top corners are not coming in a proper way (Showing the background color also).

    Thanks,
    Rocky

  4. Posted September 7, 2009 at 6:55 am | Permalink

    @Rocky Can you post your HTML/CSS of the div you are rounding?

  5. Rocky
    Posted September 7, 2009 at 9:39 pm | Permalink

    Hi Tony,

    Following is my CSS:


    div.rounded_box{
    background-color: White;
    color: #333;
    padding: 10px;
    border:0;
    margin: 0;
    zoom:1;
    }
    /* border with rounded corners */
    div.rounded_border { background: #CCCCCC; padding: 8px; }

    #my_rounded_box_id1 p
    {margin:0; padding:0;}

    HTML:

    Content

    Thanks,
    Rocky

  6. Rocky
    Posted September 7, 2009 at 9:44 pm | Permalink

    Hi Tony,

    I’m not able to add my HTML code. I included my html codes inside but not working :-(

  7. Rocky
    Posted September 7, 2009 at 9:48 pm | Permalink

    I’m having a div with id=my_rounded_box_id1 class=rounded_box style=width: 400px; and a paragraph tag inside div having some content.

  8. Posted September 8, 2009 at 5:28 am | Permalink

    @Rocky No worries, just email it over. tonyamoyal:gmail:com

  9. Posted September 23, 2009 at 1:36 pm | Permalink

    I’m having the same problem as rocky. Bottom corners work fine, but the top corners don’t. Was a solution found?

    Thanks

    Kev

  10. Posted September 23, 2009 at 1:40 pm | Permalink

    Rocky never emailed me the code. Try to post your code here. You should be able to use the “code” tag or the “pre” tag with the “lang” attribute. If that doesn’t work, email it over and I’ll post a solution.

  11. Posted September 23, 2009 at 1:41 pm | Permalink

    Probably better to just give you the link…

    http://iamdesign.ukmicrosites.net/latest-work.php

  12. Posted September 23, 2009 at 1:42 pm | Permalink

    Thanks for the help btw :)

  13. Posted September 23, 2009 at 1:43 pm | Permalink

    I have a feeling it might be something to do with my css…

  14. Posted September 23, 2009 at 1:45 pm | Permalink

    No problem, I obviously want the code I post here to work so you are also helping me. I’ll try to get to this today/tonight.

  15. Posted September 23, 2009 at 1:48 pm | Permalink

    I appreciate the help a lot, thanks.

    To be honest, its a mixture of yours and a theme i purchased so don’t feel obligated. The reason i’ve tried yours is because I was having problems with the theme. I have tampered with it so much i’m surprised its still working… But nothing has changed. Bottom works, top doesnt… Weird…

    Thanks again.

  16. Posted September 23, 2009 at 1:57 pm | Permalink

    I might suggest as a first step, create a test page with a minimal amount of code to make sure that works. Then as a second step, include the theme’s CSS file and make sure that still works. If that works well, you can keep adding stuff in until you see it break. Or at that point you can put up the original page and subtract stuff until it works.

    Whether you work backwards or forwards is up to you, but I would definitely start by making sure the rounding works with the basic rounding code and the CSS files in your theme. This is exactly how I would debug it.

    Also, you may already know to do this but I am mentioning it for the sake of being complete. If the code is being generated by a CMS or lots of server side code, just copy the code from the view->source and work off of that.

    Let me know if you want me to take a look…but that is exactly what I will do.

  17. Posted September 23, 2009 at 2:07 pm | Permalink

    Yeah, it makes sense to do that. I’m applying it to other div’s to test and its working fine.

    I’m gonna do some debugging and see what happens.

    Many thanks Tony, i’ll let you know the solution… IF I find the solution.

    Thanks again

    Kev

  18. Posted September 23, 2009 at 2:16 pm | Permalink

    Just a quick note, if these are going to be a few static images, I highly recommend using roundpic. Obviously you don’t want to use this if you will need to round images dynamically (or lots of them).

  19. Posted October 20, 2009 at 12:39 pm | Permalink

    I was same problem as Rocky. Bottom corner works fine, but top don’t. I solved this problem using “.corner(’10px’)” or “.corner(’15px’)” works too. When I was using 20px, didn’t work.

  20. kamini
    Posted October 24, 2009 at 4:11 am | Permalink

    Hello

    it’s working fine in mozila as well as in ie. but it creates problem in ie when background of the page is image. corner will be display as white color….

    any solution for this

    it’s urgent help me…

  21. Posted October 24, 2009 at 2:12 pm | Permalink

    @Frederico – Yea the solution is a bit hackish and can screw up occasionally. It’s not too difficult to mess with it and get it to work though. Glad you figured it out.

    @kamini – That may be a limitation. Email me your code and background image and I’ll mess around with it for a few minutes.

  22. Posted November 1, 2009 at 4:54 pm | Permalink

    I was trying to create the rounded corners from your code on the page but can’t seem to produce it on a test page? I’ve got a page here: http://robquigley.me/rounded-corner/index.html and I’m not sure what I’m doing wrong. Can you help?

  23. Posted November 2, 2009 at 8:39 am | Permalink

    @Rob, doesn’t look like you are namespacing jQuery to use $j. If that is the case, then I think you should just use ‘$’ in place of ‘$j’. Make sense?

  24. Murad
    Posted January 12, 2010 at 8:37 am | Permalink

    @ Kamini, did you have a fix for your problem? I am having the same issue. I would really appreciate if you share. Thank you in advanace.

    “… when background of the page is image. corner will be display as white color.”

  25. Posted February 9, 2010 at 10:04 am | Permalink

    Rob Q, dude, you have it all wrong. You have JS links as CSS links, you crazy dog!

    Look at your code, there first .js files are linked inside a CSS declaration, which will never work because you are loading your javascripts as CSS.

    Then there is the issue Tony mentioned. You have “$j” everywhere. Just make sure to remove the “j” from that and you will be fine.

    ;-) glad to help!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">