How to Created Rounded Corners for Your Box Borders in CSS
If you were to create a rectangular box outline around the content on your web page using Cascading Style Sheets (CSS), the default is that your box borders will have pointed right-angled corners. To get rounded corners for the box, many webmasters in the past have resorted to using images to give the appearance of curved corners. This article shows you how to achieve the same effect using CSS. It also discusses some of the limitations to using CSS for this purpose at this time.
Who is This Article For?
This article is written for someone who knows how to work directly with HTML and CSS code. You don't have to be an expert in either HTML or CSS to understand this article, but you need to know the basics, otherwise you might be lost when I start talking about CSS rules and properties.If you don't have a website, and are reading this article just to get the feel of things, it's best to start by reading How to Make / Create Your Own Website: The Beginner's A-Z Guide instead. The latter is written in plain English, and is more suitable for the beginner.
Rounded Corners Using CSS 3's Border-Radius
The CSS property for creating rounded corners is known asborder-radius. It is part of CSS Level 3, which at the time this article is written, is still in its "Working Draft", meaning that it has not been formally adopted as a standard yet. Although it has not been officially standardised ("standardized" in certain variants of English), the property appears to have already been implemented in the current versions of Opera, Chrome and Safari. Perhaps more importantly (since they have such a huge browser share), Microsoft has also included support for it in the pre-release Platform Preview version of Internet Explorer 9 ("IE9"), which means that when IE9 is released some time in 2010, it will be able to render your CSS code with that property correctly. Let's take a look at an example box using the
border-radius property (below). Rounded Corners Using Purely Standards-Compliant CSS (Demo)
This paragraph is enclosed in a box that uses the CSS3border-radius property. You should be able to see the effect if you're using the latest version of Opera, Chrome, Safari or Internet Explorer 9 (preview edition and above). If you're using Firefox and don't see the rounded corners, don't worry. A non-standard workaround will be given later to force it to show rounded corners. (At the time I write this, the current version of Firefox, 3.6.8, does not support border-radius.) border-radius: 20px ;
border-radius: 40px / 20px ;
If you do not want to create a symmetrical box, but want each corner to have different curvatures, the easiest way to do this is to use the following (self-explanatory) properties:
border-top-left-radiusborder-top-right-radiusborder-bottom-left-radiusborder-bottom-right-radius
border-radius except that they control only one corner of your box. That is, for each of the above properties, you can either specify a single value like "35px" or a pair of values separated by the slash character ("/") like "35px / 90px". Non-standard Workarounds for Firefox 3.x and Old Versions of Safari
At the time this article was written, the current version of Firefox, 3.6.8, does not support theborder-radius property. However, Firefox implements a proprietary CSS property called -moz-border-radius that functions the same way as the standard CSS version. As such, if you want your box to be rendered with rounded corners even with Firefox 3.6.x, you can use this property together with the standard CSS property, as in the example below. border-radius: 20px ; -moz-border-radius: 20px ;
To set a different radius for each individual corner of your box, use:
-moz-border-radius-topleft-moz-border-radius-topright-moz-border-radius-bottomright-moz-border-radius-bottomleft
Although the current version of Safari supports
border-radius, older versions also include their own proprietary equivalents, called -webkit-border-radius. The individual corners can also be customised separately with: -webkit-border-top-right-radius-webkit-border-bottom-right-radius-webkit-border-bottom-left-radius-webkit-border-top-left-radius
border-radius property. Incidentally, the
-webkit-border-radius family of rules affect all WebKit-based browsers, not just old versions of Safari. That is to say, it also affects old versions of Chrome. However, since Chrome auto-updates itself on all installations whether the user wants it or not, I've not bothered to mention the use of this rule for older versions of Chrome, since there probably aren't any in use. The CSS code below puts the standard CSS 3 rule together with the non-standard extensions from Firefox and Safari.
border: 1px solid black ; border-radius: 10px ; -moz-border-radius: 10px ; -webkit-border-radius: 10px ;
What About Internet Explorer 8 and Below?
Internet Explorer 8 and earlier does not have any support forborder-radius in any form, whether in the standard CSS 3 version or through the use of a proprietary rule like in Firefox. As such, if you are really determined to have rounded corners for your boxes for all browsers in use, you will need to find another way to do it (eg, using images). Alternatively, if you're willing to compromise a bit, and your site's visual design is sufficiently flexible to accomodate sharp edges, you can do what I currently do with thesitewizard.com's home page: implement the rules only for those browsers that support it. Visitors using other browsers will just see a regular rectangular box. I know it's not a perfect solution, but unless you're prepared to put in a lot more work using images, it'll have to do.
Conclusion
It is already possible to create rounded corners for your boxes purely using CSS (without images) that will work in many browsers today. If you're not someone who likes to muck around with slicing and splicing images for your rounded corners, the above technique is one way to get the same effect with very little effort.Menu

0 Comments: