Browser CSS hacks
I don't use CSS hacks anymore. Instead I use IE's conditional comments to apply classes to the body tag.
Nonetheless, I wanted to document every browser-specific css selector and style attribute hack I've seen. Plus there's no way to provide stylesheets to only Safari, I believe.
With these you'll be able to better target IE, Firefox, Chrome, Safari and Opera from within the css.
So here are go:
Comprehensive List of Browser-Specific CSS Hacks
/***** Selector Hacks ******/ /* IE6 and below */ * html #uno { color: red } /* IE7 */ *:first-child+html #dos { color: red } /* IE7, FF, Saf, Opera */ html>body #tres { color: red } /* IE8, FF, Saf, Opera (Everything but IE 6,7) */ html>/**/body #cuatro { color: red } /* Opera 9.27 and below, safari 2 */ html:first-child #cinco { color: red } /* Safari 2-3 */ html[xmlns*=""] body:last-child #seis { color: red } /* safari 3+, chrome 1+, opera9+, ff 3.5+ */ body:nth-of-type(1) #siete { color: red } /* safari 3+, chrome 1+, opera9+, ff 3.5+ */ body:first-of-type #ocho { color: red } /* saf3+, chrome1+ */ @media screen and (-webkit-min-device-pixel-ratio:0) { #diez { color: red } } /* iPhone / mobile webkit */ @media screen and (max-device-width: 480px) { #veintiseis { color: red } } /* Safari 2 - 3.1 */ html[xmlns*=""]:root #trece { color: red } /* Safari 2 - 3.1, Opera 9.25 */ *|html[xmlns*=""] #catorce { color: red } /* Everything but IE6-8 */ :root *> #quince { color: red } /* IE7 */ *+html #dieciocho { color: red } /* Firefox only. 1+ */ #veinticuatro, x:-moz-any-link { color: red } /* Firefox 3.0+ */ #veinticinco, x:-moz-any-link, x:default { color: red } /***** Attribute Hacks ******/ /* IE6 */ #once { _color: blue } /* IE6, IE7 */ #doce { *color: blue; /* or #color: blue */ } /* Everything but IE6 */ #diecisiete { color/**/: blue } /* IE6, IE7, IE8 */ #diecinueve { color: blue\9; } /* IE7, IE8 */ #veinte { color/*\**/: blue\9; } /* IE6, IE7 -- acts as an !important */ #veintesiete { color: blue !ie; } /* string after ! can be anything */
If you'd like to take a gander by yourself:
Test page with all these hacks present.
View the test page at browsershots.org
(Thx to Webdevout, Evotech, and Jeffrey, porneL, and commenters.)
I should point out I'm not including weird hacks like the voice-family ones or anything particularly ugly.
Somewhat related…
Here are the most concise browser sniffs I've seen. (Not that browser sniffing is good or anything…)
Update 2009.11.10 - Added #prop: value IE<=7 hack. I prefer it much less to *prop.
Update 2010.01.24 - Added prop: value!ie hack. Thx @miketaylr for the tip.
Follow me on twitter: @paul_irish
ie8 do not do ?
"/* IE 7 and below */"
Pretty sure *:first-child+html doesn't target below IE7, otherwise nice list.
html:first-child #cinco applies to Safari 2 as well (tested 2.04 at that time)
In addition to that list here are the ones I was using in my latest project:
html[xmlns*=""]:root <selector> = Applies: Safari 3.1, Safari 2.04
*|html[xmlns*=""] <selector> = Applies: Safari 3.1, Safari 2.04, Opera 9.25
:root *> <selector> = Hides from IE
* > <selector> = Hides from IE6
property /**/: value; = Hides from IE6
missing this one:
/* IE7/8 */
*+html #nueve { color: red }
Seen elsewhere :
ALL IE VERSIONS:
#somediv{
width: 100px\9;
}
IE8 STANDARDS MODE ONLY:
#somediv{
width /*\**/: 100px\9;
}
—-
credits : http://my.opera.com/dbloom/blog/2009/03/11/css-hack-for-ie8-standards-mode
I've managed to narrow all my css hacks down to only the property hacks. It is nice to see the other options here, but I hope I never have to use them.
Great list to have just in case! Thanks for sharing
Check that in Safari.I write in my css code u specify above like
html[xmlns*=""] body:last-child #login-box_one p.last { position:relative; top:-8px;}
but it does not affect on my xhtmlfile. my safari(on windows) vesrion is "Safari 4 Public Beta(528.16)
@travis, I'm not seeing
*+html foowork in any browser. Hmm…@technova, In my tests
width /*\**/: 100px\9;also target IE7 in addition to IE8 standards. I left a comment on the Opera blog.@akbar You're right, it no longer works in Safari 4 beta. Thx. :)
Thanks everyone for their submissions and changes.
Then how can fix the css bugs in safari. any solution. In other browser issues i solved like Ie7 bugs. i use the ( /* IE7 */*:first-child+html #dos { color: red } ) shown in above. in other words how can i fix the safari problem . please this is very urgent requirement
@akbar, you can probably use the safari/chrome hack above. (I assume it affects chrome as well)
This will catch Safari3, Safari4, Chrome1, Chrome2. (Not Safari 2)
Hi, What has happened to the Attribute Hacks?
@janogarcia , Hah. Thanks. In my update I stripped the hacks out of this post. They were present in the demo page though. Fixed now!
Thanks for the quick fix!
I use this to select for IE7 only:
#id element.class{
color: blue;
.color: red}
thanks friend i have solved my problem of safari
Great Hacks for the different browsers. But one thing, is there any hack that only works for Opera9+..
@Phil Ricketts, using a period in the attribute hack targets both IE6 and IE7. So it's equivalent to the *attribute which is more common.
@Ashwin SM, I haven't yet seen one for Opera 9. There claims to be one here, but on my demo page, I don't see it catching in Opera.
@Ashwin SM, For Opera 9 you could try this:
Add a class of 'hypen-ated' to your body tag.
This technique is courtesy of javascriptkit.
Here's an alternative approach, that uses server-side User-Agent sniffing and then puts classes on the BODY tag, such as "Browser-Engine-WebKit" or "Browser-DesktopOpera-10-0-1":
http://ernstdehaan.blogspot.com/2009/06/combining-css-and-user-agent-sniffing.html
@Ernst de Haan Ernst, while it's true your technique will help target individual browsers, it negates the ability to do page-level caching. So for high traffic sites, I don't consider it as useful.
thanks for posting this !!
Hi Paul, I found this page via David Bloom's blog. Thanks so much for this doubleplusgood page!
Thanks Paul, those Safari hacks come in handy!
Defintively a great article Paul. Thanks a lot.
Just a question regarding Opera 10.01 since I didn't see any hack for it in your demo page. Is there any one for this browser?
FYI - - the webkit selector hack needs 'body' before basic tags such as h4 tags in order for them to be targeted accurately. hope that helps!
Hi,
I happened to see your post and find it very informative. I would like to share a link where a software engineer has share a tip on "CSS hack for Safari Browser".
Here is the link:
http://www.mindfiresolutions.com/CSS-hack-for-Safari-Browser-672.php
Hope you find it useful and of assistance.
Thanks,
Bizz
@Bizz, The one described is captured here. thanks!
Thanks for the great summary of available CSS hacks. One correction:
Your code sample matches also IE7 because you used a ";"
#veinte { color/*\**/: blue\9; }
If you want to address only IE 8 you have to omit the ";".
#veinte { color/*\**/: blue\9 }
What I also found out. With this hack it is not possible to add more than one rule inside the brackets. If you need more CSS rules, than you have to write each in a separate definition.
#veinte { color/*\**/: blue\9 }
#veinte { background-color/*\**/: yellow\9 }
Paul,
Thanks for this comprehensive list. I didn’t know about
!ie, that one’s pretty cool.Hear hear. I used to have one extra CSS file for every different version of IE. However, to reduce the amount of additional HTTP requests for IE, you could just use one
ie.cssfile, containing the CSS for IE6, IE7 and IE8, and use the CSS hacks you mentioned in there. Performance-wise, this seems to be the best solution.Thank You For This Hacking code.But I want userfrndly hacking code of google chrome.If You Have then plz publish.
After several days I found your page and since I am a illiterate developer
or amateur webpage builder I ask:
Where shoul I put the
/***** Selector Hacks ******/
list that you offer here.
Thank you hoping to hear from you
jorge
Thkxs again. I will do use it and
let you know if I got it right.
jorge
Just wanted to say thanks for the efforts. This article saved me a lot of stress.
Keep up the good work.
I am now using these hacks (all in one CSS file) instead of three different files (using conditional commands) for the different IE versions:
IE < = 8:
foo { property: value\9; }IE < = 7:
foo { *property: value; }IE6:
foo { _property: value; }Thanks for this awesome collection, Paul!
Hi, just found out by accident:
#.veinticinco, x:-moz-any-link, x:default { color: red }
This will only match in Firefox 3 on Windows, not Mac! Dunno if it'll work with ID's, this would work for the class .veinticinco