Personal CSS Hacks for IE6, IE7, IE8, IE9, IE10
Are CSS hacks an evil? Yes, of course.
But we will use them, while browsers like IE6, IE7 and IE8 will exist and while Microsoft will not create a normal browser, which will stand on equal with modern browsers like FireFox, Safari, Chrome and Opera.
We, web developers, are constantly faced with deficiencies of Internet Explorer, and CSS hacks allow to overcome them in most cases.
In this post I would like to group the personal CSS hacks for each of the 5 popular versions of IE. Most likely you already know the CSS hacks for IE6 and IE7, but for IE8 and IE9 probably not. Therefore the focus of this article is on a personal CSS hack for Internet Explorer 8 and Internet Explorer 9.
CSS hack for Internet Explorer 6
It is called the Star HTML Hack and looks as follows:
* html .color {color: #F00;}
This hack uses fully valid CSS.
CSS hack for Internet Explorer 7
It is called the Star Plus Hack.
*:first-child+html .color {color: #F00;}
Or a shorter version:
*+html .color {color: #F00;}
Like the star HTML hack, this uses valid CSS.
CSS hack for Internet Explorer 8
@media \0screen {
.color {color: #F00;}
}
This hacks does not use valid CSS.
CSS hack for Internet Explorer 9
:root .color {color: #F00\9;}
This hacks also does not use valid CSS.
Added 2013.02.04: Unfortunately IE10 understands this hack.
CSS hack for Internet Explorer 10
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
.color {color: #F00;}
}
This hacks also does not use valid CSS.
Demo
I’ve created a simple demo. Take a look.
CSS hacks are never a good solution…
Of course there is conditional comments but it’s not very usefull with big CSS…
I prefer this clever use of conditional comments, wich is still improvable like this:
<!--[if IE 6]><![endif]--><!--[if IE 7]><![endif]-->
<!--[if IE 8]><![endif]-->
<!--[if !IE]>--><!--
What do you think about this?
I would never use this method, because the HTML becomes ugly. Moreover, on my practice I have not met the need for simultaneous connection of stylesheets for all 3 version of IE.
I don’t see a problem with extra HTTP requests while using conditional comments.
> I would never use this method, because the HTML becomes ugly.
Ok, I understand
I make a pastie because Wordpress removes a lot of tags… ;-)
No problem with extra HTTP requests using conditional comments, it’s just sometimes difficult to maintain CSS with conditional comments because CSS parts are in different files.
THank you very much.
It was very helpful for me.
@Jeremy: against the Non standard rules of IE versions and their inconvenience , it is a great and fast solution indeed.
I prefert to use this:
<!–[if IE 6]><![endif]–>
but everyone has the way the he loves…
Hello! I saw most of the png image file in the site have failed due to IE 6! you just help me to fix for each browser are not
Thank for share it, it’s really useful. By the way, it’s ugly but very fast to change some code that mess all our css-fashion code ^^
Darn, i really hate IE.
hey guyz this css styles is not working for
is there something i need to include such as header information
my html page just contains a single div element
This will work in IE-Tester (myDebugbar). Conditional not (the <!– thing –>)
Thanks for this, it works like a charm.
Hi All..
Conditional comments are good in a sense to overcome the non-standard problems of IE versions, but there are hidden issues not known to the UI/Web designer when we develop pages with JSF. JSF has got configuration settings in WEB XML file to SKIP comments, so the CONDITIONAL comments never works…! :(
I came to this page via Thomas Fuchs’s blog and I am still slightly aghast that, when we have a 100% certain way of handling IE ’speshulness’ using conditional comments, people still advocate hacks which can have unforeseen consequences now and in the future.
All to save a bit of space in the html? Seems fack to brunt to me.
Good one , All at one place
As a UI Developer I’m constantly faced with dealing with multiple browsers and all their lovely rendering quirks.. and, I absolutely hate CSS hacks, especially ones that invalidate the killer code I’ve written :) So, this is what I use:
http://bastian-allgeier.de/css_browser_selector
This is obviously for PHP but there are similar scripts and plugins available for other languages. With this, I’m able to not only target browser level rules, but I can also target different OS’s, various browser versions, browser engines or a combination of all three! This makes all our CSS valid, clean and highly readable.
So, for these examples above, we would simply write the CSS rule for the browser we wanted to target:
.color {color: #777;} /* for all browsers */
.ie6 .color {color: #C39;} /* for IE6 */
.ie7 .color {color: #66F;} /* for IE7 */
.ie8 .color {color: #0FC;} /* for IE8 */
Since our team mainly focuses on web applications, cross browser compatibility is key and we have successfully used this within our Zend based projects as well as others.. hopefully this will just add to the pool of knowledge you’ve got going here.. and thanks for the great posts! I’m very happy to have come across your blog ;) BTW, one lead programmer is from Russia :D
Hope this helps! :D
Jesse M
Revive
Brilliant tool – hadn’t heard/thought of this before. Now built into my default templates and will be very handy indeed! Cheers.
Amazing. Helped me a lot
thanks revive this is very helpful.
First off, if you code your css correctly, you should very rarely have to use any sort of hack for IE7 and up. That being said, it’s ok to have style degradation for older browers.
The main reason for using conditional browser style sheets over hacks revolves around that hacks make your style sheet invalid. Just run it through a validator and you’ll see what I mean.
http://jigsaw.w3.org/css-validator/
@ John,
I completely agree.. hence the reason our team has chosen to ‘target’ styling to specific browsers IF we need/want to.. or as you mention we follow the ‘old browser’ degradation pattern. Well put !
We all hate Microsoft. But to my principal purpose for writing, if you care to, you can contact me and I would be glad to describe corrections to your English. Many of the phrase and word problems that you have, I suspect, will be learned fairly quickly.
For example, I’ve listened to Russians who generally are fluent in English yet they will say something like,”I went to bank” instead of,” I went to the bank”. There are no counterparts to the articles ‘a’ and ‘the’ in Russian therefore they naturally leave it out unless they become conditioned to including it.
These are sooo useful when coding myspace pages, thank you!!!
great ie8 hack! thx
You saved me a lot of time…
Thank you very much boy!
Thank s ton for these IE hacks…IE8 one nice. but it needs very often. Thanks again
very nice support for css developer thanks
you can use \9 before semi colon, this will apply to ie6, ie7, ie8 (havent really tested ie9)
eg:
.red { color: red\9; }thanks for tips, actually
.red { color: red\9; }is what I was lookin for :PFor <=IE8 only, you should use
.red { color: red\9; }Since
.color {color: red/;}works in IE9 as well.
Wow! Thanks man :) To find a simple hack for IE8 is AMAZING! YES I hate hacks too… but to add conditional comments for just ONE style overwrite is overkill…
http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
Thank you very much.
Thanks a lot, very useful post.
Thank you for sharing. I would like to ask if we need more hacks for ie9. I am using microsoft expression view for ie but I don’t have the option to view is in ie9. Any ideas?
Thanks
Thank you very much.
Thank you. This worked for me perfectly.