Simple Rounded CSS Corners
Posted on April 14, 2006 in InternetUpdate: I have updated the demo page and added an "advanced" version which will allow you to add style to the whole box, not just the corners -- so really this is no longer just for creating "Simple Rounded Corners" it really could be used to create almost any layout desired.
On par with my usual Friday morning ritual, I fired up del.ico.us, ate a bran muffin and a bag of potato chips and traveled through the popular links and saw that there were several different solutions for creating rounded corners in CSS.
Some were pretty slick advanced Javascript and CSS solutions – some required sloppy required markup – some required you to visit the sites creator for a huge generated CSS file and a crumpet – and I just thought “why they gotta go make things so complicated?” (Thanks to Avril Lavigne for the brilliant lyric)
My first approach was with the pseudo :before and :after selectors and use of the content: url() css command .. that was not happening. I guess you can only stick an image before – you cannot control its layout and position. lame.
So where I ended up was even more simple than that.
I decided to dynamically draw in the required XHTML onload with javascript and use some simple CSS to get it's layout – therefore there won't be sloppy markup which won’t validate in my code. » visit the demo page
The Markup
<div id="simpleRounded">
<h1>Simple Rounded Corners</h1>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla pulvinar egestas massa. Proin lorem.
Nulla feugiat mi nec metus feugiat luctus. Donec gravida varius risus. Mauris sed sapien. Aliquam enim
risus, semper ut, cursus pellentesque, auctor sit amet, libero. Mauris magna. Mauris placerat accumsan
arcu. Quisque et dolor.</p>
</div>
The Javascript
function roundCorners(){
// This is a simple loader for more than one round corner box on a page
addCorners('simpleRounded');
addCorners('advancedRounded');
}
function addCorners(id){
obj = document.getElementById(id);
preWrapper="<div class=\"topleft\"><div class=\"botright\"><div class=\"botleft\"><div class=\"topright\"><div class=\"boxContainer\">";
postWrapper="</div></div></div></div></div>";
objContent = preWrapper + obj.innerHTML + postWrapper;
obj.innerHTML = objContent;
}
As you can see all I'm doing here is rewriting the content of the id which is passed into the function to include 5 nested div's. (One for each corner and one container div)
The accompanying css is just as simple.
The CSS
#simpleRounded{
margin: 1em;
width:55%;
background-color:#003699;
color: #fff;
font-family:Helvetica, sans-serif;
font-size:11px;
}
#simpleRounded p{
margin:0;
padding:0 0 1em 0;
}
#simpleRounded h1{
magin: 0 0 1.5em 0;
padding: 0;
font-size: 1.5em;
}
/* These are the 5 javascript embedded div's for the 4 corners and then a container div. */
#simpleRounded .topleft{
background: url(rc_simple_topLeft.gif) top left no-repeat
}
#simpleRounded .topright{
background: url(rc_simple_topRight.gif) top right no-repeat
}
#simpleRounded .botleft{
background: url(rc_simple_botLeft.gif) bottom left no-repeat
}
#simpleRounded .botright{
background: url(rc_simple_botRight.gif) bottom right no-repeat
}
#simpleRounded .boxContainer{
padding: 15px;
}
Check out the demo page The code is still beta -- please let me know if you notice a problem in your browser. Tested in IE 6, Firefox 1.5 and Safari 1.2

RSS (2.0)
Previous Comments
Doesn't work in Firefox. Ah-ha! That's where "Mr. Complicated CSS and JS" tells "Mr. Simple" to shove it.
Anonymous--
The comments about some of the solutions being complicated was not meant as an insult -- sorry you took it that way. It was my opinion that there was another possible approach to this and because you failed to identify yourself so we can't discuss the differences/benefits in each approach..
You mention that the solution in your browser created an error but this seems to be working in my versions of Firefox -- and because you are for some reason intimidated by a more simplistic approach -- you failed to give any helpful information on "why" it was failing.
Part of proposing a solution to a problem (as you have, somewhere) is soliciting feedback on that solution. Don’t take it to heart -- we're not comparing penises here.
I've already said too much.
This also works in Safari 2.0.3
Thanks Andrew -- do you know if it works on older versions of safari as well .. I tested it on Safari 1.2 and it seemed to work fine -- someone mentioned they were having a problem.
I think it may be the compatibility of the javascript innerHTML property ..
What a weird posting to post Anonymous as. And what is the deal with anonymous anyway. Put in a little effort and make up a name at least. Use some creativity. Or use some good ones already out there -- Count Longrod (Baron) Von Hugenstein.
I found this particular rounded corner solution to have a few flaws.
1. You're using javascript for layout? Very Bad Idea! Very unfriendly. You require javascript enabled in order to correctly view the site? Your page breaks under every browser with javascript disabled.
2. Ok, let's say that we don't care if our users without javascript are boned... Those with browsers that are 100% W3C DOM compliant will not have a .innerHTML attribute available to their javascript routines! If you've got the DOM element via document.getElementById(string), why not use one of the DOM supported routines?
.removeChild(element), .appendChild(element), .insertBefore(element,element)
you can use the DOM element methods and properties to discover parents and sibling nodes, then parent.insertBefore(element, nextSibling).
Your approach is to reduce effort on the designer's part by assuming a lot about your visitors' browser.
I find that the best approach is to either hard code the HTML/CSS, or to serve up the code via CGI (server side code). If you don't have access to CGI (PHP, ASP, Perl, etc) then hard code it.
My only beef with the 'advanced' example was some sheering when viewed with FF 1.5.0.2, the corners were not quite smooth.
Thanks for the comments guys.
VC --
There is no perfect answer to this problem -- I think this is what we are all trying to do figure out the best solution. As I mentioned before my solution was aimed at answering this problem without using unaccessible, unvalid markup.
In the sted of those two -- yes javascript is required. Is it a perfect solution? No. If a user has javascript turned off they just won't see the site as intended graphiclly, but as with many other sites the site is still fully functional.
I doubt we will ever be able to design a site that works flawlessly on 100% of the browsers with various settings -- gotta pick and choose our battles. Embedding nested div's in the markup with a server side solution will cause the site to fail accessibility validation and make some messy markup -- You might as well just write it into the content then.
RE: innerHTML -- I knew that it may cause some browser issues .. I have already started working on a version of this script which uses .document.createElement and appendChild -- i'd like to see your comments on that when comepleted.
Justice --
I think that may be due to my images which I hastily cut yesterday. Ill see if I can fix it .. are you talking about the top right corner?
Thanks, this really help for me.
works fine for me (firefox 1.5.0.2)
pretty cool idea, i like the use of javascript to apply it afterwards so it doesn't mess up the code.
I think i prefer Spiffy Corners, which has no need for images at all, but you're right, it does have the disadvantage of untidy and non-semantic code. How about a javascript solution similar to yours which adds the corners automatically afterwards?
Your advanced sample has a problem in IE 6.0 when I expand the browser to full screen. My screen resolution is 1600 x 1200. The top right and bottom left corners have about 50-75 pixes of separation between the corner image and the horizontal image.
Thanks anonymous -- the space you are seeing is because I am using background images which are about 900px wide. If I was to make those images bigger they would cover the area correctly. Good find.
Thanks for showing us! Very usefull :)
Hi,
Very good and usefull tutorial. I got a problem. I want to give dynamic photos rounded corners. I tried to use this script to get this done. Get rid of all the paddings and margins, but the corner images won't get on front. I tried z-index, but with no success.
Please help me?!
Tnx Martijn
Thanks, this really help for me.
Thanks, this really help for me.
CSS "Cascading Style Sheets" LessoNs - WeB DesigN LessoN - - Web site : http://WWW.css-lessons.ucoz.com/index.html
http://www.tapuz.co.il/olamot/UserProfile.aspx?profileID=2976650