tags:

views:

53

answers:

2

Is it possible to add a block of css that I only want to be displayed in Safari and no other browsers?

A: 

Using the UserAgent string, you can check for Safari and !Chrome. Both use the WebKit Renderer and both have Safari in the UA string, but Chrome also has 'Chrome'. To be honest, I'd just check for Webkit and code to that because who knows what other WebKit browser put in their UA strings.

Safari :

Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-HK) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5

Chrome :

Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/540.0 (KHTML, like Gecko) Ubuntu/10.10 Chrome/8.1.0.0 Safari/540.0

josh.trow
The UserAgent string can be spoofed very easily. This is not a reliable approach.
stevelove
I'm not saying its the best method, but to find SAFARI ONLY is nigh impossible so you take what you can get.
josh.trow
does css use the useragent to check which browser?
Ascherer
No, I think you would have to use a JS check in your code and include the appropriate style sheet.
josh.trow
+1  A: 

Here's an example which would set the font colour of your site to green if your browser is safari or opera (both share the common webkit).

@media screen and (-webkit-min-device-pixel-ratio:0) {
body {
color:green; /* Safari only */
}
}

This is taken from another post here

Brian Scott
I assume you meant Chrome, not Opera. Chrome uses the WebKit engine, Opera uses their own engine (called Presto).
jhurshman
@jhurshman, yes I did, thanks (it's getting late on a Friday :-) )
Brian Scott