在 CSS 中使用颜色名称代替颜色代码有什么缺点吗? [英] Are there any cons to using color names in place of color codes in CSS?

查看:19
本文介绍了在 CSS 中使用颜色名称代替颜色代码有什么缺点吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如写red#cc0000更有效率.它的字符更少,占用空间更少,更容易记住.

For example writing red is more efficient than #cc0000. It has less characters, takes up less space, and is easier to remember.

在十六进制代码或 RGB 值上使用 颜色名称 有什么缺点吗?这包括在多开发者环境中进行编程.

Are there any down sides to using color names over hex codes or RGB values? This includes programming in a multi-developer environment.

推荐答案

不同的浏览器可能无法就某些颜色名称的含义达成一致.没有所有 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.

这篇关于在 CSS 中使用颜色名称代替颜色代码有什么缺点吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆