Strict vs Hacky CSS - 哪个更可取

发布于 2024-10-12 18:52:24 字数 536 浏览 2 评论 0原文

使用 CSS 时,很快就会意识到某些东西不跨浏览器友好。

例如,当我想要一个半透明的 png 时,我必须给 IE 一些可笑的东西,例如:
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(...);

我遇到了问题任何使用 css 超过 20 分钟的人都会遇到过定位和伪选择器等。

  • 为了解决问题,有时我开始决定只做所有浏览器支持的事情,并以相同(或至少相当相似)的方式支持。
  • 其他时候我只是加入条件注释并为 IE 提供它自己的样式表。
  • 然后在其他情况下,我只是使用 css hacks 来排除某些浏览器读取特定位(例如旧的“* html {}”或“html>/**/body {}< /code>")

所以我的问题是,哪个是最好的选择。第一个是相当有限的,你最终会开发 IE6 能够做的事情,但其他两个感觉有点麻烦。那么在编程实践、渲染效率等方面的赌注是什么?

It doesn't take long to realise when using css that certain things are not cross-browser friendly.

For example, when I wanted a semi-transparent png I had to give IE something ridiculous like:
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(...);

I've had problems with positioning and pseudo-selectors etc. as anyone who has played around with css for more than 20 minutes will have encountered.

  • To solve problems sometimes I start out deciding that I will only do what all the browsers support and support in the same (or at least a fairly similar) way.
  • Other times I just chuck in conditional comments and give IE it's own stylesheet.
  • And then on other occasions I simply use css hacks to exclude certain browsers from reading particular bits (like the old "* html {}" or "html>/**/body {}")

So my question is, which is the best option. The first is pretty limiting and you end up developing for what IE6 is capable of doing but the other two feel a bit mucky. So which is the bets in terms of programming practices, rendering efficiency etc.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入怼 2024-10-19 18:52:24

简短回答:

  • 尽最大努力遵守标准
  • 发挥创意
  • 停止使用 hack,开始使用条件注释

从长远来看,CSS hack 只会污染您的样式表,并使它们更难以维护。尽可能避免使用它们,而是使用条件注释和 IE 特定的样式表(因为大多数 hack 都迎合 IE)。我可以保证 CC 比 hack 更肮脏,如果有的话。

您不必一直受到 IE 的限制。有一个称为渐进增强的概念,这意味着您可以使您的页面在支持高级样式等的现代浏览器中看起来稍微漂亮一些。如今,使用诸如 border-radius 这样的 CSS3 属性并没有什么问题,因为浏览器供应商正在(相对)快速地为他们的产品提供对这些属性的支持。

但还有客户,这将是一个不同的讨论话题。不过,我想说的是,要取得平衡:教育你的客户并发挥创造力,但不要尝试做任何太聪明或花哨的事情。请避免 CSS hack,除非是凌晨 4 点半,并且您正在喝下一罐又一罐啤酒,并且只想在任何情况下完成某件事。

Short answer:

  • Do your best to adhere to standards
  • Be creative
  • Stop using hacks and start using conditional comments

In the long run, CSS hacks are just going to pollute your stylesheets and make them more difficult to maintain. Avoid them wherever you can, and use conditional comments with IE-specific stylesheets instead (since most hacks cater to IE). I can guarantee that CCs are much less mucky than hacks, if mucky at all.

You do not have to be limited by IE all the time. There's a concept called progressive enhancement, which means you can make your pages look slightly prettier in modern browsers that support advanced styling, and so on. There's nothing wrong with using CSS3 properties like border-radius today, since browser vendors are working (relatively) quickly to bring support for these to their products.

But then there are clients, which would be a different topic of discussion. I would say, though, strike a balance: educate your clients and be creative, but don't try to do anything too smart or fancy. And please, just avoid CSS hacks unless it's half past 4 in the morning and you're downing can after can of beer and just want to get something done in whatever circumstance.

挽容 2024-10-19 18:52:24

我总是更喜欢带有 IE 修复的条件注释。这些黑客行为与 IE 修复解析的行为不一致,因此黑客行为不再起作用,但随后没有修复黑客所补偿的内容。而且,它们可能会干扰其他 CSS 解析器。

I always prefer conditional comments with IE fixes. The hacks are not consistent with IE fixing the parsing so the hack doesn't work anymore but then not fixing the thing the hack was compensating. Also, they could mess with other CSS parsers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文