The CSS3 border-radius property

The CSS3 border-radius property

One of the CSS3 properties designers have been longing the most for is undoubtedly the border-radius property. With CSS3 border-radius property it’s possible to create the so popular rectangles with rounded corners exclusively via CSS - no images needed.

Cross-browser compatibility

CSS3 aren’t supported by all browsers yet. The border-radius property is supported by Firefox (since version 3.0), Safari (since version 3.1) and Chrome (since the first version), but it’s not supported by Internet Explorer or Opera (it should be implemented in Opera 10).

Since CSS3 aren’t standard yet, you must add prefixes to border-radius in order to work with the browsers that support it. If you want to work it with Firefox, you must write -moz-border-radius in the CSS; if you want it to work with Safari/Chrome, it’s -webkit-border-radius.

Note that although Firefox, Safari and Chrome support this property, they do so in slightly different modalities. For the sake of simplicity, I’ll first show you how it is supported by Firefox and then explain the differences in Safari and Chrome.

The box

Let’s start by creating the box we will experiment the border-radius property with. In the HTML we simply build an empty box:

<body>
<div id="box"></div>
</body>

In the CSS, we give it a width, a height and a background color:

#box {
   width:590px;
   height:100px;
   background-color:#6B86A6;

Declaring the border-radius property

The border-radius property is declared pretty much like the margin and padding properties. It can be used alone as a shorthand property for all the four angles of the rectangle, or in combination with specific suffixes for each of the four angles.

Declaring the border-radius property as a shorthand property

If used as a shorthand property, assigning just one value will set that value as the radius of each angle:

#box { -moz-border-radius: 20px; }

With this CSS, all four angles will have a 20px radius:

The CSS3 border-radius property

If you assign two values, the first one will represent the radius of the top-left and bottom-right corners while the second one will represent the radius of the top-right and bottom-left corners:

#box { -moz-border-radius:20px 40px; }

With this CSS, the top-left and bottom-right corners will have a 20px radius, the top-right and bottom-left corners will have a 40px radius:

The CSS3 border-radius property

You could also assign three values: the first one will be the radius of the top-left corner, the second will be the radius of the top-right and bottom-left corners, the last will be the radius of the bottom-right corner.

#box { -moz-border-radius:20px 40px 5px; }

With this CSS, the top-left corner will have a 20px radius, the top-right and bottom-left corners will have a 40px radius, the bottom-right corner will have a 5px radius:

The CSS3 border-radius property

Your last option is to assign four values - one for each angle - in this order: the top-left corner radius, the top-right corner radius, the bottom-right corner radius, the bottom-left corner radius:

#box { -moz-border-radius:10px 20px 30px 40px; }

With this CSS, the top-left corner will have a 10px radius, the top-right corner will have a 20px radius, the bottom-right corner will have a 30px radius, the bottom-left corner will have a 40px radius:

The CSS3 border-radius property

Declaring the border-radius property for each angle

If you need to assign the border-radius property to a single angle - maybe to override a previously assigned value - you can do so by adding a suffix to “border-radius” depending on the angle you want to modify.

The suffixes are as follows:

  • -moz-border-radius-topleft for the top-left corner;
  • -moz-border-radius-topright for the top-right corner;
  • -moz-border-radius-bottomright for the bottom-right corner;
  • -moz-border-radius-bottomleft for the bottom-left corner;

So if for example you only want to round the top right corner:

#box { -moz-border-radius-topright: 30px; }

Here is what you’d get:

The CSS3 border-radius property

The horizontal radius and the vertical radius

I have so far made examples that assumed that we want each angle to be a perfect quarter circle, so that I could focus on showing you the basic syntax of the border-radius property. But with CSS3 you can also have quarter ellipses rather than quarter circles.

Let’s round the top-left corner of our box with the following CSS code:

#box { -moz-border-radius-topleft: 30px; }

Here is what we get:

The CSS3 border-radius property

The top-left corner is a perfect quarter circle. But let’s add a second value to the above CSS code:

#box { -moz-border-radius-topleft: 30px 15px; }

The CSS3 border-radius property

You can see the top-left corner is now a quarter ellipse. This is because the two values represent the horizontal radius and the vertical radius of the angle respectively. When using one value, the browser interprets that value as assigned for both the horizontal and vertical radii, and therefore renders a quarter circle.

The radii with the shorthand property

How can we declare different values for the horizontal and vertical radii when we use the shorthand property though? Enters the slash. Look at the following CSS code:

#box { -moz-border-radius: 30px / 15px; }

In the above CSS code, the first value works as the horizontal radius of each angle, while the second works as the vertical radius.

The CSS3 border-radius property

The syntax to declare all the other forms of shorthand properties follows the same logic. If we wanted to use different values for both the horizontal and vertical radii of each angle, we would write the following CSS code:

#box { -moz-border-radius: 10px 20px 30px 40px / 5px 10px 15px 20px; }

Here is the result:

The CSS3 border-radius property

Why would you ever want such a rectangle is beyond me ;) But my job here is to present you with all the possibilities. The rest is up to you!

What about Safari and Chrome?

As pointed out at the beginning of the tutorial, everything as described so far works in Firefox, while Safari and Chrome currently use a slightly different syntax (by currently I mean July 13th 2009).

Declaring the border-radius property for each angle in Safari and Chrome

The formulas used to declare the border-radius property for each angle are different in Safari and Chrome. Here they are:

  • -webkit-border-top-left-radius for the top-left corner;
  • -webkit-border-top-right-radius for the top-right corner;
  • -webkit-border-bottom-right-radius for the bottom-right corner;
  • -webkit-border-bottom-left-radius for the bottom-left corner;

Note that Safari and Chrome use the same syntax suggested in the W3C CSS3 Working Draft.

Using the shorthand property in Safari and Chrome

Safari and Chrome only support the possibility to declare one value for all the four angles. If you want to assign different values to one or more angles, you’ll have to do so by using the specific properties for each desired angle (as listed above).

The following shorthand property to assign different values for the four angles doesn’t work in Safari/Chrome:

#box { -webkit-border-radius: 10px 20px 30px 40px; }

To get the desired effect, you should change the CSS as following:

#box {
   -webkit-border-top-left-radius: 10px;
   -webkit-border-top-right-radius: 20px;
   -webkit-border-bottom-right-radius: 30px;
   -webkit-border-bottom-left-radius: 40px; }

Horizontal and vertical radii in Safari and Chrome

Safari and Chrome support the possibility to assign different radii with specific-angle properties. To make the top left corner a quarter ellipse, you should use the following CSS code:

#box { -webkit-border-top-left-radius: 30px 15px; }

As to shorthand properties, the only case Safari and Chrome allow for assigning a vertical radius is when all the four angles have the same value. Besides, the syntax doesn’t require a slash; it’s enough to write the two values consequently:

#box { -webkit-border-radius: 30px 15px; }

The above CSS code results in the following image:

The CSS3 border-radius property

Attention, please

I found out about Safari and Chrome inconsistencies by trying different CSS codes - not reading about them somewhere - so I might have made some mistakes. If you actually spot any inaccuracy, please contact me and I’ll correct the tutorial.

What can apply the CSS3 border-radius property to?

The answer is: everything. All border-related properties are applied to the boxes each HTML elements is included in according to the CSS box model. Therefore you can apply border-radius to menus, paragraphs, images, lists, etc. In the following images there are three examples: a paragraph, a menu with rounded links, and another paragraph with a border. Note how the border gets rounded, too:

The CSS3 border-radius property

So, what do you think?

Are you excited about creating rounded rectangles just with CSS? Have you used the border-radius property already? Share your thoughts in the comments!

47 Responses

    • Mike Brisk
    • Jason Grant
    • Robert
    • Michael Combs
    • grazz
    • pig1000
    • dottorgonzo
    • Carson Shold
    • grazz
    • Barbara Henley
    • grazz
    • bart
    • grazz
    • Jason
    • Graham
    • devolute
    • Tony
    • stk
    • Nuno Mestre
    • Scott Phelps
    • grazz
    • links for 2009-07-30 « James A. Arconati
    • Jason
    • links for 2009-07-31
    • Twitted by Pudny
    • tudor moldovan · blog personal
    • FireDart
    • Kolyan
    • eneko
    • Lee
    • cssah
    • devolute

Leave a Reply