{"id":585,"date":"2010-04-15T17:11:11","date_gmt":"2010-04-15T22:11:11","guid":{"rendered":"http:\/\/cssnewbie.com\/?p=585"},"modified":"2010-04-15T17:11:11","modified_gmt":"2010-04-15T22:11:11","slug":"test-for-border-radius-support","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/test-for-border-radius-support\/","title":{"rendered":"Test for Border-Radius Support"},"content":{"rendered":"<p><a href=\"http:\/\/cssnewbie.com\/example\/border-radius-support\/\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2010\/04\/border-radius-support.png\" alt=\"\" title=\"Test your browser's border radius support.\" width=\"500\" height=\"150\" class=\"alignnone size-full wp-image-586\" \/><\/a><\/p>\n<p>Of all the fantastic magical CSS3 properties out there, border-radius was one of the first I really latched on to. It made sense to me from the start, and I could see immediate practical uses (at least, as far as a border can be practical). If you&#8217;re not using the border-radius property yet, be sure to <a href=\"http:\/\/cssnewbie.com\/easy-rounded-corners-with-border-radius\/\">check out my article on how to use it.<\/a><\/p>\n<p>Unfortunately, even though more advanced browsers started supporting border-radius years ago, not all browsers are up to speed yet. As of this writing, border-radius is supported in Webkit (Safari\/Chrome), Firefox, and rumor has it Konqueror, though I don&#8217;t have a Linux install so I can&#8217;t prove that. That leaves Opera and Internet Explorer out of the game \u2013 I&#8217;ve heard rumors that Opera will support border-radius soon, but the latest version is still a no-go.<\/p>\n<p>As I <a href=\"http:\/\/cssnewbie.com\/easy-rounded-corners-with-border-radius\/\">argued in my last article on the topic,<\/a> most of the places people should use rounded corners today fall into the realm of &#8220;progressive enhancement&#8221;: the site looks better with the element rounded, but it&#8217;s not the end of the world if someone sees a square corner. <\/p>\n<p>Of course, what should be true and what a client requires aren&#8217;t always the same thing. <!--more--><\/p>\n<p>I ran into this problem recently. I needed to create new buttons in CSS to replace dozens of image-heavy buttons. The buttons were all different widths but were otherwise very similar: background color, centered text&#8230; and rounded corners. <\/p>\n<p>I made quick work of the buttons for those browsers that support border-radius, but that left Opera and IE out of the fun. For them, I developed a different way of rounding the corners, but then I needed a quick way to tell which browsers to give the simple border-radius solution, and which to give the more complex solution.<\/p>\n<p>Enter jQuery. The following tiny script extends the jQuery.support object to include border-radius information:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">jQuery(function() {\n\tjQuery.support.borderRadius = false;\n\tjQuery.each(['BorderRadius','MozBorderRadius','WebkitBorderRadius','OBorderRadius','KhtmlBorderRadius'], function() {\n\t\tif(document.body.style[this] !== undefined) jQuery.support.borderRadius = true;\n\t\treturn (!jQuery.support.borderRadius);\n\t});\n});<\/pre>\n<p>That&#8217;s all it takes, folks. You can copy it, or download the script from here. And if you want to test it out, <a href=\"http:\/\/cssnewbie.com\/example\/border-radius-support\/\">here&#8217;s a quick and dirty example page.<\/a><\/p>\n<p>$.support.borderRadius is a Boolean: if the browser supports border-radius, it&#8217;s true, otherwise it&#8217;s false. For example, you could use it like this:<\/p>\n<pre lang=\"javascript\" escaped=\"true\" line=\"1\">$(function() {\n\tif($.support.borderRadius) { \n\t\talert('I can do rounded corners!'); \n\t} else { \n\t\talert('No rounded corners for me!'); \n\t}\n});<\/pre>\n<p>And now, because this is a tutorial blog, I&#8217;ll explain how the script works. The copy\/paste crowd can stop reading now. :)<\/p>\n<h3>How it Works<\/h3>\n<p>We&#8217;re piggybacking here on an existing object in jQuery called jQuery.support. It already contains lots of information about what the user&#8217;s browser does or doesn&#8217;t support. Testing for browser support is considered the heir of the &#8220;browser sniffing&#8221; practice. Instead of checking to see what browser someone is using, we instead test what that browser can and can&#8217;t do.<\/p>\n<p>So the first thing we do is add borderRadius to the support object, and set it to &#8220;false&#8221; by default.<\/p>\n<p>Next, we loop through an array of CSS properties using jQuery.each(). The array contains every vendor-specific version of border-radius, as well as the standard CSS3 version (which currently isn&#8217;t supported by anyone, but will be eventually). I&#8217;ve even included the Opera vender-prefixed version, just in case Opera adds support. <\/p>\n<p>For each style, we test to see if the browser understands the style by seeing what it&#8217;s set to. If it&#8217;s set to blank (or a value, for that matter), then the style is supported. If it&#8217;s undefined, that means the browser doesn&#8217;t understand the property: it doesn&#8217;t support that version of border radius.<\/p>\n<p>If we find a CSS property that the browser understands, that means it supports border-radius! We set borderRadius to be true and exit our loop. Otherwise, borderRadius remains false.<\/p>\n<p>And that&#8217;s all there is to it. Let me know what you think. If you have suggestions for improvement I&#8217;d love to hear them. And next week, I&#8217;ll try to post a tutorial on border-radius where I&#8217;ve used this script. <\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Rounded corners with border-radius are nice&#8230; when they work. Unfortunately, not all browsers support border-radius yet. Here&#8217;s a simple script that will let you test for border-radius support using jQuery. [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/test-for-border-radius-support\/\" title=\"Click to read 'Test for Border-Radius Support'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":586,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[165,176,184,193],"tags":[62,151,47,291],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/585"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=585"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/585\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/586"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}