What is better: CSS hacks or browser detection?

2019-01-21 02:29发布

Commonly when I look around the Internet, I find that people are generally using CSS hacks to make their website look the same in all browsers. Personally, I have found this to be quite time consuming to find all of these hacks and test them; each change you make you have to test in 4+ browsers to make sure it didn't break anything else.

About a year ago, I looked around the Internet for what other major sites are using (Yahoo, Google, BBC, etc) and found that most of them are doing some form of browser detection (JS, HTML if statements, server based). I have started doing this as well. On almost all of the sites I have worked on recently, I use jQuery, so I use the built in browser detection.

Is there a reason you use or don't use either of these?

15条回答
爷的心禁止访问
2楼-- · 2019-01-21 03:24

hacks will always add to your work efforts and work efforts should be optimized

first you add the hacks and then start worrying about how they behave on different browsers and different machine.

instead you can rely on vendor specific css extensions http://reference.sitepoint.com/css/vendorspecific

查看更多
Lonely孤独者°
3楼-- · 2019-01-21 03:25

Personally, I have found this to be quite time consuming to find all of these hacks and test them; each change you make you have to test in 4+ browsers to make sure it didn't break anything else.

You shouldn't have to test 'proper' CSS hacks on every browser.

The idea is that you write standards-compliant code, and then add specific hacks to target one and only one browser (or rendering engine) that makes a mistake. For example, writing "* html #myelement" to target an exception for IE6 only: no other browser will ever be affected by that hack so there's no need to test it exhaustively. It could only go wrong if some new obscure browser came along and made exactly the same mistake as IE6, which is extremely unlikely, not your fault, and something you could expect to get fixed quickly.

There are some things that call themselves CSS hacks but which use invalid constructs, such as the "_propertyname" hack. This can break across browsers because when you use invalid code every browser might interpret it differently. Don't use these.

To be honest, it is in any case not the issue it once was, primarily because IE5 is dead. If you use a Standards Mode doctype and write to the standards, it will mostly work in the current round of browsers. The only real remaining problem case is IE6, which you can target with "* html"; it is unlikely you will need much more in the way of CSS hacks than that. The days of the Box Model Hack are, thankfully, over.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-21 03:28

In 6 years of writing HTML and CSS for a living, the vast majority of my CSS issues have come from Internet Explorer.

As pointed out in other answers, you can use conditional comments to serve additional stylesheets to IE (or to add a class to the <body> or <html>`` element, if you don’t like multiple stylesheets). Unlike CSS hacks, conditional comments are explicit and supported. Unlike trying to detect IE from theuser-agent string`, they’re guaranteed to work.

As for non-IE CSS issues, I’ve never found one that was worth browser detection.

查看更多
The star\"
5楼-- · 2019-01-21 03:31

Well hacks are quicker for the browser, but browser detection gives better readablity in your CSS if you structure it right. If you can make browser detection and at the same time can share the CSS between the browsers, and only have the different css in seperate files or whatever, then I would use browser detection - as this is something a newbie can understand, where CSS hacks is hard to understand if you don't know them

查看更多
贪生不怕死
6楼-- · 2019-01-21 03:31

[My approach][1] using a PHP class to detect os, browser and browser version.

[1]: My approach using a PHP class to detect os, browser and browser version http://reinholdweber.com/css/css-hacks-browser-version-detection-a-new-approach/

查看更多
虎瘦雄心在
7楼-- · 2019-01-21 03:32

I try not to use either. In a lot of cases the issues that IE have can be avoided by simplifying the structure of your markup somewhat.

It also helps if you use a decent CSS reset like Eric Meyer's.

I am also slowly but surely dropping support for IE6 as a matter of principle, especially given the latest security issues with IE6 and IE7 - we're not going to change people's browsing habits and browser preferences if we keep supporting crappy browsers.

查看更多
登录 后发表回答