Why i preffer hacks over separate css files for IE:
Disadvantages:
Below is list of CSS hacks. I don't have time to explain how it works. Just use it.
Please notice that newest hack are on top of the page and some older hack may be invalid now. Some hack are really old (for netscape 4 or opera 7) so use it wisely.
This is variant of * html hack for IE6 but this one works only in IE7.
Text will be green in IE7 and blue in other browsers.
.test { color: blue; }
*+html .test { color: green; }TESTSUITE
This is modification of hack 5. Ie 7 supports child selectors but not when there is an empty comment after it.
Text will be blue in IE7 and below and green in other browsers.
.test { color: blue; }
html >/**/body .test { color: green; }TESTSUITE
Text will be green in Internet Explorer 9. Red in other browsers. It will not work for background or font-*.
.test{color:red;}
:root .test{color:green \ ;}TESTSUITE
Text will be green in Internet Explorer 8 and 9. Red in other browsers. It will not work for background or font-*.
.test{color:red;}
.test{color:green \ ;}TESTSUITE
On the web you can find hack for Internet Explorer 9 :root .test {color: green\0;}. But be careful. This declaration will work not only in IE9 but also in Opera (tested in 10.63).
You can replace \0 with other chars (see hack 17) but then it will not work for background.
Text green in Internet Explorer 9 and Opera.
.test{color:red;}
:root .test {color: green\0;}TESTSUITE
And here is version for IE9 only:
.test{color:red;}
:root .test {color: green \0;}TESTSUITE
in firefox 4 you can use :-moz-any() grouping selector. Inside :-moz-any() you can use simple selectors. :any() grouping element cannot contains combinators.
In firefox 4 text will be green. Red in other browsers.
p.test{color:red;}
:-moz-any(p.test}{color:green;}TESTSUITE
Text will be red in Opera7+, Firefox 3.5+, Chrome4+, IE9. Green in other browsers (also in Internet Explorer 8 but not in Internet Explorer 9).
This is old hack but few years ago it was used only for Opera 7+. Now it is used to filter IE.
.test{color:green;}
@media all and (min-width: 0px){
.test{color:red;}
}TESTSUITE
Text will be green in firefox>1.5 and red in other browsers.
.h21{color:red;}
@-moz-document url-prefix(){
.h21{color:green;}
}TESTSUITE
In Internet Explorer 7 and below text will be red. Text will be green in other browsers.
.h20{color:green;}
body*.h20{color:red;}TESTSUITE
In Internet Explorer 7 and below text will be green and red in other browsers.
.h19a{color:red; color: green !important!;}TESTSUITE
.h19b{color:red; color:green !inIE;}TESTSUITE
Text red underlined in IE < 8 and green, bold, without underline in other browsers.
div {color:green; font-weight:bold;}
div\ {color:red; text-decoration:underline;}TESTSUITE
This is modification of \9 hack (No 15). I noticed that \9 can be replaced by any other not printable sign. But be carefull with \0. It differs from others.
In Internet Explorer <9 text will be pink, undelined, bold and with yellow background.
In Internet Explorer 9 text will be pink, undelined, bold without background.
In other browsers text will be blue, without underline, no bold without background.
This cannot be considered as IE9 hack because this hack doesn't work only when used for background. Also sometimes you may need to add space between value and \, eg font-size:20px \d;.
\0 works with background in IE9 but it also works in Opera (10.5) so it unsafe. \0 not works in ie6.
Attention! Using this hack with font-family can be dangerous. See second example.
p{
color:blue;
color: pink\9;
text-decoration:underline\a;
font-weight:bold\b;
background:yellow\d;
border:1px solid\0; /*This works in IE7, IE8, IE9, Opera 10.63).*/
}TESTSUITE
Dangerous use with font-family:
p{font-family:"courier new"\d;} /* "Courier New" also in Chrome and Opera but not in Firefox */
p{font-family:courier new\d;} /* "Courier New" also in Firefoksie but not in Chrome and Opera */
p{font-family:courier new, \d;} /* "Courier New" also w Firefox, Chrome i Opera */
/* bonus */
p{font-family:courier\ new;} /* "Courier New" in Firefoksie, Chrome and Opera but not in IE*/Text red only in Internet Explorer 8, green in other browsers (also in IE7 or IE9).
p{color:red;}
p,#ie8#bug{color:green;}TESTSUITE
Text green in Internet Explorer 7 and above, red in other browsers. Remove comment to make text green also in IE6.
p{color:red; color /*\**/: green\9;}TESTSUITE
Hacks for goole Chrome and one for Opera. Text will be red in Google Chrome, green in other browsers.
/* will be red only in opera<10 and google chrome */
#test2{
color:green;
-bracket-:hack(;
color:red;
);
}TESTSUITE
/* will be red only in google chrome */
#test3{
color:green;
(-bracket-:hack;
color:red;
);
}TESTSUITE
/* will be red only in google chrome */
#test4{
color:green;
-bracket-:hack[;
color:red;
];
}TESTSUITE
Hack for Google Chrome. In Google Chrome text will be green and red in other browsers.
#item .element{color:green;}
@media screen and (-webkit-min-device-pixel-ratio:0) {
#item .element{color:red;}
}TESTSUITE
Long list of hacks for Internet Explorer 7. All will work in this browser.
Original list was publised by Anne van Kesteren on his blog.
.ie7_hacks{
`background:red;
~background:red;
!background:red;
@background:red;
#background:red;
$background:red;
%background:red;
^background:red;
&background:red;
*background:red;
(background:red;
)background:red;
=background:red;
+background:red;
[background:red;
]background:red;
{background:red;
|background:red;
,background:red;
<background:red;
.background:red;
>background:red;
/background:red;
?background:red;
}In IE5, IE6, IE7(!) text will be blue, green in other browsers.
body{
color: green;
* color: blue;
}TESTSUITE
In IE5.x/WIN (don't know if works for Mac) text will be red, blue in other browsers:
.menu{
width: red;
width: /**/blue;
}Text will be green in Internet Explorer 5 for Mac. Red in other browsers.
p{color: green;}
/*hack for ie5\*/
p{color: red;}
/* end */Text will be red in Internet Explorer 6, green in other browsers.
IE6 thinks that html element has parent. For other browsers html is root element so * html is invalid.
p{color: green;}
* html p{color: red;}TESTSUITE
Will work only in IE5.5, IE6:
Text will be red in Internet Explorer 5.5 and Internet Explorer 6, blue in other browsers.
.example{
color: blue;
_color: red;
}Those style wouldn't work in IE 5.x (Attention: those will work if backslash is first sign, eg: \width: 300px; will work):
.example{
back\ground: red;
}
.example{
font\t: bold 15px arial;
}
.example{
bord\er: 1px solid red;
}IE6 doesn't support child selectors (">"), so text will be red in IE6 and green in other browsers.
p{color: red;}
div#mydiv{color: red;}
html>body p {background-color: green;}
html>body div#myDiv{background-color: green;}TESTSUITE
This hack worked in Opera7+ but now it is supported by all browsers except for IE, so i moved this hack up. See hack 22.
Will not work in older browsers (Netscape 4) and Opera below version 8.
body{
background-color: green;
voice-family: "\"}\"";
voice-family: inherit;
background-color: red;
}In IE 5, 6, 7.0beta2(earlier versions) text will be green, red in other browsers.
.example{
color: red !important;
color: green;
}Multihack sets red color for ie, blue for gecko (Firefox), pink for Opera > 8.5:
/*ie*/
#example{
color: red;
}
/*firefox(gecko)*/
body:not([foobar]) #example{
color: blue;
}
/*opera > v8.5*/
@media all and (min-height:0px;){
body #example{
color: pink;
}
}I don't know if this can be considered as "hacks" but I here is code:
<!--[if IE]>
Here goes code for IE only
<![endif]-->Possible syntax (you can change version number):
[if IE 6] - kod tylko dla IE6
[if IE 5.5] - kod tylko dla IE5.5
[if gte IE 5] - kod dla IE5 i wyższych
[if gt IE 5] - kod dla wyższych niż IE5
[if lte IE 5] - kod dla IE5 lub niższych
[if lt IE5] - kod dla niższych niż IE5
[!IE 7] - kod dla każdej wersji oprócz IE7Code for all browsers except for IE:
<!--[if !IE]>< -->
kod nie dla IE
<!-- ><![endif]-->[[http://msdn.microsoft.com/workshop/author/dhtml/overview/ccomment_ovw.asp|Microsoft o komatarzach warunkowych].