Posted on Oct 5, 2008

Documenting the Hacks: CSS Browser Targeting

I think I’m somewhat of a rarity in the world of web developers – I actively use and certainly approve of CSS hacks to target and fix certain browsers. Of course, I don’t condone their use unless it is absolutely necessary, and in most cases good, standards compliant CSS should alleviate most issues before they occur, but when there is a need I think these hacks can be a great way to fix issues while still keeping your CSS logical and concise. The caveat to this is that all hacks need good comments to describe what is going on for future reference, whether that be your own or that of another developer. Also, always keep in mind that hacks can break when new browser versions are released, so always keep an eye out and use them sparingly.

I’ve always found it hard to find a good reference for these hacks. There are many sites that talk about an individual hack, but finding a single place that goes through targeting all the different browsers has been hard. This post hopes to solve that – more for my own use, but hopefully other developers can find it useful as well. What follows are the hacks that I use, which I believe to be the most logical and simple ones out there. In every case, #selector represents the element you want to apply a rule to (this can be either an ID or a class), and property and value represent any normal CSS property.

Internet Explorer 6

Let’s start with the one that developers will probably need to use most often. To target IE6:

  1. #selector {
  2.     _property: value;
  3. }

Internet Explorer 7

The following targets IE7 and below. You can use it in conjunction with the above rule to be able to target IE7 and IE6 individually – just make sure you declare the IE7 rule first, then the IE6 rule.

  1. /* Target IE7 and below */
  2. #selector {
  3.     *property: value;
  4. }
  5. /* Then overwrite for targeting IE6 specifically */
  6. #selector {
  7.     _property: value;
  8. }

Firefox 2

The easiest way to target versions of Firefox is with selectors that only work in Gecko-based browsers. The following will target Firefox version 2 and below:

  1. #selector, x:-moz-any-link {
  2.     property: value;
  3. }

Firefox 3

As with the Internet Explorer 7 hack, you can easily combine the Firefox 2 and Firefox 3 hacks to target those specific versions of Firefox.

  1. /* Target Firefox 2 */
  2. #selector, x:-moz-any-link {
  3.     property: value;
  4. }
  5.  
  6. /* Then overwrite for Firefox 3 specifically */
  7. #selector, x:-moz-any-link, x:default {
  8.     property: value;
  9. }

Safari 3.0+

The following will target Safari version 3 and above. To target just Safari 3.1, see the next hack. Note that this is valid CSS3, and as such other browsers will eventually start supporting it, so be wary.

Update: Firefox 3.5 now supports this, and will read the style

  1. body:first-of-type #selector {
  2.     property:value;
  3. }

Safari 3.1 and Google Chrome

Google Chrome and Safari both use the Webkit rendering engine, and as such shouldn’t usually need to be targeted separately. So far, I have not found a way to target just Google Chrome, so attack that with JavaScript for the best results. Make sure you declare this after the Safari 3.0 hack above, as Chrome and Safari 3.1 will read that as well.

Update: Firefox 3.5 now supports this, and will read the style

  1. body:nth-of-type(1) #selector {
  2.     property: value;
  3. }

Opera 9

While Opera is pretty good at rendering standards compliant code, and won’t generally need hacks, there are times when it is necessary. Unfortunately, I haven’t found a good way to target just Opera – instead, the easiest way is to use a rule that targets Opera and Safari (and other Webkit browsers like Chrome), then reset for the Webkit browsers.

  1. /* Declare for Opera and Webkit */
  2. @media screen and (-webkit-min-device-pixel-ratio:0){
  3.     #selector {
  4.         property: value;
  5.     }
  6. }
  7.  
  8. /* Revert for Safari (and other Webkit browsers) and Firefox 3.5 */
  9. body:first-of-type #selector {
  10.     property:value;
  11. }
  • vkladish
    It's amazing!!! Thanks.
  • Thanks for this hack. I've been looking for browser-specific CSS hacks for ages!
  • Wow! Thanks. Is there one for IE8?
  • All these hacks look so clean an nice. Thank you!
  • Sign: yyams Hello!!! punht and 843dhursyvpxd and 7004 My Comments: Cool!
  • These are solid. Thanks.

    I'm not seeing that Opera 9 hack working. Mostly because the -webkit min ratio guy doesnt catch for Opera.

    I have a test page with these and other hacks here:
    http://paulirish.com/demo/css-hacks

    Also my comprehensive roundup of all css hacks:
    http://paulirish.com/2009/browser-specific-css-hacks/

    Cheers
  • Nice one man. Have always just played about with descendants to get stuff to work in Opera - non rgba stuff etc. No more.
  • Thanks Deb, you're absolutely right. There isn't really an ideal way to target FF2 without Javascript, this is about as close as you get.

    Thankfully, Firefox users are much more inclined to upgrade than users of some other browsers.
  • Deb
    The FF2 hack is useful only if it is followed by the IE7 and FF3 hacks (thus leaving FF2 the only one affected). It's a bit misleading to call it a FF2 hack since you need two other hacks, but it does work. :)
  • Denise
    Yeah, I used this FF2 hack only it changed IE7 too.
  • Ben
    Good post. I don't think the Firefox selectors work, though. I'm finding that both the FF2 and FF3-targeting rules are being matched by both versions of Firefox and also IE7 (but not IE6, Safari, Opera or Chrome).
  • Madhu
    Thanks,

    But this works for both in SAFARI 3.1 AND GOOGLE CHROME there is any hack only for SAFARI 3.1

    body:first-of-type #selector {
    property:value;
    }

    Thanks in Advance
  • Luc
    Nice!
  • Marcin
    Thanks for this site. But I'm a bit confused, I put into my css these lines:

    #selector { *background-color: yellow; /* target IE7 and below */ }
    #selector { _background-color: blue; /* then overwrite for targeting IE6 specifically */ }

    #selector, x:-moz-any-link { background-color: pink; /* target Firefox 2 */ }
    #selector, x:-moz-any-link, x:default { background-color: white; /* then overwrite for Firefox 3 specifically */ }

    @media screen and (-webkit-min-device-pixel-ratio:0) { #selector { background-color: red; } /* declare for Opera and Webkit browsers */ }

    body:first-of-type #selector { background-color: silver; /* target Safari 3 and above */ }
    body:nth-of-type(1) #selector { background-color: black; /* target Safari 3.1 and Google Chrome */ }

    but only Firefox, Chrome and Safari displays defined colors. IE6, IE7 have background-color white and Opera black... How to fix it?
  • Ahhh great one on the FF2 hack, that saved me some time.
blog comments powered by Disqus