What is the correct way to deal with css browser compatibility?

ぐ巨炮叔叔 提交于 2019-12-10 13:55:01

问题


Is it better to have a different CSS file for each user-agent or is it better to use CSS Hacks that only certain browsers can see?


回答1:


Neither.

The best is to write code that works in all browsers without the need of browser specific code or css hacks. It's of course not quite as easy to accomplish, which is why many people use the other methods.

The key is to avoid things that some browsers (very often Internet Explorer) has problems with. One such thing is to use padding rather than margin, because IE doesn't handle margin collapsing correctly.

Some methods that is in the border line of being hacks is using code that doesn't affect browsers that work correctly, but fixes problems for a specific browser. That could be things like specifying a height for an element that normally shouldn't need one, or specifying display:inline on a floating element.

The page Position is everything has examples of some bugs and suggested fixes. (Often the fix is some kind of hack, so you should of course also consider if you can avoid the problem altogether.)




回答2:


It's better to do neither.

A good css-reset and css that works the same cross-browser is a much better solution.

If your design absolutely precludes that, then (and only then) would I try hacks or IE conditional comments.

I haven't yet seen the need for mutliple css files (beyond a few IE6 corrections addressed via a conditional comment).




回答3:


Neither if possible. Now that the old Netscape, IE <= 6 etc. are not longer really that much in use, I try to use features which work in all those browsers (e.g. FF >= 2, IE >= 7, Chrome, Opera).




回答4:


Conditional comments for issues with Internet Explorer appear to be the norm. Combined with a little bit of JavaScript, like Dean Edward's ie7.js you can mitigate most cross browser issues without resorting to hacks within your CSS files.




回答5:


its better to use a different css files for Internet Explorer 6-7 (include them via conditional comments), and a hacks for other browsers.




回答6:


A sort of follow up is how to develop the single file that works.

The best approach that I've seen work is to start from nothing, slowly building it up and checking change by change that it's still compatible across your core browsers (especially the problematic ones).

When you get it fully working with one browser and then say "time to convert it" is when the pain really starts and where you have to start hacking.




回答7:


My approach using a PHP class to detect os, browser and browser version. You can target any version of almost any browser on any operating system.



来源:https://stackoverflow.com/questions/808902/what-is-the-correct-way-to-deal-with-css-browser-compatibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!