简体   繁体   中英

target ie8 and ie9 only (not ie7)

I have an simple page, and everything works fine except ie8 and ie9 moves one of my elements 1px to the right, and its significant b/c it results in a 1px white line straight on black background. Horrible. I've tried a lot of css hacks, and ie conditional comments but things like <!--[if gt IE 7]> or <!--[if gte IE 8]> aren't working. Only <!--[if gte IE 7]> is recognised, targets ie7 as well - but ie7 renders the page pixel perfect. So I've tried to rollback the changes for ie7 with additional comment <!--[if IE 7]> but this killed the fixes in later ie's. Ie8 and 9 seem to think they are ie7.

My code so far

<!--[if gte IE 7]>
<style>
#promo {margin-left:-1px;}
</style>
<![endif]-->

EDIT: Solved, It seems it is a bug in IETester, anything works fine in properly installed Explorers.

I would recommend using an actual stylesheet with the IE conditional comments.

This article gives a good overview, but I'd avoid the hacks section.

http://css-tricks.com/how-to-create-an-ie-only-stylesheet/

If it's just one thing you need to bump, you can also use:

<!--[if IE 7 ]>    <html class= "ie7"> <![endif]-->
<!--[if IE 8 ]>    <html class= "ie8"> <![endif]-->

This will give the html element the class of .ie7 or .ie8 depending on what they're using or what you've specified. You can then target whatever you want to override, like:

.ie7 #promo {margin-left:-1px;} or

.ie8 #promo {margin-left:-3px;} and target them individually. You could also specify a rule for IE 9.

And include it in your main stylesheet, near the bottom. This saves you from having to make a lot of different stylesheets.

As far as IE9 not rendering as IE9, I think Internet Explorer will actually fallback into a previous renderer if it's installed for reasons that are totally unknown to me. I think Paul Irish mentions it on his talk on the HTML5 Boilerplate on the onTwik website.

They use some code in their .htaccess file to force IE to use the most recent rendering engine, and will actually force IE to use the Chrome engine if it's installed:

<IfModule mod_setenvif.c>
  <IfModule mod_headers.c>
    BrowserMatch MSIE ie
    Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
  </IfModule>
</IfModule>

Are you doing anything that forces IE8/9 to go into Compatibility View? If so, you could be unwittingly telling those browsers to think of themselves as IE7, at least as far as it matters for interpreting those conditionals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM