chris barr


Detect browser support for CSS properties

Adding shadows or rounded corners to any element is way more trouble than it should be, especially if you’re tried out some of the newer CSS3 properties. A simple two lines of CSS can get you these amazingly gorgeous effects, but as expected they aren’t supported in every browser (I’m looking at you Internet Explorer!) Note: This post was updated on 9/21/2010 to include support for detection of these features in the Internet Explorer 9 beta!

Some older versions of browsers that do support these features don’t use the official W3C method and you have you use a vendor specific CSS property,like so:

    box-shadow:#000 3px 3px 8px;
    -moz-box-shadow:#000 3px 3px 8px;
    -webkit-box-shadow:#000 3px 3px 8px;

So, that of course won’t work in IE, and if you want to have rounded corners or shadows on IE you have to make some custom PNG images and align everything properly. It’s a lot of work, but it must be done if you want it to look consistent across all browsers. I realized today that ideally I want to do both, meaning that I want to use the pure CSS method when it’s available in the browser, and the old clunky way otherwise.

Sure I could just see what works in each browser and only allow certain versions of browsers, but I’d always have to come back and update that list as future browsers add support for these things. Instead, why not test and see if the browser supports these features first, and then decide what to do.

function supportsBoxShadow(){
  return s.WebkitBoxShadow!== undefined||s.MozBoxShadow!==undefined||s.BoxShadow!==undefined||s.boxShadow!==undefined;
function supportsBoderRadius(){
  return s.WebkitBorderRadius!== undefined||s.MozBorderRadius!==undefined||s.BorderRadius!==undefined||s.borderRadius!==undefined;

Those functions will return true if the CSS property is supported in the browser, and not just the W3C property, but the vendor specific property as well. If there’s some other property you want to test, follow the same pattern and just use CamelCase and remove the hyphens.

So putting this into practice is pretty simple, assuming we use the CSS property defined above.

    //Woo! We don't have to do anything! Css does all the work!
    //Write your complex code here to position your PNG shadows.... :(