Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one target IE7 and IE8 with valid CSS?

I want to target IE7 and IE8 with W3C-compliant CSS. Sometimes fixing CSS for one version does not fix for the other. How can I achieve this?

like image 972
Wasim Shaikh Avatar asked May 02 '09 05:05

Wasim Shaikh


People also ask

Does IE8 support CSS?

Most notable is that IE8 Supports all of CSS2.

How do I add specific CSS to Internet Explorer?

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

Explicitly Target IE versions without hacks using HTML and CSS

Use this approach if you don't want hacks in your CSS. Add a browser-unique class to the <html> element so you can select based on browser later.

Example

<!doctype html> <!--[if IE]><![endif]--> <!--[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 IE 9 ]>    <html lang="en" class="ie9">    <![endif]--> <!--[if (gt IE 9)|!(IE)]><!--><html lang="en"><!--<![endif]-->     <head></head>     <body></body> </html> 

Then in your CSS you can very strictly access your target browser.

Example

.ie6 body {      border:1px solid red; } .ie7 body {      border:1px solid blue; } 

For more information check out http://html5boilerplate.com/

Target IE versions with CSS "Hacks"

More to your point, here are the hacks that let you target IE versions.

Use "\9" to target IE8 and below.
Use "*" to target IE7 and below.
Use "_" to target IE6.

Example:

body {  border:1px solid red; /* standard */ border:1px solid blue\9; /* IE8 and below */ *border:1px solid orange; /* IE7 and below */ _border:1px solid blue; /* IE6 */ } 

Update: Target IE10

IE10 does not recognize the conditional statements so you can use this to apply an "ie10" class to the <html> element

<!doctype html>     <html lang="en">     <!--[if !IE]><!--><script>if (/*@cc_on!@*/false) {document.documentElement.className+=' ie10';}</script><!--<![endif]-->         <head></head>         <body></body> </html> 
like image 195
potench Avatar answered Sep 21 '22 21:09

potench


I would recommend looking into conditional comments and making a separate sheet for the IEs you are having problems with.

 <!--[if IE 7]>    <link rel="stylesheet" type="text/css" href="ie7.css" />  <![endif]--> 
like image 41
scragar Avatar answered Sep 24 '22 21:09

scragar