Yesterday at 5:15 PM, I was jones-ing for my quick email fix after being away from the computer for almost 48 hours so I fired up Thunderbird and instantly grabbed a few RSS feeds I subscribe to. (On the weekends I’m swamped with home renovation projects and all my free time is spent with friends and family… AWAY from the computer)
My new “buddy” Dustin Diaz (who included a reference to me and this site in his most recent audioblog) posted an edgy “take-it-or-leave-it” kind of post called “Enough With Rounded Corners”.
So I checked out this new resource for rounded corners and saw that the html would be perfect for an impromptu tutorial on jQuery - combining the wrap(), prepend(), and append() functions.
Here’s the html markup that we’re going to use as our starting point:
<div class="dialog"> <div class="hd"> <div class="c"></div> </div> <div class="bd"> <div class="c"> <div class="s"> <-- main content goes here --> </div> </div> </div> <div class="ft"> <div class="c"></div> </div> </div>
So, how would we take this html, and use jQuery to clean up the code?
Well, first we need a “hook” - a unique html element, or an id, or a class name - to tell jQuery to target.
The solution I’m gunning for will remove all of the divs you see above. Therefore, I think a great “hook” would be a simple div with either a unique id or class name.
Let’s try this:
<div class="roundbox"> <-- main content goes here --> </div>
Next step… we use jQuery to add in our html code:
$(document).ready(function(){ $("div.roundbox") .wrap('<div
class="dialog">'+
'<div class="bd">'+
'<div class="c">'+
'<div class="s">'+
'</div>'+
'</div>'+
'</div>'+
'</div>');
});
Look closely at the code above and you’ll see that all of the divs fit nicely inside of each other and also wrap nicely around the content that will go inside our rounded box we’ll soon create.
You should also notice that I’ve left out quite a bit of the markup from the original. That’s because the wrap() function in jQuery needs perfectly nested divs in order to work.
Specifically, I’ve left out these two parts:
<div class="hd"><div class="c"></div></div>
<div class="ft"><div class="c"></div></div>
If you notice, the parts that I left out go just inside, but at the beginning and at the end of the class=”dialog” div. This is a perfect opportunity to use append and prepend functions of jQuery and chain them together.
$('div.dialog').prepend('<div class="hd">'+
'<div class="c"></div>'+
'</div>')
.append('<div class="ft">'+
'<div class="c"></div>'+
'</div>');
I’ve put a Rounded Corners with jQuery example online for you to see. Be sure to view the source code because I want you to see how clean the html looks when all the javascript and CSS is packaged off into separate files.
The code comes from an article on Schillmania and that’s where I would recommend grabbing the zip file with the image slices that make the rounded corners so pretty.
There are many other ways to create rounded corners - some involving no images whatsoever.
On the jQuery site there’s a plugin for making rounded corners without images. It’s not for everyone (or every application) but worth checking out. Don’t ask me to break down the code because it’s pretty intense and chok full of trig functions that I am a little rusty on.
But look at how nice the code looks (using the plugin):
$(document).bind("load", function(){
$("#box1").corner();
});
[tags]rounded corners, javascript, jQuery, DOM, CSS, Dustin Diaz, Schillmania[/tags]
56 Responses
Dustin Diaz
June 5th, 2006 at 1:02 pm
1Sweet! Good stuff Jack. I am most definitely comfortable with JavaScript doing this for me as only a very little percentage of my users are actually going to be seeing the fallback.
Jack
June 5th, 2006 at 1:14 pm
2Dustin,
Great to see you back! How’s that for a quick turnaround…? I see your post yesterday and in 24 hours I’ve put a jQuery spin on it.
For anyone reading this that hasn’t seen Dustin’s site, I suggest checking it out. Great stuff and very entertaining podcast (think web topics over a beer or two).
As an aside, everyone look at that last bit of code I posted for an alternative to the $(document).ready function. In other words, if you want to kick off your javascript when the page loads, you can use the $(document).bind(’load’, function(){}); routine.
Jack
June 5th, 2006 at 1:28 pm
3And yes, I completely agree with the comment you make, Dustin, about a low percentage of folks having js turned off.
Florian
June 5th, 2006 at 3:40 pm
4I use similar code on 120 div (For pictures…) but its doesn’t apply at all !
How can fix it ?
$(document).ready or $(document).bind(’load’, function(){});
same result…
Jack
June 5th, 2006 at 3:51 pm
5“Similar code” could mean “missing something really important that was in the original”.
Do you have a link to the site? I’ll try to take a look if you can give me more info but so far you haven’t told me enough.
Rizky
June 7th, 2006 at 6:53 pm
6funny, on fist load the css and images didn’t show up at all. just like an unstyled document. but after i refresh the page, there it is, the rounded corners.
is that a bug? or is it just me? thanks…
Rizky
June 7th, 2006 at 6:54 pm
7sorry. I was talking about the demo page
cheers…
Jack
June 7th, 2006 at 6:58 pm
8It could have been the server. If you want to test, you could clear your cache and history and hit the page again.
Antonio
June 9th, 2006 at 3:56 am
9Very nice. I’m all for using images for this, as you can see in my site
I’m gonna save this for the next version of my template. Thanks a lot!
Jack
June 9th, 2006 at 10:31 am
10@Antonio
You’re welcome.
Sorin Ionescu
June 15th, 2006 at 2:18 pm
11I believe the plugin I made is a lot better. You can have any border and corner you want. And it’s a lot simpler to use.
“Custom Borders Plugin”
http://jquery.com/discuss/2006-June/
Jack
June 15th, 2006 at 3:23 pm
12Nice plugin. Thank you for sharing that link, Sorin.
Fotis Evangelou
July 14th, 2006 at 10:30 pm
13A bit off topic!
The append/prepend code ability of JQuery has made think if it is possible to use it on specific tags (either beginning o rending) to “attach” specific code.
For example, a lot of add-on scripts, like sIFR, require a tag to be copied right before the closing .
Would it be possible to use JQuery to automatically prepend the ) and that tag only?
Jack
July 14th, 2006 at 10:51 pm
14Can you provide an example? I understand the basic concept of what you’re asking but I think you’ve left out some details I need to give you a complete answer.
Fotis Evangelou
July 16th, 2006 at 8:03 am
15Jack, what I would like to do is “auto” prepend a code snippet before the closing tag . The auto prepend code would exist in a seperate js file linked to the template.
sIFR, Google Analytics and many other scripts need to have a code snippet applied as well before the closing tag in order to work. It is mainly a script tag like:
What I want that for? I am preparing an extension for Joomla (which I hope to incorporate in your CMS as well when it comes out if it’s possible
) and would like the user to simply install the extension and have it run immediately without copy pasting any additional code in their joomla template. I like the keep-it-simple rule for such extensions and this is why I want to try JQuery.
Using the Joomla API I can “make” the extension auto include all necessery links to files used, as well as the JQuery library and an external js file that would include the “auto prepend” code that I’m talking about.
I hope you understand now what I want to do… Thanks man!
Jack
July 16th, 2006 at 4:02 pm
16The closing body tag???? That’s what I’m missing. You haven’t said which closing tag.
I don’t think you can auto prepend to the body tag… but I’m just going off of one test I did weeks ago when the same thought ocurred to me.
Fotis Evangelou
July 16th, 2006 at 7:43 pm
17Yes, now I noticed that the commenting system ate up the tag. It is the tag. The closing body tag!
Fotis Evangelou
July 16th, 2006 at 7:45 pm
18It ate it up again!!!
instead of I will use *.
So the tag is */body*.
Fotis Evangelou
July 16th, 2006 at 7:45 pm
19Sorry, I see now that if the system detects an opening/closing html “arrow” it ommits it.
Bryan
October 25th, 2006 at 12:52 pm
20I’m loving these tutorials, they’re definitely helping me get a grip on jquery.
I’m curious though… When i do view the source, none of the code that you have wrapped or appended, etc actually shows up in the body code? I also noticed this on the drop-shadows one…
Also, and this may be a silly question… in the wrap function…
I’m assuming jquery is smart enough to know where to properly place the candidate for wrapping within the “‘+”+…” so it sits in the middle of all the tags… am i correct?
thanks again for the tutorials!
Jack
October 25th, 2006 at 9:10 pm
21It’s in corners.js. View the source and look before the closing head tag.
And yes, it knows to nest within the tags correctly.
Glad you enjoy the tutorials.
Sly
February 4th, 2007 at 9:03 am
22Don’t understand how this things can be applied in joomla.
I have copied the jquery and jq-corner javascript files in template root, but i don’t know what is the next step to make modules appear as rounded…
If someone can explain…
Thanks.
Jack
February 4th, 2007 at 12:59 pm
23I’m not familiar with Joomla. So the best I can recommend is to find your header template, put a correctly formatted link to your jquery javascript file, and proceed as normal.
You can use the Firefox Developer plugin to verify if your javascript file is being accessed correctly.
Colin "Vindexus" Kierans
April 18th, 2007 at 10:39 am
24Awesome stuff, I’m definitely going to be using this.
Thanks!
Henry Jin
April 25th, 2007 at 4:30 pm
25Amazingly clean final codes. Thanks, Jack. I will implement into my project.
BTW, for the jQuery plugin for rounded corners without images, I believe the author draws every rounded corner with many 1px-height divs line by line, whose width is defined by a math-function (sin, cos, sqrt). Just like a scientific x-y plotter.
Nick Horne
February 28th, 2008 at 3:08 pm
26OK, I do like this code and it is very simple and easy to use. However I am having problems trying to float elements within the roundbox div class and have tried and tried to get this to work. If anybody has any ideas or has also experienced this problem can you please offer me any advice. Have already tried the obvious.
Antoine BERNIER
April 12th, 2008 at 9:33 pm
27I’ve published a jquery plugin based on that technique: http://dev.pnumb.com/cornflex#jQuerycornflexplugin
All you need is design your box into photoshop and load the plugin with your custom values on your elements.
Refer to the wiki page for more details.
Hope it helps
Cheers,
Antoine
Newcastle
June 19th, 2008 at 4:34 am
28This is cool but has anyone seen the sIFR version for rounded corners?
numericalexample.com
August 20th, 2008 at 8:24 am
29I have created a ToHeader plugin for joomla 1.5.
You can use this extension for including jQuery and other javascript from the joomla articles.
The url with the description and the download link is
http://www.factsandpeople.com/facts-mainmenu-5/18-php/83-using-jquery-in-joomla-articles
The plugin only works with PHP 5 and later.
ebuyc
September 4th, 2008 at 2:54 pm
30Ok I needed something similar but wanted to set specific widths….
$(”div.fancy_box”).each(function(){
var ems = $(this).outerWidth() / parseInt($(this).css(’font-size’));
$(this).css(’width’, ‘auto’);//ie6
$(this).wrap(”");
});
$(’div.dialog’).prepend(”").append(”");
Quintin
September 30th, 2008 at 7:52 am
31Is it possible to ‘prepend’ just the opening tag of a div and then later ‘append’ the closing tag of that div, or does it have to be perfectly nested tags?
Thanks
Richard
November 26th, 2008 at 8:11 am
32It is, you need to use wrapInner instead. Like this:
$(’#element’).wrapInner(”);
This should place a new div around the content but inside the #element div.
Baz L
December 22nd, 2008 at 6:02 pm
33Funny, I wrote this same tutorial last night: http://www.webdevelopment2.com/rounded-corners-jquery-css/ and heavily borrowed from this code (got off of jQuery site).
Didn’t know this is where it lives….
I don’t quite understand the markup though, but they’re tons of ways to round corners I suppose.
Browser Dilemas | Flyingcows Blog | Digital Cattle Farms
January 4th, 2009 at 2:16 am
34[...] http://15daysofjquery.com/wrap-it-up-pretty-corners/13/ [...]
yfur
January 23rd, 2009 at 9:01 am
35erw
David
January 28th, 2009 at 5:24 pm
36Please could someone explain how I can implement Jquery in Joomla (Like from downloading jquery, to where to put it, what other files are needed, and how to link it all up)…’cos it’s not working for me, and there aint any info like that on the net!
40?????jQuery???? - Code Index
February 23rd, 2009 at 4:06 am
37[...] Tutorial Link: Click Here [...]
40+ Excellent jQuery Tutorials | instantShift
March 4th, 2009 at 12:41 pm
38[...] Tutorial Link Demo Link [...]
bolo
July 15th, 2009 at 8:17 pm
39Is it free?Where can I get the dounload link?
pligg.com
July 18th, 2009 at 2:33 am
40Rounded Corners With Javascript (jQuery) by 15 Days Of jQuery…
A very simple way to make rounded corners with JQuery…
?e?a???
July 22nd, 2009 at 8:19 am
41?????-?? ???????? ??? ???????
Interesting list of tutorials and codes to build a rounded corners with Javascript and CSS | AEXT.NET
August 17th, 2009 at 6:51 pm
42[...] A very simple way to make rounded corners with JQuery… See more … [...]
Bütün Taray?c?lar için Kö?eleri Oval Yapmak » mujdatkorkmaz.com: blog
August 17th, 2009 at 7:00 pm
43[...] taray?c?lar için geçerli olan ve kullan?m? da gayet basit olan bir özellik. Hay?r, tabii ki jQuery ile yap?landan bahsetmiyorum. Bana göre daha basit ve daha kullan??l? bir özellik olan [...]
Chris R
September 18th, 2009 at 2:04 pm
44Guys use ddroundies-
its a jquery script that uses pixel dimensions to round EVERYTHING including pictures. just put the div code in the head of your page.
DD_roundies.addRule(’#menu’, ‘0px 0px 10px 10px’, true);
DD_roundies.addRule(’#banner’, ‘15px 15px 15px 15px’, true);
DD_roundies.addRule(’#banner’, ‘15px 15px 15px 15px’, true);
DD_roundies.addRule(’#footer’, ‘0px 0px 15px 15px’, true);
very simple!!!!!
10 Easy jQuery Tricks for Designers - Programming Blog
October 1st, 2009 at 11:37 am
45[...] useful technique is from Day 13 of the 15 Days of jQuery site. For a full tutorial or more detail on how it works, head on over [...]
10 trucos sencillos para diseñadores con jQuery « In Nomine Pixel
October 2nd, 2009 at 2:54 am
46[...] 10 Easy jQuery Tricks for Designers | Rounded Corners With Javascript (jQuery) Categorías:Utiles, jQuery Etiquetas:bordes redondeados, jQuery, recopilacion Comentarios [...]
Design-Flash.fr » Blog Archive » 10 Easy jQuery Tricks for Designers
October 2nd, 2009 at 7:16 pm
47[...] useful technique is from Day 13 of the 15 Days of jQuery site. For a full tutorial or more detail on how it works, head on over [...]
sioarpaesas
October 7th, 2009 at 7:29 am
48Great stuff!!
But it only works in Firefox for me. Opera and Internet Explorer don’t play acording to my FF.
Is there a fix for this?
Gregg Seipp
October 12th, 2009 at 6:46 am
49I guess the only drawback to this approach is that it doesn’t work with transparent backgrounds on the bitmaps. Seems like, if you could clip the tl, ml and bl bitmaps it would work. Is there a way to do that?
20 Simple jQuery Tricks | Design Shack
October 28th, 2009 at 10:35 am
50[...] #1 – jQuery Rounded Corners [...]
20 egyszer? jquery trükk | Blogzóna | VENTOSA kreatív stúdió
November 4th, 2009 at 10:33 am
51[...] jQuery Rounded Corners [...]
Adam Hey
November 6th, 2009 at 7:48 am
52I hate to shock you, but your example: http://15daysofjquery.com/examples/rounded/demo.php
doesn’t use javascript or jquery at all.
Claire
November 19th, 2009 at 12:05 am
53interesting. You seem very knowledgeable in da field.
Why Rounded Corners? | Freelance Web Designer
January 5th, 2010 at 7:58 am
54[...] http://15daysofjquery.com/wrap-it-up-pretty-corners/13/ [...]
Carlos
January 9th, 2010 at 2:23 am
55it’s easy to use, but, i have problems with background images ?, anyone ?
40??jquery?? | QK31
February 23rd, 2010 at 12:38 pm
56[...] Tutorial Link Demo Link [...]
RSS feed for comments on this post · TrackBack URI
Leave a reply
Categories
Archives
Links
15 Days Of jQuery is proudly powered by WordPress - BloggingPro theme by: Design Disease