Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check browser CSS property support with JavaScript?

Is it possible in JavaScript to know if a CSS property is supported by the client browser? I'm talking about the rotation properties of CSS3. I want to execute some functions only if the browser supports them.

like image 783
mck89 Avatar asked Aug 27 '09 18:08

mck89


People also ask

How do you check if CSS property is supported?

Use the new CSS @supports feature. You can do this from CSS and also from JavaScript. JavaScript way: The supports() method is provided in the CSS API to do this.

Does CSS support JavaScript?

CSS is applied to both HTML and XML. JavaScript is applied on HTML only. There is more scope for Optimization in the case of CSS. JavaScript does not support these types of optimizations because it does not have access to those APIs.

Can we change CSS property using JavaScript?

With JavaScript, we are able to set CSS styles for one or multiple elements in the DOM, modify them, remove them or even change the whole stylesheet for all your page.


2 Answers

I believe you can do it this way:

if ('WebkitTransform' in document.body.style 
 || 'MozTransform' in document.body.style 
 || 'OTransform' in document.body.style 
 || 'transform' in document.body.style) 
{
    alert('I can Rotate!');
}
like image 66
jamland Avatar answered Oct 20 '22 15:10

jamland


There is a new DOM API CSS.supports for that purpose. FF, Opera (as supportsCSS) and Chrome Canary already implement this method.

For cross-browser compatibility you can use my CSS.supports polyfill

Example:

CSS.supports("display", "table");//IE<8 return false

But, you still need to specify browser prefix for prefixing css properties. For example:

CSS.supports("-webkit-filter", "blur(10px)");

I suggest to using Modernizr for feature-detection.

like image 39
termi Avatar answered Oct 20 '22 13:10

termi