Jump to main navigation


Applying CSS to Opera only

080815

[Edit: 080828] This hack targets only Opera 9 and below, but fails in (at least) v9.52. You should not use it unless you're specifically targeting Opera 9 and below.

If you ever need to apply certain styles to Opera only, there is an easy way that I found in Jeff Starr's post, 'CSS Hack Dumpster'. All you need to do is prefix your selector with html:first-child.

For example, let's see how you would add a left margin of 10px to all paragraphs in all browsers except Opera:

p {
  margin-left: 10px;
}

html:first-child p {
  margin-left: 0;
}

This hacks validates and seems reasonably safe. As always, it's a risk to use hacks in your CSS (what would happen if, say, Internet Explorer 12 matched html:first-child? Any websites using this hack wouldn't work correctly in IE12). I'm still going to use it though, it seems quite safe to me.

You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

7 comments to “Applying CSS to Opera only”

  1. #01 By hakukoneoptimointi, 080828 at 20:16

    html:first-child selector also applies to Mozilla based browsers

  2. #02 By dense13, 080828 at 21:39

    Are you sure? I don't think so. If you've tested it, can you tell me what version and OS are you using?

    But: I recently upgraded Opera to v9.52 and here html:first-child doesn't seem to have any effect. So I'll have to look for another Opera hack. I'll update this post as soon as I get a chance to have a look at this.

  3. #03 By hakukoneoptimointi, 080908 at 18:45

    You're right.. my bad. I tested it again with Firefox 3 on XP and
    Vista - didn't work. And it doesn't seem to work in Opera 9.51
    either. One Opera hack I've been using is: @media all and
    (min-width: 0px){ selector{property: value;} } Works with recent
    versions of Opera

  4. #04 By Alex Thomas, 081114 at 01:56

    This effects IE too...

  5. #05 By dense13, 081114 at 08:45

    I don't think so, which version of IE? I've tried 7, 6 and 5.5, and it doesn't affect any of those.

    But I insist: this technique is not useful anymore, because it won't work in newer versions of Opera. Which proves that using hacks is dangerous and should be avoided as much as possible. :)

  6. #06 By latinsydney, 110113 at 15:09

    thanks hakukoneoptimointi, the hack works on Opera 10.52

  7. #07 By Alan-A, 110123 at 01:18

    Hakukoneoptimointi's hack unfortunately does not work in Opera 11.
    A.

Additional content and navigation

Categories

Recent comments