Search code examples
csstestingbrowserconditional-comments

Can I insert a browser test or conditional comments in a CSS file?


Hi
I have a CSS that applies different styles according to the browser, and I want to test for the browser (if browser is not IE) in the CSS file itself (not in the HTML or PHP file).

How can I do it? Should I use this:

<!--[if !IE]>
<style>
//code
</style>
<![end if]-->

in the CSS file???


Solution

  • These conditional comments are IE-only. So checking for !IE won't do anything to other browsers ;)

    You could inject the CSS with Javascript. I'm just improvising, something like this might do (jQuery):

    if( !$.browser.msie ) {
      $("#someLinkElement").attr( "href", "webkit.css" );
    }
    

    Edit: the condition holds for non-IE browsers now