As more and more browsers adopt the CSS3 standards, it is becoming easier to create a rounded corners feel to a website. Soon web developers will be able to do away with the headache of creating additional rounded corner background images just to give the “feel” of rounded corners. Although we will never get back the hair we pulled out while trying to get rounded corners to look correct on different browsers, at least we should have less struggle once CSS3 becomes more mainstream.
This leaves us with a question, can I get rounded corners by using only CSS while I wait for a broader adoption of CSS3? Yes, you can. Through the use of some proprietary CSS properties, there are already browsers on the market that can display rounded corners. Here are some samples of proprietary CSS properties that would create a 10 pixel rounded border:
-moz-border-radius: 10px;
-khtml-border-radius: 10px;
-webkit-border-radius: 10px;
(Mozilla Firefox uses the -moz-border-radius property and Safari (WebKit) & Chrome use the -webkit-border-radius or the -khtml-border-radius property.)
It is important when using these properties that you protect backwards compatibility and cross browser considerations. Because of this a developer should be including all the above code in order to provide a unified look and feel of the website across the most number of browsers. If you are interested in how different browsers currently handle the border-radius properties, Jerry Seeger has a very useful & graphical table to help you out.
Example Code
What good is a quick tutorial without some sample code and some description of what is happening with the sample code. Well here are two quick examples of something you could include in your style.css file to achieve a simple rounded corner effect.
Four Corners
.rounded-box {border:1px solid #DDDDDD; border-radius:10px; -moz-border-radius:10px; -webkit-border-radius:10px; -khtml-border-radius:10px;}
This code will apply a rounded border feel to the “rounded-box” div. All four corners will be equally rounded with a 10 pixel radius and have the color #DDDDDD (which is a light grey)
Only Two Corners
.twocorner-box{border:1px solid #FF9999; border-radius:5px 5px 0px 0px; -moz-border-radius: 5px 5px 0px 0px; -khtml-border-radius: 5px 5px 0px 0px; -webkit-border-radius: 5px 5px 0px 0px;}
This code will apply a rounded corner to the top two corners of the “twocorner-box” div. The border color will be a pink-red (#FF9999) and will have a 5 pixel rounded radius on the top left and top right of the “box”.
An important tip to remember that will help you read CSS is that when you see four pixel numbers together on one css box property, they are referring to each “side” of the box”. When you see four pixel numbers together on a radius property, the first number refers to the top-left corner. The second number refers to the top-right corner… and it continues clockwise around the box.
So that if I only wanted the top-left and the bottom right to be rounded, one way of performing this task would be:
border-radius: 5px 0px 5px 0px;
The box concept in CSS is very important. We will talk more about this later. But one of the “A-ha!” moments for many people learning CSS is when they begin to see things in a box-style framework.
Also, in the sample code above, remember that the border-radius property is not yet supported by all browsers. But it isn’t going to hurt you by including this in the code now. This way you will always have forward-compatibility since CSS3 has already setup this standard for the border-radius property. Browsers that do not have this compatibility will just ignore the code. No Harm – No Foul.
Additional Information:
Here are some other great sites with additional information on CSS3 and rounded corners:
http://www.css3.info/preview/rounded-border/
http://www.w3.org/TR/css3-background/
http://www.w3schools.com/css/css_boxmodel.asp (CSS Box Model information)
UPDATE:
We need to remember that until CSS3 is fully adopted across the board on all browsers, developers will continue to run into problems… even this developer who wrote this post. It was pointed out that I forgot about a wonky feature/bug that pops up when using the -webkit-border-radius property. The -webkit-border-radius property does not support the stringing out of the 4 pixel numbers IF the corners are different. So when using the -webkit-border-radius property you will need to specify each corner as follows:
-webkit-border-top-left-radius: 10px;
-webkit-border-top-right-radius: 5px;
-webkit-border-bottom-right-radius: 3px;
-webkit-border-bottom-left-radius: 2px;
It’s an odd little issue, but if you don’t actually specify the top-left, top-right, bottom-right, and bottom-left is won’t display correctly on browsers like Safari and Chrome.