Home > Article > Web Front-end > How to Target IE7 and IE8 with CSS: A Comprehensive Guide
To achieve targeted styling for IE7 and IE8 without compromising W3C compliance, employing explicit browser-specific classes is an effective method. This eliminates the need for unreliable CSS hacks.
Add HTML classes to the element based on the browser:
<code class="html"><!doctype html> <!--[if lt IE 7 ]> <html lang="en" class="ie6"> <![endif]--> <!--[if IE 7 ]> <html lang="en" class="ie7"> <![endif]--> <!--[if IE 8 ]> <html lang="en" class="ie8"> <![endif]--> <!--[if (gt IE 9)|!(IE)]><!--><html lang="en"><!--<![endif]--></code>
This allows you to target specific browsers in your CSS:
<code class="css">.ie6 body { border:1px solid red; } .ie7 body { border:1px solid blue; }</code>
Alternatively, you can use CSS hacks to target IE versions:
Example:
<code class="css">body { border:1px solid red; /* standard */ border:1px solid blue; /* IE8 and below */ *border:1px solid orange; /* IE7 and below */ _border:1px solid blue; /* IE6 */ }</code>
IE10 does not recognize conditional statements. To target it, use this script:
<code class="html"><!doctype html> <html lang="en"> <!--[if !IE]><!--><script>if (/*@cc_on!@*/false) {document.documentElement.className+=' ie10';}</script><![if lt IE 9]><![endif]--> <head></head> <body></body> </html></code>
The above is the detailed content of How to Target IE7 and IE8 with CSS: A Comprehensive Guide. For more information, please follow other related articles on the PHP Chinese website!