Search code examples
cssinternet-explorerconditional-comments

Is there any way to do "if IE" inside of a CSS file?


I know you can put conditional statements to load different CSS files if you are in Internet Explorer, but can you do this inside of a CSS file?

I have a situation where I want the right margin to be 20 if it's Internet Explorer but 10 if it's any other browser.


Solution

  • The easiest way to handle this is with conditions in your HTML that put a div with a specific id around the rest of the page.

    <!--[If IE 8]>
    <div id="IE8">
    <![endif]-->
      .
      .
      .
    <!--[If IE 8]>
    </div>
    <![endif]-->
    

    Then, your CSS can do this:

    #IE8 div.whatever { ... }
    

    This sort of makes all those CSS hacks unnecessary.