且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有任何缺点使用颜色名称代替CSS中的颜色代码?

更新时间:2023-01-27 09:45:13

不同的浏览器可能不同意某些颜色名称的含义。没有名称为所有1600万24位颜色。事实上,只有17个W3C标准颜色名称。

Different browsers may not agree on what some color names mean. There are not names for all 16 million 24-bit colors. In fact there are only 17 W3C-standard color names. It's probably OK to use those.

我个人在构建时使用模板系统预处理我的CSS文件,这样我可以保留一套标准的网站颜色并通过名称引用它们。这样我得到了两个世界的***的:我知道我的RGB颜色值是什么,但我可以在CSS中使用更简单的名称。

Personally I use a templating system at build time to pre-process my CSS files, so that I can keep a standard set of site colors and refer to them by name. That way I get the best of both worlds: I know exactly what my RGB color values are, but I can use simpler names in the CSS.

(当然,仍然无法准确地知道在给定用户的浏览器上颜色的外观。)

(Of course, it's still not possible to know exactly how a color will look on a given user's browser.)

编辑—在写这个答案的5年里,像Less和Sass这样的预处理器变得非常普遍。这些提供了一些非常复杂的工具来管理CSS源中的颜色(和许多其他东西)。

edit — in the 5 years since this answer was written, preprocessors like Less and Sass have become pretty common. Those provide some very sophisticated tools for managing colors (and many other things) in CSS sources.