Browser-Specific CSS Hacks

refs:

https://www.wired.com/2010/02/browser-specific_css_hacks/#Firefox

 

Browser-specific CSS hacks have become a taboo among standards-aware web designers for good reason; ideally you shouldn’t need them. However, as long as the Internet Explorer 6 browser continues to hold significant market share, there will likely remain some cases where you need to use unfashionable CSS rules to accommodate outdated technology.

In all fairness, it’s not just Internet Explorer that necessitates these hacks. There are also times when you might need to target specific browsers for the technology it supports, which is why we’ve put together this comprehensive list of techniques to target specific browsers.

Because these hacks clutter your stylesheet and greatly complicate the process of maintaining your code, we strongly recommend to only use these techniques as a method of last resort. There’s a good chance many of these hacks will stop working at some point. Several of them exploit bugs in browsers we hope will be fixed in the future.

Still, for the times you need them, here is our list of browser-specific hacks.

Contents

  1. Internet Explorer
  2. Firefox
  3. Safari
  4. Opera
  5. Conclusion

Internet Explorer

The easiest and best way to target CSS rules to Internet Explorer only is to use conditional comments to load an extra IE-specific stylesheet. That way all your IE-specific rules are in one file and separate from your standards-compliant CSS rules.

To target IE using conditional comments, just add this line to the >head< tags of your HTML file:

<!--[if IE 6]><link rel="stylesheet" 
href="http://mysite.com/path/to/ie6.css" type="text/css" media="screen, 
projection"><![endif]-->

<!--[if IE 7]><link rel="stylesheet" 
href="http://mysite.com/path/to/ie7.css" type="text/css" media="screen, 
projection"><![endif]-->

These conditional comments will be ignored by every other browser so only IE 6 and IE 7 respectively will load these stylesheets. Now all you need to do is create the files on your server and override whatever CSS rules are messing with IE’s headers.

In fact this method works so well we aren’t even going to mention the * html hack or other older methods because there’s just no reason to use them any more.

Firefox

Firefox does a pretty good job of rendering web pages the way they are supposed to look, but every once in a while you’ll find some of the older versions doing something a bit wonky.

To target a rule at Firefox 1.5 and 2, use this hack:

body:empty #my-id {

    /* Firefox-specific rules go here */

}

The trick to this hack is the proposed CSS 3 :empty pseudo-class. The purpose of the :empty pseudo class is to allow you to target any element that has no child elements. However, Firefox 1.5 and 2.0 (and others based on those versions of Gecko) select the body even when the body has content. In other words this hack exploits a bug that was fixed in Firefox 3.

The big downside to using this hack is that it’s invalid CSS 2 and may not even make it into the CSS 3 specification, so your stylesheets won’t validate. However, Firefox is pretty good at backwards compatibility, so this may not be an issue for a while. Your mileage may vary.

What if you need to target all versions of Firefox? To do that you can use a trick borrowed from Firefox extensions:

@-moz-document url-prefix() {

    #my-id { font-size: 100%; }

}

The -moz prefix (which is also used for some of the cutting-edge CSS support in Firefox 3) is combined with the -document url-prefix() selector, which is how Firefox add-ons define their styles.

Since other browsers don’t use this add-on contention, the result is a rule only Firefox will apply.

If you’re looking to target only Firefox 3, you’re out of luck. At the time of this writing, Firefox 3 seems immune to any browser-specific hack.

We may have missed something, though. If you know of a way to target Firefox 3, be sure to add it.

FIREFOX 3 SPECIFIC CSS (from sethviebrock)

For example, to set Firefox3-specific CSS properties for an image with an id of “header_right_gif”, i.e.

<img id="header_right_gif" src="header_right.gif" align="right">

you’d use the following code:

/* Firefox3-specific CSS property for header_right.gif */

@-moz-document url-prefix() {

    #header_right_gif, x:-moz-any-link, x:default {

        position: relative;

        top: -1px;

    }

}



FIREFOX 3 SPECIFIC CSS with ASP.Net (from Focus 44)

I ran into a need to target Firefox 3 specifically. My developer at Focus44.com wrote me the following code to be placed in the <head>. You can see I have added a Firefox 3 specific Style Sheet and individual styles in the example. It will only work for ASP.Net pages but I have seen similar tricks for php:

<%if (Request.UserAgent.ToLower().Contains("firefox/3")) { %>

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

	<style>



		*.style{ overflow:hidden; }

	</style>

<% } %>

Safari

As with Firefox 3, there’s no clear way to target specific versions of Safari, but there is a very ugly trick you can use to apply rules to only Safari 1 and 2:

#my-id { color:red; }

#my-id { color:black;# }

The first rule will set the font to red in all browsers. The second rule will set the font to black in every browser except Safari 1 & 2. This hack works because the first two releases of Safari had a bug where a hash mark after the semicolon caused Safari to choke.

This is probably the ugliest hack in this tutorial, so use it sparingly if at all. Also note that Safari 1 and 2 will choke on every rule after the “#” so put these at the bottom of your stylesheet.

If you need to target Safari in general (regardless of which version) use the same prefix trick we used with Firefox.

In Safari’s case (or any other webkit browser), the rule looks like this:

@media screen and (-webkit-min-device-pixel-ratio:0) {

    #my-id { height: 100%; }

}

The downside to this hack is that it also applies to Opera 9+. However you can retain the Safari-only aspect by combining it with the Opera-only rule below.

Opera

Generally speaking Opera doesn’t require many CSS hack since it’s perhaps the most standards compliant of all the browsers. In fact, if you find something is rendering poorly in Opera, there’s a good chance the error is on your end, not the browser’s.

But, should you ever need to target Opera, we have a way. This one comes courtesy of Neal Grosskopf. Grasskopf also has a comprehensive list of other browser hacks, including some conditional comment hacks we try to avoid.

To target Opera, use this rule:

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

    #my-id { clear:right; }

}

As Gosskopf notes in his write up, this is one of the weakest hacks since it isn’t really targeting Opera, it’s targeting all browsers that support -min-device-pixel-ratio that aren’t using the webkit rendering engine. At the moment that means Opera, but eventually Firefox will add support for -min-device-pixel-ratio which means, eventually, it too will be affected by this hack.

Conclusion

CSS hacks are just that — hacks. In general you should make every effort to avoid using them.

But for those times when you’ve tried everything and already pulled all the hair out of your head, hack away

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值