CSS Specific for Internet Explorer 154
As much as we don't like to deal with the IE bugs, we still have to face it because your boss and visitors are still using Explorer. It gets frustrating when different versions of Explorer displays web pages differently due to the inconsistent rendering engine. We typically use IE conditional comments to fix the IE issues. But there are more ways than the conditional comments...
#1 IE Conditional Comments
IE conditional comment is probably the most commonly used to fix the IE bugs for specific versions (IE6, IE7, IE8). Below are some sample code to target different versions of Internet Explorer:
<!--[if IE 8]>= IE8<!--[if lt IE 8]>= IE7 or below<!--[if gte IE 8]>= greater than or equal to IE8
<!--[if IE 8]>
<style type="text/css">
/* css for IE 8 */
</style>
<![endif]-->
<!--[if lt IE 8]>
<link href="ie7.css" rel="stylesheet" type="text/css" />
<![endif]-->
#2 CSS Rules Specific to Explorer (IE CSS hacks)
Another option is to declare CSS rules that can only be read by Explorer. For example, add an asterisk (*) before the CSS property will target IE7 or add an underscore before the property will target IE6. However, this method is not recommended because they are not valid CSS syntax.
- 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.
.box {
background: gray; /* standard */
background: pink\9; /* IE 8 and below */
*background: green; /* IE 7 and below */
_background: blue; /* IE 6 */
}
#3 Conditional HTML Class
The third option, which was founded by Paul Irish, is to add an CSS class with the IE version to the HTML tag by using IE conditional comments. Basicially, it checks if it is IE, then add a class to the html tag. So to target specific IE version, simply use the IE class as the parent selector (eg. .ie6 .box). This is a clever way and it doesn't cause any validation errors.
<!--[if lt IE 7 ]> <html class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html> <!--<![endif]-->
Nice css tips,thanks for sharing.
I like #3 a lot! Cheers!
I never see number 3, you can put it for Internet explorer 7,8 and 9? I didn’t know that
I have seen the #3 in html5boilerplate I think.
There are very useful and problem solving tips in this article.IE is really becoming a headache for developers for the same reason that is mention here that people are still using it.
this one saves me lot of time while implementing html pages….
Thank you
Tips 2 & 3 have saved us considerable time on a current web design and development project. Much appreciated, team @ perceptant101.com
Nice write up thanks – hadn’t considered solution #3 before , but it seems really elegant and simple and leverages the benefits of the cascading nature of css.
there is merit in all three but I agree with the post above me that number 1 is the best, it has the least likely chance of having issues.
I will add though that I am so sick of cross browser issues, why they cant just get along is beyond me lol
Trevor Seabrook
Mythic Tech – Toronto Web Design
Except for no. 1, the other 2 solutions are not so clean – first, better to avoid hacks as they make your code less cleaner (and once IE6 is old enough not to require anymore our support & attention, it will be rather tedious to go through a big css file and start cleaning all the useless code.)
No. 3 can actually be tweaked to make it a bit easier – instead of giving a class to the html, you can wrap your whole html code in a for example, and then in your css you’d use it like this:
#ie6 .example {etc}
However, the issue I have with both these options is that they turn your code into an ugly soup (and no. 2 makes your css invalid). The first option allows for a nice & clean CSS, while all the IE specific food goes in a separate file.
forgot to put some spaces in my example! – what I meant was:
… you can wrap your whole html code in a for example
I dislike internet explorer, whatever version it will always still ugly.. i would rather told my boss how ugly IE is.
I know this hack only for IE8: font-size: 12px/; using this / before semicolon.
Thanks a lot for sharing the CSS insights and IE, the unavoidable monster
Hey great article thanks! Have not heard of option 2 before, shame its not valid really.
Nice article, also @Ariakan wrote in comments great method.
Though Example 3 seems like a good solution, I wonder how well it would work if you have a large site where there are a lot of styles and loading all variations for all browsers may simply put a dent on performance.
I like Example 1 because it loads only when needed. I suppose if the site is small enough, it may not be an issue.
Good post for sure..
nice article.
thanks
Thanks for sharing! We use #3. We also told our clients We will stop supporting IE7 or below.
Thank you. This helped fixed my CSS issues on IE browsers.
Looking for high quality web design services and CMS development team? I tried http://www.webdesign.8rf.org they also have cheap unlimited website hosting plan.