Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 500
leora Avatar asked Dec 12 '10 16:12

leora


People also ask

How do I set IE to specific CSS?

#2 CSS Rules Specific to Explorer (IE CSS hacks) IE8 or below: to write CSS rules specificially to IE8 or below, add a backslash and 9 ( \9 ) at the end before the semicolon. IE7 or below: add an asterisk ( * ) before the CSS property. IE6: add an underscore ( _ ) before the property.


2 Answers

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.

like image 154
John Fisher Avatar answered Sep 21 '22 21:09

John Fisher


There is no official conditional comment syntax for CSS. There are only conditional HTML and JavaScript comments, so get creative with those (John Fisher shows one way).

You could emulate conditional CSS by using the very well-known CSS hacks, but I'd rather not. They could break or otherwise work unexpectedly at any arbitrary version of IE, because hacks are meant to exploit bugs in IE's handling of CSS.

like image 21
BoltClock Avatar answered Sep 22 '22 21:09

BoltClock