Perfect Rounded Corners with CSS

by Jeff Starr on Monday, November 24, 2008 54 Responses

[ ~{*}~ ] A great way to enhance the visual appearance of various block-level elements is to use a “rounded-corder” effect. For example, throughout the current design for this site, I am using rounded corners on several different types of elements, including image borders, content panels, and even pre-formatted code blocks. Some of these rounded-corner effects are accomplished via multiple <div>s and a few background images, while others are created strictly with CSS.

Of these two different methods, extra images and markup are used whenever I need the rounded corners to appear in all browsers, or in other words, whenever the effect is an essential aspect of the design. And then, on the other hand, when using rounding corners for visual enhancement, I prefer the strict-CSS method because it is much easier to implement. Rounding the corners on image borders or <pre> elements, for example, is an excellent way to progressively enhance the visual presentation of a design (generally speaking).

The good news about using CSS to create rounded-border effects is that it is possible to apply the effect to many different browsers. Sure, Internet Explorer can’t deal with it, but that’s the whole idea behind progressive enhancement, right? Browsers that “get it” enjoy the benefits, while those that don’t continue to function perfectly well without the enhancements. So, for those of you looking for a complete collection of quick copy-&-paste code snippets for easy rounded corners with CSS, here you go:

CSS Rounded corners for all four corners

This CSS rounded-corner recipe applies a 10px-radius rounded corner effect to all four corners of any block-level element:

/* 4 rounded corners */
.all-four-rounded-corners {
	-webkit-border-radius: 10px;
	-khtml-border-radius: 10px;	
	-moz-border-radius: 10px;
	border-radius: 10px;
	}

CSS Rounded corners for top-left corner

This CSS rounded-corner recipe applies a 10px-radius rounded corner effect to the top-left corner of any supportive block-level element:

/* top-left rounded corner */
.top-left-rounded-corner {
	-webkit-border-top-left-radius: 10px;
	-khtml-border-radius-topleft: 10px;	
	-moz-border-radius-topleft: 10px;
	border-top-left-radius: 10px;
	}

CSS Rounded corners for top-right corner

This CSS rounded-corner recipe applies a 10px-radius rounded corner effect to the top-right corner of any supportive block-level element:

/* top-right rounded corner */
.top-right-rounded-corner {
	-webkit-border-top-right-radius: 10px;
	-khtml-border-radius-topright: 10px;	
	-moz-border-radius-topright: 10px;
	border-top-right-radius: 10px;
	}

CSS Rounded corners for bottom-left corner

This CSS rounded-corner recipe applies a 10px-radius rounded corner effect to the bottom-left corner of any supportive block-level element:

/* bottom-left rounded corner */
.bottom-left-rounded-corner {
	-webkit-border-bottom-left-radius: 10px;
	-khtml-border-radius-bottomleft: 10px;	
	-moz-border-radius-bottomleft: 10px;
	border-bottom-left-radius: 10px;
	}

CSS Rounded corners for bottom-right corner

This CSS rounded-corner recipe applies a 10px-radius rounded corner effect to the bottom-right corner of any supportive block-level element:

/* bottom-right rounded corner */
.bottom-right-rounded-corner {
	-webkit-border-bottom-right-radius: 10px;
	-khtml-border-radius-bottomright: 10px;	
	-moz-border-radius-bottomright: 10px;
	border-bottom-right-radius: 10px;
	}

The Full Meal Deal — Non-symmetrical CSS Rounded corners

This CSS recipe applies non-symmetrical rounded corners to all four corners of any supportive block-level element. This technique is basically writes the various border rules individually, thereby enabling differing values for each rounded-corner radius. Confused? Don’t be; just check out the following code and everything will be perfectly clear:

/* exploded rounded corners */
.exploded-rounded-corners {
	-webkit-border-bottom-right-radius: 10px;
	-webkit-border-bottom-left-radius: 10px;
	-webkit-border-top-right-radius: 10px;
	-webkit-border-top-left-radius: 10px;

	-khtml-border-radius-bottomright: 10px;
	-khtml-border-radius-bottomleft: 10px;
	-khtml-border-radius-topright: 10px;
	-khtml-border-radius-topleft: 10px;

	-moz-border-radius-bottomright: 10px;
	-moz-border-radius-bottomleft: 10px;
	-moz-border-radius-topright: 10px;
	-moz-border-radius-topleft: 10px;

	border-bottom-right-radius: 10px;
	border-bottom-left-radius: 10px;
	border-top-right-radius: 10px;
	border-top-left-radius: 10px;
	}

Round ‘em!

Let me know if I missed anything here.. I want to provide an easy, complete and accurate copy-&-paste reference for creating rounded corners with CSS. So if you know of any related tricks or techniques, please share them in the comments section. Otherwise, see you next post! Thanks for reading :)


54 Responses

Add a comment

[ Gravatar Icon ]

Graham Smith#1

Wowzer Jeff,
Just the sort of thing I was looking for for my new sites. Certainly going to give this a go quite soon.

And I really have to say hat’s off to the site design, more like a site evolution that has missed a few steps in between.

You are far and away a most talented and gifted creative. Good for you, this really takes stuff to a whole new level.

Graham

[ Gravatar Icon ]

Jeff Starr#2

@Graham: Great to see you again, Mr. Graham! I am stoked that you find the latest design incarnation to your liking, but it won’t be around for long, I’m afraid: I am currently working secretly behind the scenes on a brand new theme, hopefully to be rolled out at the beginning of 2009. If the current design is a “site evolution,” then the next design would be a “de-evolution!” And that’s certainly enough said about that!

As for the kind words, Graham, you always flatter me! I really don’t know how to respond, other than to say that I am still a novice compared to many in the community. Quite honestly, I have been completely blown away by your intense activity on teh Intarwebz as of late — there is no way I could ever match your sheer and utter velocity within logo design circles and social media in general. It is a pleasure to watch as you rise to the top!

[ Gravatar Icon ]

Rick Beckman#3

Jeff, you’re the man. I knew about the -moz bits and am currently rocking them on my site; the others (as well as the [I’m guessing] CSS3 property) were unknown to me.

Thanks for rectifying that. I’ll definitely be adding support for other browsers later today.

[ Gravatar Icon ]

Jeff Starr#4

@Rick: Always a pleasure to see you! You have been a busy man lately, from what I gather from your always insightful kingdom feed. What was it, like yesterday my feed reader was cold stuffed with all sorts of fresh Beckman goodness: politics, religion, and even WordPress to boot! An incredible run, to the say the least!

As for the current post, it is an honor to provide useful information that benefit fine people such as yourself. With each passing day, pure CSS borders are becoming a reality for all modern browsers. Of course Internet Exploder hasn’t joined in the reindeer games (yet), but just about every other browser is now supportive of rounded corner styles via CSS.

[ Gravatar Icon ]

Kim Woodbridge#5

Hi Jeff - Haven’t talked to you in awhile. A friend and I were just discussing rounded corners - you must have read our minds and provided that code that we needed.

Thanks!

[ Gravatar Icon ]

Jeff Starr#6

@Kim: Well, it certainly is nice to see you again! Yes, I was floating around on the seventh astral plane earlier and began picking up on some strong CSS rounded-corner vibes! lol — Seriously though, glad I could help out with the article — it is my pleasure to do so! :)

[ Gravatar Icon ]

Erik#7

I was browsing using ff3 and came to this article. Curious if it worked on ie6 I openend your site in this browser. I noted that you don’t show any theme at all in ie6.

Actually you are the first site I see that completely gave up the effort to satisfy the ie6 users. This site offcourse attracts a lot of webmasters so little problem.

But for ordinary sites with lots of less technical users these kind of new technicalities remain a problem.

I wish people abandon ie6 very quickly. Nice to see that you take a stand in this respect.

[ Gravatar Icon ]

Jeff Starr#8

@Erik: Thanks for the comments, Erik! I completely agree that people should drop support for IE6 as soon as possible — it is just a horrible browser, especially by today’s standards.

You observe correctly that the current (Quintessential) theme doesn’t support IE6; after spending a gazillion hours making it work in modern browsers, I just didn’t have the stamina to fix everything that was broken in IE6.

As for CSS rounded-corner support in IE, I don’t worry about it for design enhancements, but would certainly take the time to code up some proper rounded-corner sprites if they were central to the design.

[ Gravatar Icon ]

Chris Coyier#9

I like the idea of using border-radius as well as the vendor-specific attributes. It’s very likely to become the final css3 spec, so even though I don’t believe there is any browsers supporting it yet, you’ll be future-proof. It doesn’t validate, but neither do the vendor-specific ones so whatever.

[ Gravatar Icon ]

Joshua Richardson#10

Just a note regarding rounded corners in IE, I came across a nifty solution for this that uses VML and HTC files to draw the corners.
I wish I could claim this idea for myself but I found it at: http://www.htmlremix.com/curved-corner-border-radius-cross-browser/

So if you truly want cross-browser rounded corners it is indeed possible for most browsers (without nested div’s and what not).

Great article though and it made me aware of the -khtml css statements I didn’t realise existed.

Also nice to hear you will be redesigning the website, I hope the next incarnation will be a bit more minimalistic.

[ Gravatar Icon ]

teddY#11

I love the idea of what you think about rounded corners - it’s an enhancement. Older browsers (like IE6, it’s so 1990s), visitors will not suffer from incorrect/inaccurate display or rendering of elements but simply revert to the blocky styles. The rounded corners are something like a bonus to those who use up-to-date browsers, heh.

The properties for rounded corners are aplenty, but I’m not sure which is CSS3 valid or not. Are all of them CSS3 valid or is just strictly just border-radius? So far -moz-border-radius and etc are not recognised to be valid… but that doesn’t bother me a lot since I’m not exactly a CSS puriest. I just aim to make my layouts XHTML valid - I guess that’s the most important part for designs.

Thanks for sharing again! I find your entries really insightful and more importantly, relevant to what web designers and coders alike need and want to know.

[ Gravatar Icon ]

zubfatal#12

wow, I didn’t know it could be done that simple, however what I really want is a solution that works in all browsers, still using pure css, no javascript and no images, which almost seems like a search for the holy grail heh, I mean I’ve found a bunch of examples how to accomplish this using css only, but most of them either requires images, which is allright except I don’t like it, because I want to be able to change the color of the design, without having to re-do the images. Other examples uses quite a lot of extra markup, which I’d rather be without.

I’ll probably give this a go, and forget about support for browsers which doesn’t understand it for now, since the lack of support for this won’t actually “break” anything, but just appear as square boxes instead.

[ Gravatar Icon ]

zubfatal#13

Hm, just noticed that using these properties prevents the CSS from being properly validated - it doesn’t really matter, except if you’re clinically insane about a clean validation :)

[ Gravatar Icon ]

jeroen#14

This is the answer to life!

[ Gravatar Icon ]

Phoat#15

Great write up… A very timely post for me, as I just started using progressive enhancement techniques on the sites I currently maintain and the results are well worth it.

One question… can’t you simplify your code by combining all the radius sides into one statement. For Firefox for example, I’m pretty sure instead of this…

-moz-border-radius-bottomright: 10px;
-moz-border-radius-bottomleft: 10px;
-moz-border-radius-topright: 10px;
-moz-border-radius-topleft: 10px;

you could do this

-moz-border-radius: 10px 10px 10px 10px;

or even more simply

-moz-border-radius: 10px;

This seems to work on all my tests and makes for cleaner code. Same applies for -webkit-border-radius but I’m not sure about khtml.

Again… great write up!

[ Gravatar Icon ]

Jeff Starr#16

@Phoat: Absolutely — check the first chunk of code in the article for this exact technique. Completely cleaner, faster, and more efficient. Thanks for the comment and best of luck with your site!

[ Gravatar Icon ]

Jeroen#17

It shows sometimes right and sometimes it doesn’t show right. But when i zoom in or out it will show right but an other border won’t anymore.

From my iPod 2nd generation

[ Gravatar Icon ]

Jonathan Ellse#18

What are -webkit, -moz and -ktml doing? I realise that they are for specific browsers, but which.

Love the coding, works a dream in every browser I can throw at it, exculding IE of course, but anyone using IE is unworthy of seeing these beatiful rounded corners.

Great Work!

[ Gravatar Icon ]

Thomas Levy#19

Never knew about these little tidbits before, soooo much easier than placing rounded corner images

thanks!

[ Gravatar Icon ]

EricB#20

I am impressed that the round boxes on this site are transparent. How is that done?

[ Gravatar Icon ]

Jeff Starr#21

@EricB: The background boxes for the dark/colorful Quintessential theme are made with CSS and background-image sprites. I am planning a tutorial on the topic just as soon as I can find the time.

[ Gravatar Icon ]

shubha#22

Hi,

Hey this code is not working with me… is there anything i must download or refer to any file??

border-radius: 10px; this statement is showing some error in css, that its not a recognized element.

And what is that webkit or khtml or moz…

i was totally confused about how to use this…

please give me some sample page using the same..

please explain me..

Thanks & Regards,
Shubha K.S.

[ Gravatar Icon ]

Vinayak#23

Seriously man.. you have changed my life with this code LOL.

Worked instantly. I always thought this cannot be done without creating curved images or by using javasacript. Undoubtedly, your CSS solution for the purpose is THE BEST I had seen so far.

[ Gravatar Icon ]

shubha#24

hey man its working fine in mozilla… that looks really great..

but dont know why its not working in IE 7.. windows XP SP2… VS 2005

..

so is there any technique that i can make this work in IE7 also… help would greatly appreciated…

[ Gravatar Icon ]

Jeff Starr#25

@shubha: Not sure.. The CSS styles presented here will only work in supportive browsers. You could always use a bit of JavaScript or even background images to serve browsers that just don’t “get it”.

@Vinayak: Thank you kind Sir — your generous feedback just made my day! Cheers!

[ Gravatar Icon ]

Vinayak#26

ah Thanks to YOU Jeff, you’re the Starr :)

btw, I have few browser related questions please.

1. I have already gone through the whole blog posts and comments, but just wanted to confirm if there is simply NO WAY to get this work in IE. I find it really surprising that even IE 8 is not supporting it. May I know whats SO SPECIAL in this code that even latest browser fail to recognize it!

2. It also does not work in Opera. Gives a bit cluttered curved edges in Chrome. Works great on Safari though.

3. Works great on latest Firefox BUT this CSS code has slowed down the site browsing ONLY on FF (which is my primary browser). I mean scrolling the page has become quite rough, feels heavy. I hope you understood what I mean. Can you please browse my site for a moment and see what i mean. Btw, I experience the same behaviour on this site.

Thanks again.

[ Gravatar Icon ]

Jeff Starr#27

@Vinayak: You are too kind!

I wouldn’t say that there is absolutely positively without a doubt “NO WAY” to get CSS rounded-corner functionality in IE. A way may indeed exist, but I sure haven’t found it yet. And for the record, I am not surprised that IE8 isn’t supporting it either; frankly, I wish IE had never existed!

Also, your pages seems to scroll relatively smoothly on my home machine, but I haven’t tested it on my (slower) laptop. For this site (current Quintessential theme), there are actually two contributing factors to the slow scrolling: rounded corners and transparent background images. This sort of heavy design implementation requires a significant amount of processing power to render the page. It has more to do with the amount of memory available to the browser than anything else. For sure, my next design will be less resource-intensive. ;)

[ Gravatar Icon ]

Michael B#28

awesome article! I love how using this technique doesn’t break internet explorer at all, but instead gives them a crappy straight border.
I hate IE and I hate the fact that people use IE, so I say if they want to use that browser which doesn’t support (within reason) the standards or trends of web design, then let them have a crappy experience.

I know, not exactly good for business, but what they don’t know can’t hurt ‘em.

[ Gravatar Icon ]

Jeff Starr#29

@Michael B: Amen, brother! Death to IE! :)

[ Gravatar Icon ]

dugfresh#30

Does the fact that it’s not working in some modern browsers (i.e., the latest Safari, the latest Chrome, the latest Opera) have anything to do with the DTD not being “strict”? Just curious. I have to build my pages through a CMS editor, as the pages are portions of the overall website, which is already governed by several stylesheets. This requires me to insert an inline stylesheet at the top of my page…for every page I create. Anyway, the rounded corners CSS works flawlessly in FF.

Oh, and great article, btw. Very simple (which I like), and who cares if IE can’t pick it up? M$ is trying to create their own set of web standards, a feat at which they have succeeded. Unfortunately, their web standards suck, and make developing web sites that work in their browsers a migraine-inducing exercise in futility.

[ Gravatar Icon ]

Ejiofor Akonam#31

Shame on IE!

[ Gravatar Icon ]

Nathan Rosquist#32

Awesomely useful!

Thanks!

Nathan

[ Gravatar Icon ]

Ourobo#33

If I wish to create rounded tabs, where do I paste this? Can the code be pasted within openhooks?

And, of course, what code do I need to embed the above code within to have it apply to my navigation bar?

Thanks for any assistance!

[ Gravatar Icon ]

Jeff Starr#34

Hi Ourobo, sorry not too familiar with openhooks. But you should be able to apply the styles to any navigation markup by adding the code to your CSS stylesheet and targeting the elements that you want to round. I hope that helps! :)

[ Gravatar Icon ]

Ourobo#35

Thank you Jeff. I was able to get it working (learning a bit more about CSS in the process). I’ve also played with your piece to create transparencies. It looks like it makes the text fade as well. So I must be targeting the wrong element?

For e.g. using it on ContentArea

[ Gravatar Icon ]

Jeff Starr#36

Hi Ourobo, you have stumbled onto one of the shortcomings of CSS (as it currently exists). As far as I know, there is no way to prevent the child element from applying the same transparency as its parent, which seems to be the case in your situation. One of the best workarounds that I have seen can be found here. If you discover anything else, please let me know. Good luck!

[ Gravatar Icon ]

Extend Studio#37

You could try FlexiPanels CSS, Dreamweaver extension that generates rounded corners css boxes from an easy to use interface.

http://www.extendstudio.com/product/flexipanels_css.html

[ Gravatar Icon ]

Nicolas#38

Thanks for the tip.

I had some corners that would ideally have been round, but I had a dynamic change-color script that would have forced me to make the corners in Photoshop in multiple colors or something.

I had left them square, but having them round painlessly on some browsers is an ideal solution.

[ Gravatar Icon ]

CM#39

Rounded corners works fine in Firefox but shows square in Safai and iPhone. What am I doing wrong?

#masthead{padding-top:1px;background-color:#c00;opacity:.85;color:#fff;-moz-border-radius:9px 9px 0 0;-webkit-border-radius:9px 9px 0 0;-khtml-border-radius:9px 9px 0 0;border-radius:9px 9px 0 0;}

and

#footer{background-color:#c00;color:#fff;text-align:center;padding:4px 0;-moz-border-radius:0 0 9px 9px;-webkit-border-radius:0 0 9px 9px;-khtml-border-radius:0 0 9px 9px;border-radius:0 0 9px 9px;}

and the html is:

<!-- masthead content begin -->

<!-- masthead content end -->

and

<!-- footer content begin -->

<!-- footer content end -->

[ Gravatar Icon ]

CM#40

Just the <div id="masthead"> and <div id="footer"> Inside the comments brackets above.

Any ideas why round corners don’t work in Safari and iPhone?

[ Gravatar Icon ]

Jeff Starr#41

@CM: are you explicitly declaring a border property for those elements? Try adding something like this so the rounded-corner properties have something to work with:

border: 1px solid #000

[ Gravatar Icon ]

CM#42

Many thanks Jeff!

The pagewrapper element above the masthead element already does have the border specified thus:

#pageWrapper{border:solid 1px #c00;border-width:0 1px;}
#masthead{padding-top:1px;background-color:#c00;opacity:.85;color:#fff;-moz-border-radius:9px 9px 0 0;-webkit-border-radius:9px 9px 0 0;-khtml-border-radius:9px 9px 0 0;border-radius:9px 9px 0 0;border:solid 1px #c00;}

When I tried adding the border-width:0 1px; to the masthead element as well I still see square corners in Safari. So I don’t think that is it.

I checked my browser detection js script, and it uses the term AppleWebkit (and applewebkit) rather than just webkit (used in the css), I wonder if that is related?

[ Gravatar Icon ]

CM#43

Correction:

When I tried adding the border:solid 1px #c00; to the masthead element as well I still see square corners in Safari. So I don’t think that is it.

[ Gravatar Icon ]

Jeff Starr#44

@CM: I’m pretty sure that Safari supports rounded corners:

http://www.the-art-of-web.com/css/border-radius/

The only browser that seems to fail is IE, which requires some JavaScript treatment for rounded corners.

Unless I’m totally spacing out again and missing something obvious, I would recommend coding up a simple HTML test case to verify rounded corners in Safari et al. Begin with something like this:

.all-four-rounded-corners {
       border: 1px solid black;
       -webkit-border-radius: 10px;
       -khtml-border-radius: 10px;
       -moz-border-radius: 10px;
       border-radius: 10px;
       }

..and then just build it up or integrate it from there.

[ Gravatar Icon ]

CM#45

Thanks Jeff, good idea.

I assumed you guys had actually tested it on Safari already.

I’ll do the tests and post the result here when I’ve pedalled 80km to also test it on the iPhone.

[ Gravatar Icon ]

Jeff Starr#46

@CM: lol - actually the test case is only for your benefit, to enable you to then integrate the working code into your project. The idea is that, in the process of doing so, you will discover the source of your issue.

As discussed in that article I suggested, rounded corners are well-known to be supported in Safari, as well as other browsers mentioned in the article. Additionally, I have been implementing rounded corners in Safari et al for quite some time. I wouldn’t have posted otherwise.

[ Gravatar Icon ]

CM#47

Thanks Jeff :)

I’ve got it working in Safari (but haven’t had chance to look in iPhone yet).

Here’s the key.

For rounded corners Safari ignores the abbreviated coding format 9px 9px 0 0; but does recognize the very long format:

-webkit-border-top-left-radius:9px;-webkit-border-top-right-radius:9px;

So this works in FF and Safari:
(Note I deleted the khtml and repeat border-radius lines for code brevity)

#masthead{padding-top:1px;background-color:#c00;opacity:.85;color:#fff;-moz-border-radius:9px 9px 0 0;-webkit-border-top-left-radius:9px;-webkit-border-top-right-radius:9px;}

#footer{background-color:#c00;color:#fff;text-align:center;padding:4px 0;-moz-border-radius:0 0 9px 9px;-webkit-border-bottom-left-radius:9px;-webkit-border-bottom-right-radius:9px;}

In the footer form, you must include some text in the footer for the bottom round corners to show, otherwise the top square corners cover the bottom rounded.

I also discovered it works without any added browser detection script, and it also works if you designate -applewebkit- instead of -webkit-.

[ Gravatar Icon ]

Jeff Starr#48

@CM: Awesome, thanks for sharing your findings. Good to know about Safari not recognizing the shorthand syntax. I didn’t actually advocate such methodology in the article, but it is certainly a logical progression of the technique. Really great information. Thanks again :)

[ Gravatar Icon ]

CM#49

Thanks for posting the original article Jeff, and my fellow commentators. The code I used also works okay in iPhone’s browser.

Trackbacks / Pingbacks
  1. Cor Cinza » Melhorando um tema
  2. Perfect Rounded Corners with CSS • Perishable Press | CSS Tutorials - CSSHelper.net
  3. links for 2009-04-13 « Amy G. Dala
  4. re: would hooks work for this? - DIYthemes Forums
  5. Rounded Corners and Openhook - DIYthemes Forums
Share your thoughts..

Read Comment Policy

Comment Rules: No spam. No profanity. Use your real name. You may use simple HTML tags for style. Wrap all code in <code> tags. Learn more.



Attention: Do NOT follow this link!