Make CSS apply only for Opera 11? [duplicate]
Asked Answered
K

7

9

Is there a way to make some CSS rules apply only for Opera (11)?

Krasnoyarsk answered 10/1, 2011 at 14:45 Comment(0)
M
11
body {background:0} /* default */
@media not screen and (1) {
body {background:red} /* OP 11 */
}
@media not screen and (orientation) {
body {background:green} /* for the earlier versions of Opera that pick the first media query's rule + chrome/safari */
}

Browsers tested:

  • red: Opera 11
  • green: Opera 10 and 10.5 + WebKit browsers
  • none: Opera 9.26 + Firefox 3.6 + IE9

It's related to the error-handling and also the fact that NOT negates the global result (WebKit browsers don't evaluate orientation correctly without a valid value). Since orientation is supported in presto 2.7 the second media query is FALSE.

The false orientation hack sounds like a good name to me.

Mehetabel answered 18/1, 2011 at 13:59 Comment(0)
C
5

Is there a good reason you want to do this?

I'd always recommend against doing browser detection. In almost every case where people want to use it, it's a better idea to use feature detection instead. If you find out if the feature you want is supported, then you'll automatically start supporting new versions of other browsers when they catch up, without having to constantly work to keep your site up to date as you would with browser detection scripts.

For feature detection, one of the best tools I can suggest is to use Modernizr.

For browser detection - especially brand a new browser like Opera11 - I can't really suggest anything that will be foolproof. The correct answer is to look at the User Agent string, but that can easily be changed by the user to spoof another browser (and often is, especially by Opera users, as they're the one most often trying to get around sites that do browser detection and try to block them)

Climax answered 10/1, 2011 at 15:13 Comment(1)
That's definetly a good point, and i agree you.. but I try to avoid javascript as much as possible.. and Im curious if someone have found a hack for the Opera 11 yetKrasnoyarsk
S
2

You could try using http://www.headjs.com/

Saccharo answered 10/1, 2011 at 14:58 Comment(0)
S
2

I don't know of a CSS-only way as Opera 11 is still VERY new.

You can either use server-side languages like PHP to detect the User Agent of the browser or you can use the freely available Javascript solution CSS Browser Selector.

The above solution does not yet include Opera 11, so let's check Opera 11's User Agent string by checking their references. (Actually they have their own article on how to detect opera)

Opera/9.80 (Windows NT 5.1; U; en) Presto/2.7.39 Version/11.00

When you now look at the Javascript of the above mentioned CSS Browser selector you can see it is just reading out the navigator.userAgent and comparing it to many variations - just add your Opera 11 variation and you are good to go (or wait until the developer updates the javascript - or even better, update the script and tell the author about it!).

Selfhood answered 10/1, 2011 at 14:58 Comment(0)
M
2

Here you go......

/* Opera */
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0)
{
here you can display anything you want just for opera
}
Monamonachal answered 17/1, 2011 at 2:52 Comment(0)
H
2

The following style would indeed only get rendered in Opera. See Webmonkeys blog post for details:

@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) {
    #myid { background-color: #F00; }
}

But keep in mind, that all sort of CSS hacks might not work anymore in the future. So I would strongly recommend you to add the styles dynamic only for Opera with jQuery (jQuery.browser).

Halfassed answered 17/1, 2011 at 21:5 Comment(2)
Another answer gave the same code already. but I'm giving you the point for including the reference back to the blog. I really hope I never see a site actually using this hack, though. :-sClimax
As I clicked on answer and wrote it, the_'s answer wasn't exactly like what I wanted to write. I've seen that it is a duplicate but I decided to leave it just for the reference.Halfassed
A
1

The read-only pseudo-class is a simple filter for Opera:

#foo:read-only { overflow: auto; }
Acyclic answered 12/6, 2013 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.