tags:

views:

4268

answers:

13

Hello!

Is there a way to make some CSS rules visible only for Opera (9.5 +)?

Thank you!

A: 

The only way I can think of is to check the user agent and only reference the style sheet when it's an opera browser. Since the user agent can be messed with this might not be 100% reliable.

JoshBerke
+1  A: 

Not in any way I would recommend.

Check for Javascript or PHP browser sniffers on Google. Some may be so outdated that you need to add detection for Opera 9.5+, however.

Browser sniffers (for styling) are generally bad practice.

Also, note that Opera 9.5+ gives users the option of masking their browser as IE, rendering any kind of sniffing useless.

Edit: As you can see in another answer, there is window.opera.version(). I didn't know the window.opera object contained this information. HOWEVER, you should probably look to see if this object is still available when someone has set Opera to be seen as IE or some other browser.

jonwd7
Does that "masquerade as IE" option include hiding the `window.opera` variable? If not, detection should still be possible via JavaScript, at least.
Ben Blank
If the `window.opera` object is the only thing to go on, you cannot differentiate between Opera 9.5+ and earlier versions. The asker was quoting Opera 9.5+ specifically so I am not sure if he only CARES about 9.5+ or if he specifically needs to target 9.5+. Will edit my answer.
jonwd7
Of course, had no idea there was `window.opera.version()`… Shows how much I care about Opera. :)
jonwd7
A: 

You can use javascript to write out a <link> to include a specific CSS file.

if (navigator.userAgent.indexOf(’Opera’) != -1) {
    document.write(””);
}
else {
    document.write(””);
}

For Opera 7 you can use this:

/*Visible to only Opera*/
@media all and (min-width: 0) {
    /* css rules here */
}

However, it's generally bad practice to do styling based on browser-sniffing.

amr
I certainly don't want to do browser sniffing, or any kind of JavaScript or server-side detection.I need to make some rules visible only for opera to display CSS rounded corners on elements with border.
Calin Don
Other browsers have joined Media Query party, so this hack is now out of date.
porneL
And it's therefore an excellent example of why you should avoid CSS hacks.
Ben Blank
+2  A: 

There's no pure-CSS hack that I know of.

If you have to hack, you'll need to use JavaScript:

// remember to limit maximum version, because hacking all future versions
// will eventually break the page 
if (window.opera && window.opera.version() < 10)     
{
   document.documentElement.className += ' opera9';
}

and in CSS:

.opera9 .element-to-hack { /*declarations for opera <= 9 only*/ }

But please double-check CSS spec first to ensure that what you're hacking is actually a bug. Opera 10 has full CSS2.1 support and passes all Acid tests, so if something doesn't appear right, it might be because of other reasons (error somewhere else in the code, detail or corner case you shouldn't rely on, etc.)

porneL
A: 

This hack works for the latest Opera:

 @media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) {
       #id {css rule}
 }

It doesn't touch any other browser as far as i tested, but this may be actual for several months, with web technologies boom etc.

certainlyakey
+5  A: 

Do not think "detect Opera".

Think "detect browsers that do not support feature x". For example, this JavaScript statement lets you detect browsers that support moz-border-radius:

typeof (getComputedStyle(document.body, '').MozBorderRadius)=='string'

and this is the equivalent for WebKit-based browsers (Safari, Chrome):

typeof (getComputedStyle(document.body, '').WebKitBorderRadius)=='string'

Putting that together, we can come up with something like

function detectBorderRadiusSupport(){
    var styleObj;
    if( window.getComputedStyle ){
     styleObj=window.getComputedStyle(document.body, '');
    }else{
     styleObj=document.body.currentStyle;
    }
    return typeof styleObj.BorderRadius != 'undefined' || typeof styleObj.MozBorderRadius != 'undefined' || typeof styleObj.WebKitBorderRadius != 'undefined';
}

// the below must be inside code that runs when document.body exists, for example from onload/document.ready/DOMContentLoaded event or inline in body

if(!detectBorderRadiusSupport())document.body.className+=' fakeBorderRadius';

CSS:

body.fakeBorderRadius .roundMyCorners{
    /* CSS for Opera and others to emulate rounded corners goes here, 
    typically various background-image and background-position properties */
}

Caveat: untested :-p

hallvors
I just want to add that when a browser supposedly supports a feature, it doesn't mean its implementation is flawless. So in some cases you might wanna disable a feature for a specific browser anyway.Like `box-shadow` in Opera 10.53 still has some redraw issues. I see no other way than hiding it from Opera in this case. Do you?
RamboNo5
If you can get away with ignoring a temporary browser bug I'd encourage you to do so. So if the issue is purely cosmetic without impact on functionality, I'd encourage you to do absolutely nothing about it. Even if it makes Opera look worse to a few users who come across the problem, I think it's better to avoid workarounds and hacks that will cause problems further on.
hallvors
+1  A: 

You could use Modernizr ( http://www.modernizr.com/ ) to detect CSS features you want to use – it applies class names to the body element so you can then construct your CSS accordingly.

riddle
+2  A: 

<link href="opera.css" rel="stylesheet" type="text/opera" media="all" />

sample here

valentin
A: 

<link href="opera.css" rel="stylesheet" type="text/opera" media="all" />

Though this solution is rather a CSS hack and there is no guarantee it will be supported in future releases of Opera. You might also consider to use the following solution:

@media all and (-webkit-min-device-pixel-ratio:10000),not all and (-webkit-min-device-pixel-ratio:0) {

.element{/*style for opera only*/}

}

http://bookmarks-online.net/link/1308/css-including-style-for-opera-only

vpodkamenyi
A: 

I have tried EVERY CSS "hack" that is supposed to work for opera 10.10 and NONE OF THEM WORK EXCEPT THIS ONE :

 @media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) {
       #id {css rule}
 }

It was posted by certainlyakey and i don't have enough of a rep yet to vote it up but this is THE answer. I tested it in Safari, Chrome, ie7, ie8, firefox 3.6 and of course opera and it only effects opera. I love opera, I use it as my personal browser (not for dev, thats firefox's job) and I can attest there are aren't alot of situations when you have to use a hack on Opera but sometimes it is necessary when all other methods fail.

This hack FTW! Thanks again certainlyakey.

Tony C.
A: 

The Hack

@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) { #id {css rule} }

Worked wunderfull! Thanks!

Sebastian
+1  A: 

I wrote a jQuery $.support extension for detecting css property support.

http://gist.github.com/556448


Additionally i wrote a little snippet to make really little vendor hacks:

// Sets browser infos as html.className
var params = [];
$.each($.browser, function(k, v) {
  var pat = /^[a-z].*/i;
  if(pat.test(k)) { params.push(k); }
});
params = params.join(' ');
$('html').addClass(params);

This results for example in:

<html lang="de" class="webkit version safari">
or
<html lang="de" class="opera version">

In your Stylesheets use it this way:

html.opera #content_lock {
  background:rgba(0,0,0,0.33);
}
JackFuchs
A: 

works great for Opera 10.63

noindex:-o-prefocus, .class {
  color:#fff;
}
vitaly.batonov