Creating rounded corners using CSS
Solution 1
Since CSS3 was introduced, the best way to add rounded corners using CSS is by using the border-radius
property. You can read the spec on the property, or get some useful implementation information on MDN:
If you are using a browser that doesn't implement border-radius
(Chrome pre-v4, Firefox pre-v4, IE8, Opera pre-v10.5, Safari pre-v5), then the links below detail a whole bunch of different approaches. Find one that suits your site and coding style, and go with it.
- CSS Design: Creating Custom Corners & Borders
- CSS Rounded Corners 'Roundup'
- 25 Rounded Corners Techniques with CSS
Solution 2
I looked at this early on in the creation of Stack Overflow and couldn't find any method of creating rounded corners that didn't leave me feeling like I just walked through a sewer.
border-radius:
Which is exactly how you'd want it to work. Although this works OK in the latest versions of Safari and Firefox, but not at all in IE7 (and I don't think in IE8) or Opera.
In the meantime, it's hacks all the way down. I'm interested in hearing what other people think is the cleanest way to do this across IE7, FF2/3, Safari3, and Opera 9.5 at the moment..
Solution 3
I generally get rounded corners just with css, if browser does not support they see the content with flat corners. If rounded corners are not so critical for your site you can use these lines below.
If you want to use all corners with same radius this is the easy way:
.my_rounded_corners{
-webkit-border-radius: 5px;
border-radius: 5px;
}
but if you want to control every corner this is good:
.my_rounded_corners{
border: 1px solid #ccc;
/* each value for each corner clockwise starting from top left */
-webkit-border-radius: 10px 3px 0 20px;
border-radius: 10px 3px 0 20px;
}
As you see in each set you have browser specific styles and on the fourth rows we declare in standard way by this we assume if in future the others (hopefully IE too) decide to implement the feature to have our style be ready for them too.
As told in other answers, this works beautifully on Firefox, Safari, Camino, Chrome.
Solution 4
If you're interested in creating corners in IE then this may be of use - http://css3pie.com/
Solution 5
I would recommend using background images. The other ways aren't nearly as good: No anti-aliasing and senseless markup. This is not the place to use JavaScript.
Related videos on Youtube
Comments
-
Eldila about 4 years
How can I create rounded corners using CSS?
-
Stan almost 16 yearsHere's a good video on how to make rounded corners using only CSS: <sampsonvideos.com/video.php?video=12>
-
aruno over 15 yearsArticle on rounded corners techniques from Yahoo Developer network - from 2007. And a way to add rounded corners (requiring images) to a YUI panel.
-
SteveCav about 9 years200+ likes says it IS constructive.
-
DavidRR about 9 yearsI believe this is a valuable canonical question that deserves to be reopened, especially since it is now community wiki.
-
Jim Garrison about 9 yearsExcept that it is information that is trivially findable many, many other places on the web. There is no need for SO to duplicate existing resources.
-
-
tschoartschi almost 16 years"...walked through a sewer?" That's a bit harsh, Jeff. The answer is "It depends." Single color, gradient, or drop-shadow boxes? Do they need to expand vertically, horizontally, or both? Different solutions for different requirements. The fancier the requirement, the more sewer-like the solution.
-
Sam Murray-Sutton almost 16 yearsI would prefer using this method. If a browser doesn't support it, who cares?
-
Lance Fisher over 15 yearsTwitter downgrades the rounded corner feature to pointed corners on their website to IE users. Not a big deal really.
-
Wolfr about 15 yearsYour syntax for -webkit-border-bottom-rightright-radius: 3px; and -webkit-border-top-rightright-radius: 10px; should read -webkit-border-bottom-right-radius: 3px and -webkit-border-top-right-radius: 10px;
-
aruno almost 15 yearsjavascript definitely has the highest scope for failure, and flickering in my experience. all the jquery plugins i've tried have some kind of unexpected side effects
-
vsync almost 15 yearsi find DD_roundies to be the prefect solution: dillerdesign.com/experiment/DD_roundies/#nogo
-
vsync almost 15 yearsnever understood why so many are concerned with CSS validation. it doesn't matter anything, only DOM validation matters.
-
mbillard almost 15 yearsIt can all be simplified using CSS sprites, maybe I'll update my answer.
-
mbillard almost 15 yearsThere, I updated the code to use a sprite sheet.
-
nickytonline over 14 years@mcaulay - Does it also support giving the box a shadow?
-
Ben Regenspan over 14 yearsCurvyCorners (curvycorners.net) and ShadedBorder (ruzee.com/blog/shadedborder) do manage to support anti-aliasing. There are also ways of using these sans extra markup, you can implement with a class and then the markup is added dynamically to the classed elements. That said, the more I use them the more you seem right...they're useful for prototyping but add a lot of extra weight to the DOM. Now that I've got mine adjusted the way I want, I plan to convert them to images.
-
Jitendra Vyas over 14 years@vsync but it's not well supportedin IE8 dillerdesign.com/experiment/DD_roundies/#supported_browsers
-
Matthew Olenik over 14 yearsThis looks like the way to go.
-
ChenZhou about 14 yearsI'd definitely use this approach - let's handle things as they are. If someone doesn't use a browser (i.e. IE), they deserve to see rounded corners as angular. :)
-
allyourcode about 14 yearsI want to vote this answer down, because it doesn't describe what the answerer (Jeff) thinks is the best way.
-
allyourcode about 14 yearsThis answer doesn't contain nearly enough details for someone to implement what the answerer is thinking.
-
allyourcode about 14 yearsCan you add a brief explanation of what a behavior file is? I think person looking for an answer to this question is not probably going to be familiar with those.
-
allyourcode about 14 yearsI want to find the part of the YUI page that you're talking about, but none of the bullet points I've looked at say that trying to get rounded corners is a bad idea. Can you be more specific? I found one part that discusses talks about how you have to set your widths differently when using rounded corners, but doesn't discourage it.
-
aruno about 14 years@allyourcode - i don't see it either anymore. i remember making this post but not exactly what they said. however it looks like in that second link above they mention removing IE support for 1px rounded corners. but in anycase this post was a little tongue in cheek since theyre talking only about 1px corners which i think is a tremendous waste of time! i do remember at the time i wrote this being frustrated in general with rounded corners, and settled on corner images
-
mickthompson about 14 yearsuseful link if you decide to go with CSS3 (like SO) border-radius.com (another ref. css3.info/border-radius-apple-vs-mozilla)
-
evanmcd over 13 yearsSeems the behavior file link is dead
-
evanmcd over 13 yearsjquery roundcorners doesn't support IE 8.
-
Krishna almost 13 yearsthird link is not working cssjuice.com/25-rounded-corners-techniques-with-css
-
bharal over 12 yearsthis completely worked for me, along with defining the -moz etc etc corners for other browsers.
-
Matt Browne over 11 yearsCSS3 is good for most situations, if you don't mind it falling back to square corners for older browsers. But sometimes there are graphic design requirements for how the rounded corners should look that go beyond what you can do with
border-radius
. For those cases I recommend an approach like this one: github.com/stubbornella/oocss/wiki/Module. The only thing I don't like about the linked example is the use of the<b>
tag...I think a<span>
tag would be more appropriate.