279

I'm trying to find a way to apply CSS just to Safari, but everything I find also applies to Chrome. I know these are currently both WebKit browsers, but I'm having problems with div alignments in Chrome and Safari; each displays differently.

I have been trying to use this but it affects Chrome as well:

@media screen and (-webkit-min-device-pixel-ratio:0) { 
  #safari { display: block; } 
} 

Does anyone know of another one that will just apply to Safari?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Bear John
  • 3,135
  • 7
  • 21
  • 22
  • Not using CSS, you'll need to use javascript. http://stackoverflow.com/questions/5899783/detect-safari-using-jquery – Christian May 02 '13 at 22:06
  • 1
    I’d be more interested in the actual problem than a fragile and hacky solution. Is it (still) available somewhere? – Matijs Jan 05 '18 at 10:57
  • For people who are posting 'does not work' - please realize that you need to know the version of Safari you are testing. There is no 'catch-all' Safari solution for every version. You need to provide that information with your posting or no-one can assist you in finding a solution. – Jeff Clayton Mar 05 '20 at 14:04
  • Different versions of Safari have different needs - check here for live examples: https://browserstrangeness.bitbucket.io/css_hacks.html#safari [Or the Mirror] https://browserstrangeness.github.io/css_hacks.html#safari – Jeff Clayton Mar 05 '20 at 14:07
  • 3
    **N.B.** The question states: _"i know these [Safari and Chrome] are both webkit browsers"_ But in April 2013, just before this question was asked, Google Chrome Browser switched from WebKit to **Blink**, a fork of WebKit. – Rounin Feb 11 '22 at 10:43
  • Closing as not reproducible since Chrome uses Blink now, and did so basically since the time the question was asked. – TylerH Sep 20 '22 at 14:24

22 Answers22

509
  • UPDATED FOR VENTURA & SAFARI 16.4 (Spring 2023 Update) *

PLEASE PLEASE -- If you are having trouble, and really want to get help or help others by posting a comment about it, Post Your Browser and Device (MacBook/IPad/etc... with both browser and OS version numbers!)

Claiming none of these work is not accurate (and actually not even possible.) Many of these are not really 'hacks' but code built into versions of Safari by Apple. More info is needed. I love the fact that you came here, and really want things to work out for you.

If you have issues getting something from here working on your site, please do check the test site via links below -- If a hack is working there, but not on your site, the hack is not the issue - there is something else happening with your site, often just a CSS conflict as mentioned below, or perhaps nothing is working but you may be unaware that you are not actually using Safari at all. Remember that this info is here to help people with (hopefully) short term issues.

The test site:

https://browserstrangeness.bitbucket.io/css_hacks.html#safari

AND MIRROR!

https://browserstrangeness.github.io/css_hacks.html#safari

NOTE: Filters and compilers (such as the SASS engine) expect standard 'cross-browser' code -- NOT CSS hacks like these which means they will rewrite, destroy or remove the hacks since that is not what hacks do. Much of this is non-standard code that has been painstakingly crafted to target single browser versions only and cannot work if they are altered. If you wish to use it with those, you must load your chosen CSS hack AFTER any filter or compiler. This may seem like a given but there has been a lot of confusion among people who do not realize that they are undoing a hack by running it through such software which was not designed for this purpose.

Safari has changed since version 6.1, as many have noticed.

Please note: if you are using Chrome [and now also Firefox] on iOS (at least in iOS versions 6.1 and newer) and you wonder why none of the hacks seem to be separating Chrome from Safari, it is because the iOS version of Chrome is using the Safari engine. It uses Safari hacks not the Chrome ones. More about that here: https://allthingsd.com/20120628/googles-chrome-for-ios-is-more-like-a-chrome-plated-apple/ Firefox for iOS was released in Fall 2015. It also responds to the Safari Hacks, but none of the Firefox ones, same as iOS Chrome.

ALSO: If you have tried one or more of the hacks and have trouble getting them to work, please post sample code (better yet a test page) - the hack you are attempting, and what browser(s) (exact version!) you are using as well as the device you are using. Without that additional information, it is impossible for me or anyone else here to assist you.

Often it is a simple fix or a missing semicolon. With CSS it is usually that or a problem of which order the code is listed in the style sheets, if not just CSS errors. Please do test the hacks here on the test site. If it works there, that means the hack really is working for your setup, but it is something else that needs to be resolved. People here really do love to help, or at least point you in the right direction.

That out of the way here are hacks for you to use for more recent versions of Safari.

You should try this one first as it covers current Safari versions and is pure-Safari only:

This one still works properly with Safari 16.4 (Spring-2023):

/* Safari 7.1+ */

_::-webkit-full-page-media, _:future, :root .safari_only {

  color:#0000FF; 
  background-color:#CCCCCC; 

}

To cover more versions, 6.1 and up, at this time you have to use the next pair of css hacks. The one for 6.1-10.0 to go with one that handles 10.1 and up.

So then -- here is one I worked out for Safari 10.1+:

The double media query is important here, don't remove it.

/* Safari 10.1+ */

@media not all and (min-resolution:.001dpcm) { @media {

    .safari_only { 

        color:#0000FF; 
        background-color:#CCCCCC; 

    }
}}

Try this one if SCSS or other tool set has trouble with the nested media query:

/* Safari 10.1+ (alternate method) */

@media not all and (min-resolution:.001dpcm)
{ @supports (-webkit-appearance:none) {

    .safari_only { 

        color:#0000FF; 
        background-color:#CCCCCC; 

    }
}}

This next one works for 6.1-10.0 but not 10.1 (Late March 2017 update)

This hack I created over many months of testing and experimentation by combining multiple other hacks.

NOTES: like above, the double media query is NOT an accident -- it rules out many older browsers that cannot handle media query nesting. -- The missing space after one of the 'and's is important as well. This is after all, a hack... and the only one that works for 6.1 and all newer Safari versions at this time. Also be aware as listed in the comments below, the hack is non-standard css and must be applied AFTER a filter. Filters such as SASS engines will rewrite/undo or completely remove it outright.

As mentioned above, please check my test page to see it working as-is (without modification!)

And here is the code:

/* Safari 6.1-10.0 (not 10.1) */

@media screen and (min-color-index:0) and(-webkit-min-device-pixel-ratio:0) 
{ @media {
    .safari_only { 

        color:#0000FF; 
        background-color:#CCCCCC; 

    }
}}

For more 'version specific' Safari CSS, please continue to read below.

/* Safari 11+ */

@media not all and (min-resolution:.001dpcm)
{ @supports (-webkit-appearance:none) and (stroke-color:transparent) {

    .safari_only { 

        color:#0000FF; 
        background-color:#CCCCCC; 

    }
}}

One for Safari 11.0:

/* Safari 11.0 (not 11.1) */

html >> * .safari_only {

  color:#0000FF; 
  background-color:#CCCCCC; 

}

One for Safari 10.0:

/* Safari 10.0 (not 10.1) */

_::-webkit-:host:not(:root:root), .safari_only {

  color:#0000FF; 
  background-color:#CCCCCC; 

}

Slightly modified works for 10.1 (only):

/* Safari 10.1 */

@media not all and (min-resolution:.001dpcm)
{ @supports (-webkit-appearance:none) and (not (stroke-color:transparent)) {

    .safari_only { 

        color:#0000FF; 
        background-color:#CCCCCC; 

    }
}}

Safari 10.0 (Non-iOS Devices):

/* Safari 10.0 (not 10.1) but not on iOS */

_::-webkit-:-webkit-full-screen:host:not(:root:root), .safari_only {

  color:#0000FF; 
  background-color:#CCCCCC; 

}

Safari 9 CSS Hacks:

A simple supports feature query hack for Safari 9.0 and up:

@supports (-webkit-hyphens:none)
{

  .safari_only {
    color:#0000FF; 
    background-color:#CCCCCC; 
  }

}

A simple underscore hack for Safari 9.0 and up:

_:not(a,b), .safari_only {

  color:#0000FF; 
  background-color:#CCCCCC; 

}

Another one for Safari 9.0 and up:

/* Safari 9+ */

_:default:not(:root:root), .safari_only {

  color:#0000FF; 
  background-color:#CCCCCC; 

}

and another support features query too:

/* Safari 9+, < 13.1 */

@supports (-webkit-marquee-repetition:infinite) and (object-fit:fill) {

    .safari_only { 

        color:#0000FF; 
        background-color:#CCCCCC; 

    }
}

One for Safari 9.0-10.0:

/* Safari 9.0-10.0 (not 10.1) */

_::-webkit-:not(:root:root), .safari_only {

  color:#0000FF; 
  background-color:#CCCCCC; 

}

Safari 9 now includes feature detection so we can use that now...

/* Safari 9 */

@supports (overflow:-webkit-marquee) and (justify-content:inherit) 
{

  .safari_only {
    color:#0000FF; 
    background-color:#CCCCCC; 
  }

}

Now to target iOS devices only. As mentioned above, since Chrome on iOS is rooted in Safari, it of course hits that one as well.

/* Safari 9.0 (iOS Only) */

@supports (-webkit-text-size-adjust:none) and (not (-ms-ime-align:auto))
and (not (-moz-appearance:none))
{

  .safari_only {
    color:#0000FF; 
    background-color:#CCCCCC; 
  }

}

one for Safari 9.0+ but not iOS devices:

/* Safari 9+ (non-iOS) */

_:default:not(:root:root), .safari_only {

  color:#0000FF; 
  background-color:#CCCCCC; 

}

And one for Safari 9.0-10.0 but not iOS devices:

/* Safari 9.0-10.0 (not 10.1) (non-iOS) */

_:-webkit-full-screen:not(:root:root), .safari_only {

  color:#0000FF; 
  background-color:#CCCCCC; 

}

Below are hacks that separate 6.1-7.0, and 7.1+ These also required a combination of multiple hacks in order to get the right result:

/* Safari 6.1-7.0 */

@media screen and (-webkit-min-device-pixel-ratio:0) and (min-color-index:0)
{  
   .safari_only {(;

      color:#0000FF; 
      background-color:#CCCCCC; 

    );}
}

Since I have pointed out the way to block iOS devices, here is the modified version of Safari 6.1+ hack that targets non-iOS devices:

/* Safari 6.1-10.0 (not 10.1) (non-iOS) */

@media screen and (min-color-index:0) and(-webkit-min-device-pixel-ratio:0) 
{ @media {
    _:-webkit-full-screen, .safari_only { 

        color:#0000FF; 
        background-color:#CCCCCC; 

    }
}}

To use them:

<div class="safari_only">This text will be Blue in Safari</div>

Usually [like in this question] the reason people ask about Safari hacks is mostly in reference to separating it from Google Chrome (again NOT iOS!) It may be important to post the alternative: how to target Chrome separately from Safari as well, so I am providing that for you here in case it is needed.

Here are the basics, again check my test page for lots of specific versions of Chrome, but these cover Chrome in general. Chrome is version 45, Dev and Canary versions are up to version 47 at this time.

My old media query combo I put on browserhacks still works just for Chrome 29+:

/* Chrome 29+ */

@media screen and (-webkit-min-device-pixel-ratio:0) and (min-resolution:.001dpcm)
{
    .chrome_only {

       color:#0000FF; 
       background-color:#CCCCCC; 

    }
}

An @supports feature query works well for Chrome 29+ as well... a modified version of the one we were using for Chrome 28+ below. Safari 9, the coming Firefox browsers, and the Microsoft Edge browser are not picked up with this one:

/* Chrome 29+ */

@supports (-webkit-appearance:none) and (not (overflow:-webkit-marquee))
and (not (-ms-ime-align:auto)) and (not (-moz-appearance:none))
{
    .chrome_only {

       color:#0000FF; 
       background-color:#CCCCCC; 

    }
}

Previously, Chrome 28 and newer were easy to target. This is one I sent to browserhacks after seeing it included within a block of other CSS code (not originally intended as a CSS hack) and realized what it does, so I extracted the relevant portion for our purposes:

[ NOTE: ] This older method below now pics up Safari 9 and the Microsoft Edge browser without the above update. The coming versions of Firefox and Microsoft Edge have added support for multiple -webkit- CSS codes in their programming, and both Edge and Safari 9 have added support for @supports feature detection. Chrome and Firefox included @supports previously.

/* Chrome 28+, Now Also Safari 9+, Firefox, and Microsoft Edge */

@supports (-webkit-appearance:none) 
{
    .chrome_and_safari {

       color:#0000FF; 
       background-color:#CCCCCC; 

    }
}

The block of Chrome versions 22-28 (If needed to support older versions) are also possible to target with a twist on my Safari combo hacks I posted above:

/* Chrome 22-28 */

@media screen and(-webkit-min-device-pixel-ratio:0)
{
    .chrome_only {-chrome-:only(;

       color:#0000FF; 
       background-color:#CCCCCC; 

    );}
}

NOTE: If you are new, change class name but leave this the same-> {-chrome-:only(;

Like the Safari CSS formatting hacks above, these can be used as follows:

<div class="chrome_only">This text will be Blue in Chrome</div>

So you don't have to search for it in this post, here is my live test page again:

https://browserstrangeness.bitbucket.io/css_hacks.html#safari

[Or the Mirror]

https://browserstrangeness.github.io/css_hacks.html#safari

The test page has many others as well, specifically version-based to further help you differentiate between Chrome and Safari, and also many hacks for Firefox, Microsoft Edge, and Internet Explorer web browsers.

NOTE: If something doesn't work for you, check the test page first, but provide example code and WHICH hack you are attempting for anyone to assist you.

Jeff Clayton
  • 7,053
  • 1
  • 31
  • 36
  • 16
    Just want to say that test page is awesome. I can now browse to that site with any device and see which css rules are applicable! – duyn9uyen Oct 28 '14 at 20:02
  • 1
    Will this apply to Safari on iPhone or iPad? – Greg Rozmarynowycz Nov 14 '14 at 15:20
  • I have an iPad mini, and it works on it. As long as you have Safari 6.1 or newer (8 is current) then you are good. So yes. – Jeff Clayton Nov 14 '14 at 19:04
  • 1
    actually, I just solved my own question using a combination of the example above AND the (;property: value;); below and it worked great! – mydoglixu Dec 03 '14 at 20:49
  • 1
    The hack for Safari 6.1+ at the top of this answer throws an error in the Sass compiler. Is there a way to solve it? – Blackbird Apr 17 '15 at 09:52
  • 2
    @Blackbird Sorry but you cannot compile or filter the hacks, it ruins them. They have to be used as-is. (Add them to the completed css file after compilation.) The fact that they are non-standard is why they work. – Jeff Clayton Apr 17 '15 at 22:18
  • a media query within a media query holy shoot :) – OutFall Apr 23 '15 at 20:52
  • @Jeff Clayton, how do you use 6.1+ with certain screen size? I want the safari only css apply when screen size is at 768px. How to make that work? – user2734550 Sep 11 '15 at 01:24
  • The interior media query can be used for that. Instead of just media { css here } you can media screen and (min-width:768px) and (max-width:768px) { css here } leave the outer one as-is. The reason it works is the nested media queries only existed after Safari 6.0, but you can make it have any media effect you wish. – Jeff Clayton Sep 11 '15 at 01:38
  • This helped me fixed a really annoying Safari problem. Thank you so much! +1! – jehzlau Jan 27 '16 at 18:23
  • @MayankVadiya Please provide example code. Are you using all parts of the CSS code I provided? And what CSS/which CSS hack are you talking about? Usually when people have issues they are not using the entire hack as written. Please go to the test page and check the examples and see if they are not working on your machine. What browser(s) are you trying? Is the version of browser matching up to the browser you are using? Without any further information you cannot be assisted. Viewing the site you are working on would be the best way to get assistance here. Thanks in advance. – Jeff Clayton Feb 11 '16 at 13:32
  • @JeffClayton can talk in chat – Mayank Vadiya Feb 11 '16 at 13:55
  • @MayankVadiya you are invited to chat, invite sent css-hacks room – Jeff Clayton Feb 11 '16 at 14:11
  • Ah, I still may be able to help, are you attempting to filter or compile the hacks you are trying (which are you trying?) And have you looked at the test page to see if they work for you there? If so, what are you doing differently? If not, I would love to find out what is needed to update them. – Jeff Clayton Feb 12 '16 at 17:25
  • I trimmed up notes at the end on posting and asking for help. – Jeff Clayton Feb 15 '16 at 13:56
  • Thank you for this comprehensive list! There seems to be a typo in the first answer where @media is stated twice. Removing it allowed it to work for me! – zero_cool Sep 22 '16 at 19:01
  • @zero_cool You are welcome but no that is not a typo, the double media query blocks many older browsers, you do need all of the closing brackets as well. What happens if you remove it is that additional browsers will be allowed that shouldn't be. Try redoing a copy of the hack in entirety on a fresh page, check my test page and I bet the test does work here: http://browserstrangeness.bitbucket.org/css_hacks.html – Jeff Clayton Sep 22 '16 at 20:17
  • In my experience with the safari 6.1 hack, it has never failed and the only people who thought it has, had not copied all of the parts of it, or modified it before using it. It needs to be used exactly as written or it will not have the right results. Each person in the past contacted me back and let me know that it did work after they tried it again cleanly and in its complete form. – Jeff Clayton Sep 23 '16 at 12:57
  • @RobertoFlores did you look at the test site to see if it works on your browser first? Also, which version are you testing? Do you know which version of browser you are using? Usually that is the key. Also - there are several here. Could you provide a sample page? Perhaps I might be able to assist? Without any useful information or source code, I cannot help you. Thanks in advance. – Jeff Clayton Nov 23 '16 at 23:31
  • Scroll down to the bottom of the test page to match at least your browser's user agent string. Most likely it is a browser version that you are having trouble with. Otherwise, be sure you post the CSS hack or hacks after your style sheets are loaded, in their complete form and not through a css filtering program. That should fix your problem. – Jeff Clayton Nov 26 '16 at 14:03
  • 1
    Great solution, I had the same SASS compiler problem as @Blackbird, but as you say, compiler would probably break the spacing etc anyway, so I've added to the final output and works perfectly – Hayden Crocker Mar 13 '17 at 11:47
  • Updated OSX Sierra last night, now hoping for another update? – Deborah Mar 29 '17 at 13:05
  • Good catch, they did change things -- I just updated OS X to 10.12.4 and safari updated to version 10.1 (12603.1.30.0.34) with it -- this one works for 7.1+ still: _::-webkit-full-page-media, _:future, :root .selector { property:value; } – Jeff Clayton Mar 29 '17 at 14:42
  • @Deborah ok check it now and also the Safari section of the live test page reflecting the updated Safari stats. – Jeff Clayton Mar 30 '17 at 05:02
  • I can't get your first method (10.1) to work with a media query: `@ media screen and (max-width: 767px) { _::-webkit-full-page-media, _:future, :root .flex-container { display: none; } }` Works without the media query though. Any ideas? – Kyle Vassella Apr 25 '17 at 19:33
  • -webkit-full-page-media may be at odds with your 'max size' media query wrapper - try setting your size to greater than the size of your monitor and seeing if you have different results for testing - I haven't found very many specific ones. You may have to use 2 (the 6.1-10 hack and the 10.1+ hack) as separate css blocks.see here: http://browserstrangeness.com/css_hacks.html#safari – Jeff Clayton Apr 25 '17 at 23:37
  • @KyleVassella nest either hack inside your width query wrapper and that may do what you want. – Jeff Clayton Apr 25 '17 at 23:38
  • This one: @ media not all and (min-resolution:.001dpcm) { @ media { .selector { property:value; } } } you can actually add your max-width call to the interior blank media query instead of making it triple nested – Jeff Clayton Apr 25 '17 at 23:40
  • Between the two of them you would be able to cover your safari targets -- the other being this one: @ media screen and (min-color-index:0) and(-webkit-min-device-pixel-ratio:0) { @ media { .selector { property:value; } } } – Jeff Clayton Apr 25 '17 at 23:41
  • they don't overlap (safari 6.1-10.0) and (safari 10.1+) but they cannot be combined into one. – Jeff Clayton Apr 25 '17 at 23:45
  • at the very least a flexbox is not expected to be full page media so i can imagine that is inherently an issue with what you want to do – Jeff Clayton Apr 25 '17 at 23:55
  • So are you suggesting it would help to be able to target browsers in CSS in case a browser needs specific attention? How would you design a style to handle that? – 1.21 gigawatts Dec 26 '18 at 11:53
  • @1.21gigawatts -- Almost... this is meant to be temporary only until one can find a way to rewrite larger css rule sets to target specific browsers that are not acting properly with the code as it stands on a site that otherwise works properly. These code snippets do already target specific browsers, a way to place browser specific css in a style sheet, so not sure exactly what you are asking. If you have found examples in your own work where your css does not act the same way it should in all browsers, then you may already have the answer you are looking for. – Jeff Clayton Dec 27 '18 at 00:58
  • Would it be helpful if where you needed to you could declare styles that targeted the manufacturer and version? For example, if you needed to set styles specific to Chrome versions 10 to 11 you could say, `--chrome-10-11-color: red` or for Safari, `--safari-color: blue`? I have a use case now that I need to make adjustments for Edge. If I could say `--edge-color: green` in one line of CSS I would be done with the project. If later on Edge changes I can go back and change that line to `--edge-upto-12-color: green`. – 1.21 gigawatts Dec 27 '18 at 06:46
  • @1.21gigawatts you can do that. There is no reason why you can't do it that way. Over time you may need to go back and check and see if a hack you chose (often a year or years later) has been picked up by another browser or removed from the one you are at the current time targeting. – Jeff Clayton Dec 27 '18 at 16:12
  • @JeffClayton What do you mean? The browser isn't aware of these styles or how to apply them. Parsing browser specific styles would have to be implemented by browser manufacturers. For example, I just typed `-safari-line-height:1` and `-webkit-line-height:1` in Safari and neither were applied. – 1.21 gigawatts Dec 28 '18 at 07:15
  • Here is an example using one from my test page: First set a default color in css (you already have this) color:black; Set a css variable color called --safari-11-up-color: blue; In the css later do this: @ media not all and (min-resolution:.001dpcm) { @ supports (-webkit-appearance:none) and (stroke-color:transparent) { body { color: var(--safari-11-up-color); } } } – Jeff Clayton Dec 31 '18 at 15:19
  • [note that in the css code I just posted I had to separate the @ symbols from the media and supports queries it should be ATmedia not AT(space)media in this comment block.] – Jeff Clayton Dec 31 '18 at 15:23
  • So if you try that you will have different hacks, with specific variables tailored to each css hack within them, but listed above as pre-defined variables. – Jeff Clayton Dec 31 '18 at 15:25
  • If you go to my test page and view source, while I didn't generally use css variables, I did use a naming scheme like this for a good portion of my css styles. – Jeff Clayton Dec 31 '18 at 15:26
  • @1.21gigawatts it just takes a different method -- you can't really create new 'named' properties as you are correct, the browser does not know them. But you can set them to variables with names you choose in order to do a similar effect. – Jeff Clayton Dec 31 '18 at 15:32
  • The reason you did not understand me is that you did not realize that the code you asked me about previously, --safari-color: blue is valid but NOT the same as -safari-color: blue. The double dash is doable because it is a a reference to a definable css variable... NOT a browser property. The question you asked me later is the single dash, which would refer to a property not a variable. So really you didn't actually say the same thing in your question that you said in the test you just showed me. Look up how to use CSS variables for more information. – Jeff Clayton Dec 31 '18 at 15:39
  • @FairyQueen - please check the demo site to be sure none of them are working for you. Figure out which version of the browser you are using as well to decide which may match. Odds are one of them does. If it is not working within your own CSS, it is not the hack that is the issue, but something else. Other than that, you need to provide more information (like browser version and maybe OS version) if you need some assistance. – Jeff Clayton Mar 05 '20 at 05:01
  • Perhaps provide a link to a page where one of these hacks is attempted and someone may be able to resolve it. Please do check one of my browserstrangeness pages though to see these Safari hacks live. More than one often works. Knowing if you are using iOS or Desktop version with Safari may be important for others to assist you as well. So far, there has always been an ordering issue or a different css code interfering with it, or an incomplete or otherwise damaged copy of a hack (which cannot work as intended.) – Jeff Clayton Mar 05 '20 at 05:26
  • Simply, more information is needed if you have the odd chance that none of the ones on the test sites are working for you. Please read my notes at the top pertaining to help requests in order to receive any further assistance. – Jeff Clayton Mar 05 '20 at 05:42
  • this is absurd complications for todays times, too much clutter and bs, there should be onliner.. – Hiti3 Jun 25 '20 at 10:10
  • @Hiti3 I understand your frustration here. There are a few if you look for specific versions. This was not easy to accomplish due to the fact that they are not provided via standards. If you choose one for Safari 7.1+ or Safari 9+ you cover most of the current versions. However, one of the reasons one chooses a hack is because one version may not act like another, meaning your reasoning is not the ONLY reason one needs a hack. So the same hack does not suffice. – Jeff Clayton Jun 26 '20 at 11:09
  • The 7.1+ css solution didn't work. It didn't turn any text blue. – Steve Aug 06 '20 at 20:16
  • @Steve - please verify me 2 things: what specific version of Safari are you using, and if you don't see it turn red here (the top hack on the page) at my test site: https://browserstrangeness.bitbucket.io/css_hacks.html (your comment needs more information to validate and update the hack notes) – Jeff Clayton Aug 12 '20 at 15:26
  • For anyone posting comments like these 'did not work,' especially people newer to css issues, please first go to my test site and see if the hack works there. If it does not, please post your specific os version like 'os x 10.12.1' and your exact browser version (not just 'safari,' but 'safari 9.0' for example) or nobody can help diagnose the issue. If it works on my site and not yours, that means you have a difference in your html/css that needs to be resolved. I hope that helps. – Jeff Clayton Aug 20 '20 at 12:37
  • This is awesome, thanks so much for sharing. And I confirm the Safari 10.1+: hack STILL WORKS PERFECTLY as of NOVEMBER 11 2020 cheers – avia Nov 07 '20 at 18:44
94

There is a way to filter Safari 5+ from Chrome:

@media screen and (-webkit-min-device-pixel-ratio:0) { 
    /* Safari and Chrome */
    .myClass {
     color:red;
    }

    /* Safari only override */
    ::i-block-chrome,.myClass {
     color:blue;
    }
}
user2550776
  • 1,195
  • 8
  • 4
  • 3
    http://jeffclayton.wordpress.com/2014/07/22/the-chrome-and-safari-css-hacks-collection/ - I have been working on many (I test and create css hacks for browserhacks.com) and test page here: http://browserstrangeness.bitbucket.org/css_hacks.html#webkit – Jeff Clayton Aug 06 '14 at 14:14
  • Note: in iOS [tested in iOS 7], the chrome version is actually running the safari engine, so on ipads or iphones, you use the safari hacks. For other devices, they are different. – Jeff Clayton Aug 06 '14 at 14:15
  • So, basically the code in the answer works for Safari 5.0 and 6.0 and not 6.1+? – Nic Cottrell Oct 15 '14 at 13:26
  • To be absolutely accurate, the ::i-block-chrome, .myClass { } construct allows only Safari 5.1-6.0, but also Chrome 10-24 (old version of Chrome not really used anymore so not entirely worth mentioning) but nothing newer - Safari hacks tend to work like that: one batch handles 5.1-6.0, another handles 6.1-7.0, and the newer ones handle 7.1-8.0. They seem to update things a lot until they decide to go to the next numerical version which is very close to the previous '.1' release. – Jeff Clayton Oct 19 '14 at 07:38
  • At the time of this answer in 2013, Safari 6.1 had just been released, so not enough people had seen that the browser had changed so this was the most accurate one at the time. If you need newer ones check my answer below. This was a great answer at the time it was provided. Time changes however so I posted my work as an update to this one. It took months for me to create the 6.1-7.0 and 7.1-8.0 ones. I hope you like the results. Most likely when a version 8.1 is released if it follows the pattern, it will also require a different hack. Again only time will tell. – Jeff Clayton Oct 19 '14 at 07:52
21

Sarari Only

.yourClass:not(:root:root){ 
    /* ^_^ */ 
}
xicooc
  • 855
  • 7
  • 8
  • 11
    Care to add some explanation? Do I literally type root:root? – Nubtacular Jul 17 '14 at 07:36
  • 1
    This one is exactly correct for Safari 3.x (and Chrome version 1.0 only though nobody uses Chrome 1.0 since it is in the 30's now...) – Jeff Clayton Aug 06 '14 at 13:29
  • This one now targets Safari 9.0 as well, so complete stats have updated: /* Chrome 1.0, Safari 3.X, Safari 9.0+ */ – Jeff Clayton Sep 24 '15 at 20:32
  • Works for the Safari `9.1.1`! – Leon Gaban Jun 23 '16 at 18:07
  • 4
    The `:not(:root:root)` selector is invalid according to CSS Selectors 3 spec (in which `:not()` can contain only a simple selector, i.e. one type selector _or_ one ID _or_ one class _or_ one pseudo-class), but completely valid according to CSS Selectors 4 (where `:not()` accepts list of selectors). It's true that currently only Safari understands CSS Selectors 4 syntax, but this solution is not future-proof. – Ilya Streltsyn Jul 07 '16 at 06:10
  • 2
    Very few hacks are not (and much actual standard code is not due to version changes) future proof. The best plan is if you are going to use a css hack, use it only as a temporary fix in order to buy time to make a more official cross-browser update. – Jeff Clayton Apr 28 '17 at 18:12
  • The reason some of these hacks work is because they are not standard, and are rendered strangely. The :root:root combo works because other browsers filter it out as bad css. Hence... hack. – Jeff Clayton Apr 28 '17 at 18:14
  • 1
    This affect chrome too – Aamir Afridi Dec 30 '21 at 10:36
  • This no longer works unfortunately – maxshuty Aug 03 '22 at 11:44
14

This hack 100% work only for safari 5.1-6.0. I've just tested it with success.

@media only screen and (-webkit-min-device-pixel-ratio: 1) {
     ::i-block-chrome, .yourcssrule {
        your css property
    }
}
Jeff Clayton
  • 7,053
  • 1
  • 31
  • 36
Veka0881
  • 170
  • 1
  • 8
  • 3
    This hack is actually for both Chrome and Safari in different versions. It allows Chrome 10-24 (which nobody uses any more which is why people listed that it blocked Chrome) and Safari 5.1-6.0 only. It does not work for Safari 6.1 and newer however. At the time there was no better hack of its kind. – Jeff Clayton Jan 29 '15 at 16:00
  • People - please be careful. What these comments are describing is a method that does not work for versions of Safari in the last several years and only works for old versions. What that really means is that most people on the internet will not have the result you are looking for, but only people with older computers. This does not work for current operating systems. At this time most people have version 12 and higher (not 6.0 and lower, which were current only during the windows XP era.) – Jeff Clayton Dec 27 '18 at 01:04
12

Works on iOS 16 and macOS Ventura:

@supports (font: -apple-system-body) and (-webkit-appearance: none) {
  .body {
    background-color: red;
  }
}
Saber Hayati
  • 688
  • 12
  • 14
  • I really like this suggestion, because it's self-documenting and the very future proof. An "apple" font is very likely is only available in Safari now and for a long time to come... – bdombro Mar 07 '23 at 22:43
9

When using this safari-only filter I could target Safari (iOS and Mac), but exclude Chrome (and other browsers):

@supports (-webkit-backdrop-filter: blur(1px)) {
  .safari-only {
    background-color: rgb(76,80,84);
  }
}
iguido
  • 121
  • 1
  • 5
7

For those who want to implement a hack for Safari 7.0 and below, but not 7.1 and above -- use:

.myclass { (;property: value;); }
.myclass { [;property: value;]; }
Jeff Clayton
  • 7,053
  • 1
  • 31
  • 36
Stifboy
  • 247
  • 1
  • 3
  • 13
  • I gave this a try on Safari 7 and couldn't get it to work. Can you provide a working example by any chance? – Juicy Aug 15 '14 at 19:05
  • @Juicy: There are live tests of these at browserhacks.com -- They work on Safari 7 and lower, and also Chrome 28 and lower. (As mentioned in another posting iOS 7 and 8 maybe others as well use the Safari engine for Chrome, so Chrome and Safari on iPads are both really Safari) – Jeff Clayton Sep 22 '14 at 12:29
  • You may have been trying it in Safari 7.1, not Safari 7.0. It is valid for 7.0 and earlier, not 7.1 and newer. When this answer was posted, 7.0 was the latest version of Safari so it was true at that time. – Jeff Clayton Oct 19 '14 at 06:10
  • works for Safari for Windows (version 5.1.7 (7534.57.2)) – jave.web Jan 08 '15 at 00:46
  • works for Safari for Mac (Version 6.0.2 (7536.26.17)) – Merlin Nov 17 '17 at 09:02
6

Replace your class in (.myClass)

/* Safari only */ .myClass:not(:root:root) { enter code here }

Trinesh G D
  • 69
  • 1
  • 5
6

I like to use the following method:

var isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer/.test(navigator.vendor);
if (isSafari) { 
  $('head').append('<link rel="stylesheet" type="text/css" href="path/to/safari.css">') 
};
  • 1
    This JavaScript hack method requires the JQuery package to be installed. – Jeff Clayton Oct 23 '18 at 21:23
  • This is regarded as extremely bad practice, browser sniffing is very error-prone and leads to heaps of false positives. Don't do this, even media-query sniffing is very dirty but could be acceptable for very small changes. – Wannes Mar 12 '19 at 13:28
4

By the way, for any of you guys that just need to target Safari on mobiles, just add a media query to this hack:

@media screen and (max-width: 767px) {
    _::-webkit-full-page-media, _:future, :root .safari_only {
        padding: 10px; //or any property you need
    }
}

And don't forget to add the .safari_only class to the element you want to target, example:

<div class='safari_only'> This div will have a padding:10px in a mobile with Safari  </div>
Juanma Menendez
  • 17,253
  • 7
  • 59
  • 56
4

This works:

@media not all and (min-resolution:.001dpcm) { 
  @media {
    /* your code for Safari Desktop & Mobile */
    body {
      background-color: red;
      color: blue;
    }
    /* end */
  }
}
Jorge Epuñan
  • 710
  • 7
  • 9
  • @heLL0 because dcpm unit is not supported by Safari at all (@media not all and...) https://caniuse.com/mdn-css_types_resolution_dpcm – Jorge Epuñan Sep 07 '20 at 13:11
  • 1
    I've been trying dozens of `@media` queries to target _iOS Safari Mobile_ but not _Firefox Desktop_ - this is the first one I've come across that has worked. Thank you. – Rounin Sep 11 '22 at 10:02
4

https://www.bram.us/2021/06/23/css-at-supports-rules-to-target-only-firefox-safari-chromium/#safari

@supports (background: -webkit-named-image(i)) {
// 
}

h1::after {
  content: "No";
  margin-left: .3em;
  color: red;
}

@supports (background: -webkit-named-image(i)) {
  h1::after {
    content: "Yes";
    color: green;
  }
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  
  <h1>Safari?</h1>

</body>
</html>
JBeen
  • 390
  • 4
  • 9
3

Note: If iOS-only is sufficient (if you're willing to sacrifice Safari desktop), then this works:

    @supports (-webkit-overflow-scrolling: touch) {
    /* CSS specific to iOS devices */ 
    }
Andrew
  • 3,825
  • 4
  • 30
  • 44
2

hi i ve made this and it worked for me

@media(max-width: 1920px){
    @media not all and (min-resolution:.001dpcm) {

        .photo_row2 {
            margin-left: 5.5% !important;
        }
    }
}

@media(max-width: 1680px){
    @media not all and (min-resolution:.001dpcm) {

        .photo_row2 {
            margin-left: 15% !important;
        }

    }
}

@media(max-width: 1600px){
    @media not all and (min-resolution:.001dpcm) {

        .photo_row2 {
            margin-left: 18% !important;
        }

    }
}


@media (max-width: 1440px) {
@media not all and (min-resolution:.001dpcm) {

        .photo_row2 {
            margin-left: 24.5% !important;
        }

    }

}


@media (max-width: 1024px) {
@media not all and (min-resolution:.001dpcm) {
    @media {
        .photo_row2 {
            margin-left: -11% !important;
        }

    }

}
ChiiiN
  • 31
  • 1
1

You can use a media-query hack to select Safari 6.1-7.0 from other browsers.

@media \\0 screen {}

Disclaimer: This hack also targets old Chrome versions (before July 2013).

Jeff Clayton
  • 7,053
  • 1
  • 31
  • 36
tzi
  • 8,719
  • 2
  • 25
  • 45
  • It doesn't work on newer Safari, the stats for this one are surgical however: Safari 6.1-7.0, Chrome 22-28 so still handy. – Jeff Clayton Jan 09 '16 at 01:59
1

Step 1: use https://modernizr.com/

Step 2: use the html class .regions to select only Safari

a { color: blue; }
html.regions a { color: green; }

Modernizr will add html classes to the DOM based on what the current browser supports. Safari supports regions http://caniuse.com/#feat=css-regions whereas other browsers do not (yet anyway). This method is also very effective in selecting different versions of IE. May the force be with you.

9ete
  • 3,692
  • 1
  • 34
  • 32
1

If you are looking Safari specific browser hack

I tried this, and it's working for me (only for Safari)

@supports (-webkit-hyphens:none){
        @content
}
    
@media not all and (min-resolution:.001dpcm)
    { @supports (-webkit-appearance:none) and (stroke-color:transparent) {
            @content
    }
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
sachin patil
  • 61
  • 1
  • 9
0

At the end I use a little JavaScript to achieve it:

if (navigator.vendor.startsWith('Apple'))
    document.documentElement.classList.add('on-apple');

then in my CSS to target Apple browser engine the selector will be:

.on-apple .my-class{
    ...
}
Daniel De León
  • 13,196
  • 5
  • 87
  • 72
0

Wordpress solution:

<?php
add_filter( 'body_class', 'browser_body_class' );

function browser_body_class( $classes ) {
    global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

    if( $is_lynx ) $classes[]       = 'lynx';
    elseif( $is_gecko ) $classes[]  = 'gecko';
    elseif( $is_opera ) $classes[]  = 'opera';
    elseif( $is_NS4 ) $classes[]    = 'ns4';
    elseif( $is_safari ) $classes[] = 'safari';
    elseif( $is_chrome ) $classes[] = 'chrome';
    elseif( $is_IE ) $classes[]     = 'ie';
    else $classes[] = 'unknown';

    if( $is_iphone ) $classes[] = 'iphone';
    return $classes;
}
?>
Tahi Reu
  • 558
  • 1
  • 8
  • 19
0

...practically only for iSF3.2+(mSF4+)

@media (-webkit-transition) {/*CSS*/}

Why? https://developer.mozilla.org/en-US/docs/Web/CSS/@media/-webkit-transition


...only for iSF10.1+(mSF10.12.1+)

@supports (-webkit-appearance:-apple-pay-button) {/*CSS*/}

Why? https://developer.apple.com/documentation/apple_pay_on_the_web/displaying_apple_pay_buttons_using_css


...only for iSF9+

@supports (-webkit-touch-callout:none) {/*CSS*/}

Why? https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-touch-callout

See https://stackoverflow.com/a/47818418/18938887

-1

It working 100% in safari..i tried

@media screen and (-webkit-min-device-pixel-ratio:0) 
{
::i-block-chrome, Class Name {your styles}
}
-1

I have tested and it worked for me

@media only screen and (-webkit-min-device-pixel-ratio: 1) {
 ::i-block-chrome, .myClass {
    height: 1070px !important;
  }
}
Atif Tariq
  • 2,650
  • 27
  • 34