Home > front-end development > Browser CSS hacks

Browser CSS hacks

April 15th, 2009

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.06.03 - I added the suggestions left on Ajaxian and in the comments and updated the browser versions to consider Safari 4, IE 8, and Chrome 2.
Update 2009.08.03 - I added some new Firefox hacks from porneL, which are very unique in that the target selector is not a child like the rest of them. More explanation on his site.

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.

Update 2010.04.07 - Clarification on :nth-of-type(n) from Estelle Weyl

Paul Irish front-end development

  1. #1

    ie8 do not do ?

  2. #2

    "/* IE 7 and below */"

    Pretty sure *:first-child+html doesn't target below IE7, otherwise nice list.

  3. Klaus Hartl
    #3

    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

  4. #4

    missing this one:
    /* IE7/8 */
    *+html #nueve { color: red }

  5. technova
    #5

    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

  6. #6

    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.

  7. #7

    Great list to have just in case! Thanks for sharing

  8. #8

    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)

  9. #9

    @travis, I'm not seeing *+html foo work 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.

  10. #10

    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

  11. #11

    @akbar, you can probably use the safari/chrome hack above. (I assume it affects chrome as well)

    /* saf3+, chrome1+ */
    @media screen and (-webkit-min-device-pixel-ratio:0) {
     div.myClass  { color: red  }
    }

    This will catch Safari3, Safari4, Chrome1, Chrome2. (Not Safari 2)

  12. janogarcia
    #12

    Hi, What has happened to the Attribute Hacks?

  13. #13

    @janogarcia , Hah. Thanks. In my update I stripped the hacks out of this post. They were present in the demo page though. Fixed now!

  14. janogarcia
    #14

    Thanks for the quick fix!

  15. Phil Ricketts
    #15

    I use this to select for IE7 only:

    #id element.class{
    color: blue;
    .color: red}

  16. Yogesh Singh
    #16

    thanks friend i have solved my problem of safari

  17. Ashwin SM
    #17

    Great Hacks for the different browsers. But one thing, is there any hack that only works for Opera9+..

  18. #18

    @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.

  19. #19

    @Ashwin SM, For Opera 9 you could try this:

    Add a class of 'hypen-ated' to your body tag.

    #foo { color: red; } /* applies to all browsers */
    body[class|="hypen-ated"] #foo { color: black; } /* applies to everything but opera 8,9 */

    This technique is courtesy of javascriptkit.

  20. #20

    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

  21. #21

    @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.

  22. Carlos
    #22

    thanks for posting this !!

  23. François
    #23

    Hi Paul, I found this page via David Bloom's blog. Thanks so much for this doubleplusgood page!

  24. #24

    Thanks Paul, those Safari hacks come in handy!

  25. #25

    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?

  26. #26

    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!

  27. #27

    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

  28. #28

    @Bizz, The one described is captured here. thanks!

  29. #29

    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 }

  30. #30

    Paul,

    Thanks for this comprehensive list. I didn’t know about !ie, that one’s pretty cool.

    I don't use CSS hacks anymore. Instead I use IE's conditional comments to apply classes to the body tag.

    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.css file, 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.

  31. #31

    Thank You For This Hacking code.But I want userfrndly hacking code of google chrome.If You Have then plz publish.

  32. #32

    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

  33. #33

    Thkxs again. I will do use it and
    let you know if I got it right.
    jorge

  34. James
    #34

    Just wanted to say thanks for the efforts. This article saved me a lot of stress.

    Keep up the good work.

  35. #35

    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!

  36. zolid
    #36

    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

  37. #37

    wow thanks, that was really helpful! :)

  38. #38

    Here's a IE8-selector hack:

    div.class { color: red; }
    div.class, #ie8#fix { color: blue; }

    This code will set color to blue in browsers, except for IE8. In IE8 it will be red.

  39. #39

    Thanks a lot for this list! Very helpful to have in my toolbox!

  40. chok
    #40

    Hi
    Thanx, but the Safari and Chrome hack* are not valid…

    *This hack :

    @media screen and (-webkit-min-device-pixel-ratio:0) {
    	.photo_jenny {
    	margin-top: -5px;
    	}
    }
  41. #41

    @chok
    very few of these hacks are actually *valid*
    for me, validation is a tool and not a goal. but that's just me..

  42. deos
    #42

    just saw at the test page, the !ie-hack does not work at all in IE6 and 7 (tested with the tools IETester(IE6+7) and MultipleIEs(IE6)). are these tools broken or is it the hack?

  43. #43

    @deos
    You spotted an error in my #id naming. I fixed it and things work as expected. :)
    Thanks!

  44. Digo
    #44

    Really amazing for chrome browser

    @media screen and (-webkit-min-device-pixel-ratio:0) {
      .divname {
    	border: 0px solid red;
    	position: relative;
    	top: 5px;
      }
    }
  45. #45

    @Digo Yup! I got that one listed up there for both Chrome and Safari3+. :)

  46. #46

    Hi,really useful article.
    do you know what is the css hack for Android browser?
    android browser shows the checkbox a little lower !!!
    Thanks

  47. #47

    I do not, sadly. Probably a media query of some sort could identify it.

  48. Leonardo
    #48

    Thanks man. Good Job!

    You saved my life =]

  49. #49

    Good Job, Thank You!

  50. #50

    Thanks.

    Also liked your mouse drawing.

    Came for css hack and started playing with mouse :)

  51. #51

    I came across your post while having some browser compatibility issues and I must say I absolutely love your background. Perfect break from the frustration of browsers lol… ;] will def be back to play when i get too frustrated…

  52. Nagaraju
    #52

    This is perfect hack for IE8 only

    .color  {
        color: #0FC/;
    }
  53. Nagaraju
    #53

    This is perfect hack for IE8 only

    .color
    {
    color: #0FC/;
    }

  54. Nagaraju
    #54

    IE8 hack css

    .color
    {
    background:blue/;
    }

    you have to write (backslash zero slash)

  55. #55

    I came across your site when I finally had enough of hacking CSS and decided to learn how to do it right. Very interesting tips you have noted, but where can I find out the justification for them, i.e. to help me understand better, is there a big CSS resource that actually covers ‘best practices’ in minute detail? I just need to understand why I should things the way I should.
    Thanks.

  56. #56

    @css??
    I would recommend reading what I wrote here: http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/

    Nonetheless, ways to target things that arent IE are necessary.. So you may need to isolate some styles.. Thats why this list exists.

    however.. the big guidance is…. you can probably avoid these. use a css reset… know floats and box models well.. and you'll be using a lot less browser specific code than you used to.

  57. #57

    @Thomas, this is interesting. Is this because IE8 is stricter than other browsers and won't allow two IDs on the same element? Your filter is more elegant and valid than the one, but I'd like to really understand it before using.
    Thanks :-)

  58. #58

    Cheers!!! Your list was a godsend!!! Found exactly what I was looking for… Something that would exclude IE6&7.

    Thanks for posting this!

    Chris

  59. #59

    I'm shocked! IE7 matches x:-moz-any-link, x:default selectors!

  60. Craig
    #60

    What's wrong with old fashioned Conditional Comments? They take care of most hacks and keep the ugly IE fixes in a separate file where they belong. Has anyone actually ever needed to apply hacks to a browser other than IE?

  61. Kim
    #61

    thanks.. i was looking for ie8 hack..

  62. #62

    Bad news, depending on how you look at it, from hacksville - IE9 beta and PP fall prey to the /9 hack. Which begs the next hack - how to target IE8 and not IE9. For the curious, I put up a little tool awhile back for testing hacks on your browser - it's selector-shell.appspot.com

  1. | #1
  2. | #2
  3. | #3
  4. | #4
  5. | #5
  6. | #6
  7. | #7
  8. | #8
  9. | #9
  10. | #10
  11. | #11
  12. | #12
  13. | #13
  14. | #14
  15. | #15
  16. | #16
  17. | #17
  18. | #18
  19. | #19
  20. | #20
  21. | #21
  22. | #22

For code blocks, use <pre lang="javascript" escaped="true">. css and html4strict are also accepted.

i left this space here for you to play. <3