One of my favorite sites is definetely DeviantArt. Is a great source of inspiration when you are a bit lost, and it also has a great amount of brush resources and free stock photography. So check it out
I was browsing the site yesterday and I wanted to know how they were doing their rounded corners. So I grabbed my little friend Firebug (if you don’t have it, go and get it. It’s definitely a must) and I inspected them. I found what they were doing very interesting and I wanted to share it with you.
Demo
Because we always understand it better with a demo:
Visit an online demonstration.
Why their technique is cool?
- No images
- 100% Crossbrowser (Tested in IE6, IE7, IE8, FF, Safari, Opera and Chrome)
- Dimention freedom. Can have any size and never break.
- You can have multiple box styles by just changing few lines in your css.
- Lightweight
Well, here is the catch. We must generate a code that is not semantic. A bunch of spans/divs or whatever you like with no content.
Then why are you showing me this?
Because with a little javascript (Jquery) we can include these elements easily and POOF, our un-semantic code is gone
Let’s get started.
The HTML
Never mind the class names. They are for explainatory purposes. Use shorter names if you want of course.
The final code when the javascript is done:
<div class="roundedBox">
<div class="roundedBox-content">
<p>The content of the rounded box.</p>
</div>
</div>
The inner div is necessary. You’ll see that later.
The code before making our javascript:
The amount of tags we need will be relative to the corner size. The bigger the corner, the more tags we need.
You can choose any HTML tag you want to make this. I will be using divs and spans.
The tags that will create our corners are the following:
The top corners
<div class="c t t1"><span></span></div> <div class="c t t2"><span></span></div> <div class="c t t3"><span></span></div> <div class="c t t4"><span></span></div> <div class="c t t5"><span></span></div> <div class="c t t6"><span></span></div>
Class c stands for corner. Class t stands for top, and class tx for the number.
The bottom corners
<div class="c b b1"><span></span></div> <div class="c b b2"><span></span></div> <div class="c b b3"><span></span></div> <div class="c b b4"><span></span></div> <div class="c b b5"><span></span></div> <div class="c b b6"><span></span></div>
Class b stands for bottom and class bx for the number.
I’m using 6 divs due to my corner’s size. This will depend on the corner radius you are planning to make (as I said before).
This would be the final code for each box before creating the javascript:
<div class="roundedBox">
<div class="c t t1"><span></span></div>
<div class="c t t2"><span></span></div>
<div class="c t t3"><span></span></div>
<div class="c t t4"><span></span></div>
<div class="c t t5"><span></span></div>
<div class="c t t6"><span></span></div>
<div class="roundedBox-content"></div>
<p>The content of the rounded box.</p>
</div>
<div class="c b b1"><span></span></div>
<div class="c b b2"><span></span></div>
<div class="c b b3"><span></span></div>
<div class="c b b4"><span></span></div>
<div class="c b b5"><span></span></div>
<div class="c b b6"><span></span></div>
</div>
As you can see, if we don’t integrate this technique with javascript it totally sucks, because it’s extremely hard to manage and maintain. But with the integration with Jquery it goes really smooth. When we create new content we won’t even worry about this.
Now let’s look at the CSS Code, the one that makes the magic.
The CSS code
My divs will have borders. To to this, I will assign a border on both left and right sides of the extra elements, and give them a margin too. With only that, the corners will be done
1) Simple reset + body tag
* {padding:0; margin:0;}
body {background:#D6DED4; font:85%/150% Helvetica, Arial, sans-serif; padding:20px 0;}
2) The box
.cornerBox {width:600px; margin:50px auto;}
.cornerBox-content {padding:20px;}
- We define a width and margin (just for the example. It’s not really necessary).
- We apply a padding to the content (just for the example too).
3) The corners and their borders
.c, .c span {height:1px;}
.c span {display:block; overflow:hidden;}
/* borders */
.cornerBox-content,
.c span {border-left:1px solid; border-right:1px solid;}
We define a height of 1px to every extra element and apply a display:block; to the span inside. The overflow hidden property is there due to IE6. If we don’t use it, it creates something weird.
4) Making the curve:
A rounded cornered box has a symmetry between the top curve, and the bottom one:

As this little sketch shows, the first top element’s width (t1) is equal to the last bottom’s one (b6). This can be applied to the rest of the elements too, because their distances are just the same, but inverted.
Taking this into consideration, we can define the left and right margins to both the original and the inverse element at the same time. So, let’s create the curve
.t1, .b6 {margin:0 7px;}
.t2, .b5 {margin:0 5px;}
.t3, .b4 {margin:0 3px;}
.t4, .b3 {margin:0 2px;}
.t5, .b2 {margin:0 2px;}
.t6, .b1 {margin:0 1px;}
.t1 span {border-top:1px solid;}
.b6 span {border-bottom:1px solid;}
Of course the first top element (t1) and the last bottom element (b6) have a top and bottom border to assure that the border doesn’t break.
The code is ready. Now we just have to add id’s or classes to style each box as we like. We can style them completely differently with just a few lines. I’ve got three examples:
Type 1: #cBox1
The code:
/* Box 1 */
#cBox1 .cornerBox-content,
#cBox1 .c span {background:#FFF; border-color:#BED2CD;}
The result:

Type 2: #cBox2 (Other colors)
The code:
/* Box 2 */
#cBox2 .cornerBox-content,
#cBox2 .c span {background:#728776; border-color:#4E6257; color:#FFF;}
The result:

Type 3: #cBox3 (With a gradient)
The code:
/* Box 3 */
#cBox3 .cornerBox-content,
#cBox3 .c span {border-color:#728776;}
#cBox3 .t span {background:#C5D3C3;}
#cBox3 .b span {background:#B2C3B0;}
#cBox3 .cornerBox-content {color:#333F35; background:#B1C3B0 url(../images/box3.png) repeat-x 0 0;}
The result:

That’s easy!
Finally, the Javascript (Jquery)
Create a variable for the top corners and another one for the bottom corners. They will have all the extra elements.
var topCorners = '
<div class="c t t1"><span></span></div>
<div class="c t t2"><span></span></div>
<div class="c t t3"><span></span></div>
<div class="c t t4"><span></span></div>
<div class="c t t5"><span></span></div>
<div class="c t t6"><span></span></div>
';
var bottomCorners = '
<div class="c b b1"><span></span></div>
<div class="c b b2"><span></span></div>
<div class="c b b3"><span></span></div>
<div class="c b b4"><span></span></div>
<div class="c b b5"><span></span></div>
<div class="c b b6"><span></span></div>
';
Now apply them before and after the box’s content:
$('.cornerBox-content').each(function(){
$(this).before(topCorners);
$(this).after(bottomCorners);
});
That’s it!
Final thoughts
The smaller the corner, the less noticable is the element separation. A less contrasting border from the background too.
Because whether you like it or not the corners seem kind of pixeled.
The technique is very versatile and especially interesting to use in large sites. Be creative and explore the possibilities…
nice nice nice!
when i began to read this article I said : “holy crap, no-semantic code” (?)
with jquery is an elegant way to fix this
congrats!
Well I didn’t know that one, it’s always good know differents ways to do it and have more options.
I will love if you can put the demo link in the beginning of the tutorials to see the result first, is just my opinion.
*Felicidades y espero que sigan los buenos tutoriales.
Some great articles
One suggestion though: put links to demonstration’s or files at the top of an article.
Keep up the good work.
Thank you
I moved the demo link to the top.
I have a technique that I use that only needs a top and bottom non-semantic div tag for fixed width boxes, and three background images. It uses a background image for the top and bottom and a repeating image for the middle content. I intend to use your javascript implementation here to refine the non-semantic markup out of my techniques if I can get it to work.
I kinda think this technique seems like jumping through some unnecessary hoops when you can just chop some images up.
I understand what you say Joseph.
However being able to change boxes colors, borders, etc with just few lines it’s not a bad idea.
I like experimenting and finding other ways to do stuff. Maybe one day it may com handy
Mmmm… I see your point with color change with a few lines of code.
nice trick. one suggestion: i’d fork your code to only use this approach if the browser doesn’t support the CSS border radius property. Firefox and Safari/WebKit both support border radius, so the approach covered in this post would be overkill in those browsers. it would be very useful in other browsers, though, including IE and Opera.
It might be cross browser compatible but it’s damned ugly looking on Safari and Firefox when both of those support CSS3 border-radius with anti-aliasing already.
Try http://www.atblabs.com/jquery.corners.html or just let IE users suffer square corners unless it’s really important it looks identical on all browsers.
I think just the same, keep trying!
I think this technique would be nice as a fail-safe mechanism for IE. For Firefox and Safari I would prefer -moz-border-radius and -webkit-border-radius (which look better and require no Javascript enabled).
Also on Mac, your technique messes up the blue focus glow on inputs a bit. Yet another reason to prefer the CSS way when available.
I understand guys.
I just like experimenting with new techniques :p
Thanks a bunch, I was using curvy corner which is not compatible with JQ. This works well with other plug-ins.
TU
I wished to use CSS3 style : #div1 {corner-radius : 3px; border:solid 2px #C0B0B0}
But not sure when the browsers be able to work with CSS3. I hope until then JQ and other techniques like this one and curvy corner .js are just fine, Also Curvy Corner works fine, I used it with FF & ie, But there are some conflicts when I use JQ Light Box & JQ Tooltip. But if the need is for only Round Corners This or C C may help in cross browser fixes.
I would agree with Philip and say apply natural corners for the latest Webkit and Gecko browsers (I think Opera 10 does them as well).
It also might be worth noting that this is the same technique that GMail uses to create rounded corners. I wonder who came up with it first.
I didn’t know Gmail used that too. Thanks for the info Josh
Use nifty corners its a great tool! http://www.html.it/articoli/nifty/index.html
tbh i dont like that technique very much it creates way too many elements..
use css for rounded corners, they are a nice gimmick but nothing more. browsers that dont support such things, should die anyway
thanks dear, these round corners are Most Wanted by clients..
this reminds me of the Snazzy Borders of Stu Nicholls:
http://www.cssplay.co.uk/boxes/snazzy.html
And he has more examples (called Krazy Corners):
http://www.cssplay.co.uk/boxes/krazy.html
It’s a good technique, the basis of which can be used for other effects. It is also good for people who are still getting a grasp of CSS and/or jQuery. I wouldn’t recommend it for use, but that’s me – it may fit your projects very well, and if so, great!
BTW, the jQuery can be handled easily with a single HTML command instead of before & after:
$(‘.cornerBox-content’).each(function(){
$(this).html(topCorners + $(this).html() + bottomCorners)
});
Thank you Eric.
I’m really a beginner in Jquery. I started learning a few months ago :p
Regards,
Nacho
IMHO I think that your rounded corners example uses too much extra markup which I honestly think is unnecessary.
I think that because IE.x (all of them) doesn’t support rounded corners, doesn’t mean that we need to go to such extreme of adding so much extra markup. The user experience will not be hindered, or damaged in any way because the container doesn’t have rounded corners. And from a design stand point, IE.x lack of support of rounded corners shouldn’t be an obstacle for us, so the design wouldn’t be hurt THAT much because of square corners. Please.
But if you are (and I’m talking to any one, not only the author of this website, Nacho) a hard headed Web Designer and HAVE to have rounded corners show up in IE.x, then there are these two JavaScript based solutions that use A LOT less HTML markup:
1. http://www.html.it/articoli/niftycube/index.html
2. http://www.atblabs.com/jquery.corners.html
…After all, with Nacho’s solution you’re using JavaScript as well.
But as I said before, the user experience will not be hindered and the design wouldn’t be hurt THAT much because the user is seeing square corners and rounded ones.
So, I think the best solution to this rounded corners issue is to use CSS3 rules like these:
>> HTML:
The content of the rounded box.
>> CSS:
div.roundedBox {
-moz-border-radius: 5px;
-khtml-border-radius: 5px
-webkit-border-radius: 5px;
border-radius: 5px; /*This is the CSS3 rule*/
}
-moz-: For Mozilla browsers (Mozilla, Firefox, etc.)
-khtml-: For KDE based browesers (Konqueror)
-webkit-: For Webkit based browsers (Safari, Google Chrome)
Yes, IE.x will not be able to display rounded corners, but who cares? This is a minor issue when it comes to IE.x incapabilities, so I personally don’t see why we have to use so much extra markup to accomplish something that IE.x should’ve been supporting a very long time ago. Dang, not even IE8 supports it.
Hope this helps.
Later.
I actually do care for every browser user.
I want to make their experiences to be as similar as possible.
And I’m also aware of CSS3 border radius, but CSS3 is not fully here yet.
I also prefer having my work W3C valid.
If you care about the user’s experience, then you should not bloat the HTML of your pages, hence, pages will take longer to download for your users.
Also, the two JavaScript solutions I mentioned use WAY less code than your solution. Go on, give them a try.
“I also prefer having my work W3C valid.”–> Good point, using vendor specific properties is not standard.
That’s why I typed: border-radius: 5px; /*This is the CSS3 rule*/, so the day all the browsers support the CSS3 border-radius property, all you have to do is delete the vendor specific rules.
“…but CSS3 is not fully here yet.”–>Oh, CSS3 is ‘more’ here than you think.
“…but CSS3 is not fully here yet.”–> So? Just because is not released yet doesn’t mean you’re banned or prohibited from using it or it makes you a ‘non-standards’ compliant designer/developer… at all. You’d be actually helping the web be more standards compliant by pushing the use of CSS3.
Your loved Firefox, Safari, Chrome or Opera (I know you use one of these since you are worried about being W3C compliant) support MANY CSS3 rules that you can take advantage of. Go ahead, help the web, help push the envelope of standards.
Just because a box doesn’t show rounded corners in IE.x while it does in every other browser, doesn’t mean your user’s experience is SOOOO different… at all.
But, I guess not being opened minded to push standards compliant designs and websites is scary. Nah, no need to.
Later Ignacio.
I like this use, but can you tell me if it’s better or worse than this method I use for roundies…
http://www.dillerdesign.com/experiment/DD_roundies/
The downside I have with the method I linked above is it doesn’t work with some other scripts that I run on sites occasionally, but for the most part, its fairly bullet proof, even on IE6. I also notice a fair amount of pixlation using your method, which would make sense since you’re using current CSS to render…but it’s still slightly less attractive than the smoothed corners DD_Roundies produces. Any comments are appreciated!
Hi Lars. Maybe you want to checkout an article of mine with another cool way to do it: http://cssglobe.com/post/3714/css-sprites-rounded-corners
That definetely has no rendering issues
This way is not so good with js disabled. You should use JavaScript to add the un-semantic code on page load, not to remove it.
Anyway border-radius should be supported by every browser… I definitely hate IE.
Well i have seen so many of these rounded corner options but im not sure there really is any reason for designs to use them. I personally dont like them as everything become box designs and i find them to controlled to use and not pretty at all.
Of course they are useful in some parts of a website but its up to us all to design somethig pretty and as always “less is more”
The sooner the world realis IE6 needs 5 bullets through its head for good measure (need to make sure the bugger is dead) and u[grade to ie7 and hopefully ie8 the better it is for the creative minds out there.
indeed!
I like this use. but have the rounded corner property in css3.0. but thing is not working in IE
Pretty nice post. I just stumbled upon your blog, I wanted to say that its really well written I’ve enjoyed browsing it. In any case I’ll be subscribing to your feed and I hope you write again soon! But not on my iPhone as the layouts a bit off!