Journal

Thoughts, ruminations, observations, and inspiration from theMechanism.

Safari CSS hack redux

January 8, 2008 | Posted by Jeffrey Barke | 57 comments

In March 2007, I wrote about a Safari CSS hack called the "Pound Safari Post Semicolon" hack (coined by Tony at Simian Design). As of Safari 3.0, this hack no longer works. However, there is a way to target Safari 3.0:

@media screen and (-webkit-min-device-pixel-ratio:0) {
/* Safari 3.0 and Chrome rules here */
}

As always, the disclaimer: nobody condones the use of hacks, but we all (well, most of us) have to make use of them at least once in a while. The above code targets both Safari 3.0 and Google's Chrome. Hopefully, it will be a future-proof hack, since the -webkit part of the selector will probably not be adopted by other browsers.

Example: The following code set the background color of the <body> element red in all browsers and then resets it to blue in Safari 3.0 and Chrome.
body { background-color: red; }
@media screen and (-webkit-min-device-pixel-ratio:0) {
    body { background-color: blue; }
}

[demo]

Tested on Mac OS X version 10.5.1 with Safari version 3.0.4 (5523.10.6) and Mozilla Firefox version 2.0.0.11. Tested on Windows XP Professional Version 2002 SP 2 with Mozilla Firefox version 2.0.0.11, Opera 9.10, and Microsoft Internet Explorer 6. Hack from CSS, JavaScript and XHTML Explained.

Update 2008-11-26: Since this hack targets Webkit-based browsers, Chrome is also affected. At the original time of writing, it also affected Opera (9.10); it hasn't since 9.50. The post copy has been updated to reflect these developments.

Spread the Love

del.icio.us:Safari CSS hack redux digg:Safari CSS hack redux spurl:Safari CSS hack redux wists:Safari CSS hack redux simpy:Safari CSS hack redux newsvine:Safari CSS hack redux blinklist:Safari CSS hack redux furl:Safari CSS hack redux reddit:Safari CSS hack redux blogmarks:Safari CSS hack redux Y!:Safari CSS hack redux smarking:Safari CSS hack redux magnolia:Safari CSS hack redux segnalo:Safari CSS hack redux

57 Comments So Far

  • Works perfect!!!
    Thank you :-)

  • Thanks man!

    you saved the day :]

  • Hi, I am trying to use the hack to position a photo using this code but it doesn’t seem to work…any suggestions?

    @media screen and (-webkit-min-device-pixel-ratio:0) {
    img.photo { margin-right: 10px; }
    }

  • Hello Debbie,

    Not sure why your code isn't working—perhaps you could provide a link to the page in question.

    I copied your code (slight change—margin-left instead of margin-right) and created a demo here. Everything seems to be working fine.

  • Thanks a lot, it’s really works in Safari…

    Get your own gravatar for comments by visiting gravatar.com
    Nanda YK. Salunkhe
    January 23rd, 1:44 am
  • This does not work on Safari 1.3.

  • Hence "However, there is a way to target Safari 3.0". :)

    Not sure if the Pound Safari Post Semicolon hack will work in Safari 1.3, though it does in 2.0.

  • This is awesome. Perfect Safari 3 CSS hack.

  • It Works!! I’ve been using IE hacks for years. Haven’t had many troubles from Safari until late with absolute positioning. I look forward to the day they all surrender to the only truly compliant browser, FireFox. Thanks

  • Hi Jeffrey

    It took some digging to find this post, but well worth the effort. Works perfectly in Safari 3

    Thanks a million

  • Great hack. Works well. If only there was a way to target JUST opera or JUST safari…I need to target SAFARI alone. I think all browsers should have a conditional comment that allows for alternative stylesheets.

  • Works swell - thanks so much for sharing!

  • I guess I;m a but dumb, but I want to use this hack to select different levels of font-weight, so I can make the fonts among IE, FF, NS and SF look at least similar.

    Can’t seem to do it (i have a bunch of CSS commands, but only want to change font control).

    Do I need to duplicate the entire class with the @media screen and (-webkit-min-device-pixel-ratio:0) {
    as a lead in or can I just use the lead-in and specify only the changed font controls?

    Thanks.

    Get your own gravatar for comments by visiting gravatar.com
    TK Vanacoro
    April 17th, 2:46 pm
  • TK,

    You only need to specify the rules you want to override in Safari and Opera, not the entire class.

  • okay, so do I just but the overrides at the bottom of my .css page? I’m still just a bit confused.

    Is there an example that does not include BODY or must they all be BODY commands?

    Thanks again.

    Get your own gravatar for comments by visiting gravatar.com
    TK Vanacoro
    April 18th, 11:28 am
  • TK, set up your stylesheet as normal first:

    body { font-family: Verdana, Arial, sans-serif; }
    #test { color: #ff0000; }
    .callout { font-weight: bold; }

    These rules are served to all browsers, and they set the body font to Verdana, the font color of a <div> with the id "test" to red, and make the font bold for anything assigned the class callout.

    At the bottom of the stylesheet, we'll only override those rules we want to display differently in Opera and Safari. In this case, we want the font color of the "test" div to be black instead of red and we want the .callout class to be both bold and italic;

    @media screen and (-webkit-min-device-pixel-ratio: 0) {
         #test { color: #000; }
         .callout { font-style: italic; }
    }

    The entire stylesheet would look like this:

    body { font-family: Verdana, Arial, sans-serif; }
    #test { color: #ff0000; }
    .callout { font-weight: bold; }
    @media screen and (-webkit-min-device-pixel-ratio: 0) {
         #test { color: #000; }
         .callout { font-style: italic; }
    }

  • use an Opera hack to separate Safari from Opera:
    html:first-child>b\ody #menu ul {margin-top:0;}

    anyhow: big thanx for this Safari only solution!!!!!!!!

    cheers

  • Okay, the hack is cool, for a hack, but does anyone have any thoughts on how to get it to validate? Guess that’s the price you pay for using hacks…

  • Worked great! Thanks a lot! I wish there were also FF only css hacks.

  • Its working… thanks..

  • Does this work for inserting in external style sheets as well as within html?

  • Yep, it does work for inserting in external style sheets as well as within HTML.

  • Saved my life. Thanks!

  • Perfect! Worked exactly as described. Thanks so much.

    Get your own gravatar for comments by visiting gravatar.com
    Ian Ferguson
    May 15th, 9:51 am
  • Thank you from me, too.

  • Hi, anyone know of any hacks for Firefox (2.0.0.16 and down) OR for IE7?

    I’ve tried one of each that I found on various other forums, and neither works…

    Thanks,
    Jenni
    http://www.theweblotus.com

  • Jenni, you can target any version of Internet Explorer by using conditional comments: http://www.quirksmode.org/css/condcom.html

  • Thanks, this did the trick! :)

  • [...] Recently while coding a client’s site, I had an issue with a background image being misaligned by just one pixel. It was only happening in Safari. I tried a number of things and finally resorted to a CSS hack for Safari. [...]

  • Magic!! Cool stuff.

  • Man, this doesnt work for google crome which execute safari specific code. If any luck let me know please.

  • Chrome is killing me too here -

    How could we end up with the same rendering engine being used, yet with different bugs, yet no way to distinguish.

    Arg…

  • Absolutely fantastic!
    Thank you,

    have a nice weekend :)

  • Is perfect this hack! thank you!!!

  • [...] Originally published here. [...]

  • Thank you!!!
    And it is works on Chrome too.

  • thanx. its a nice hack for safari 3. it works

    Get your own gravatar for comments by visiting gravatar.com
    Ace_ov_Spade
    October 23rd, 6:44 am
  • [...] Originally published here. [...]

  • [...] Originally published here. [...]

  • Thanks for

    @media screen and (-webkit-min-device-pixel-ratio:0) {
    /* Safari 3.0 and Chrome rules here */
    }

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

    Thanks in Advance

  • well this code is visible in Google Chrome also so it is not solution only for Safari …

  • works well thanks

  • what if I want to have safari css different from chrome’s one?

  • Thanks the code works great with Google Chrome, Safri 3.2.1, but its not working on the new Opera 9.63 I downloaded this afternoon?

  • Thanks a lot…

    It works for Opera & Chrome also.

  • Dear sir, as if you expore my website in chrome, it appears that the table underneath div “index_right” is not shown properly..but it works perfectly fine under ie and ff..
    i tried to use the code :
    @media screen and (-webkit-min-device-pixel-ratio:0) {
    #index_right{margin-bottom:5px;}
    }
    it sitll doesn’t work
    im sorry that im quite new to css
    please give me some hints if u have time~!
    thanks!

  • this is superb!!!

  • ok, it work but what happend when everything is OK on Chome and I need correction only on Safari-font-weight is bold only there and make my menu longer…?

  • Thanks homie,

    This just got me out of a tough spot

  • thanks dude with this hack, i am done with my stuff now :)

  • [...] (Thx to Solid State Group, Evotech, and Jeffrey.) [...]

  • just wanted to chime in and say thank you! this works perfectly and i’ve been searching and hacking away for a while for this exact css. nice job!

  • Spasibo!!Thx!!

  • I.LOVE.YOU
    please browsers, use the same engine for all!

  • Worked great, thanks! Just a note that this hack also performs well with the new release of Safari 4.0 as well. Thanks again.

  • Thanx a lot it’s really worked.

    great!!!!!!!

  • Is there a hack that will just effect Safari and not chrome?

Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Search the Archives
Feeds
NYC Bunker Live Cam

O'Reilly user group program member

Add to Technorati Favorites

We endorse

Basecamp

Want to work with us yet? We’re ready when you are.