CSS - RGB 或 HEX 颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9802876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS - RGB or HEX for color
提问by Darren Sweeney
Possible Duplicate:
Are there any good reasons for using hex over decimal for RGB colour values in CSS?
Should one use RGB or HEX for colors in CSS style sheets?
CSS 样式表中的颜色应该使用 RGB 还是 HEX?
If one is favored more than the other, why?
如果一个比另一个更受青睐,为什么?
Is there a difference for future proofing, speed of processing, better compatibility between platforms/browsers etc
是否存在未来证明、处理速度、平台/浏览器之间更好的兼容性等方面的差异
采纳答案by Christofer Eliasson
I believe that it is mostly a matter of personal taste whether you should use RGB or HEX. Personally I use HEX, because it feels more convenient - it is easier to scan when reading the code and easier to copy from your preferred image editor. Also for colors like white (#fff), black (#000) it could save you a few bytes (not that it would make much difference though).
我相信您应该使用 RGB 还是 HEX 主要取决于个人喜好。我个人使用 HEX,因为它感觉更方便 - 阅读代码时更容易扫描,更容易从您喜欢的图像编辑器复制。同样对于像白色 (#fff)、黑色 (#000) 这样的颜色,它可以为您节省几个字节(虽然它不会有太大的不同)。
In the end it is really just two different representations of the same thing, and from a performance perspective I don't think it matters. At least there are loads of other things you can do to increase performance, that will have a greater impact on performance.
最后,它实际上只是同一事物的两种不同表现形式,从性能的角度来看,我认为这并不重要。至少你可以做很多其他事情来提高性能,这将对性能产生更大的影响。
回答by Marat Tanalin
Typically, you can copy a hex value from a graphics editor directly — from a singletext field. For RGB, you can't: RGB values are usually represented via threeseparate text fields. So hex values are at least easier for everyday use.
通常,您可以直接从图形编辑器复制十六进制值 — 从单个文本字段。对于 RGB,您不能:RGB 值通常通过三个单独的文本字段表示。所以十六进制值至少在日常使用中更容易。
But there was no wayfor a while to use hex notation for values with alpha transparency, so we were forced to use RGB notation via the rgba()
function.
但是有一段时间没有办法对具有alpha 透明度的值使用十六进制表示法,所以我们被迫通过rgba()
函数使用 RGB 表示法。
The CSS Color Module Level 4draft spec adds the 8-digit hexadecimal notation #RRGGBBAA
and #RGBA
: e. g. #00ffaa80
is equivalent to rgba(0, 255, 170, .5)
. The feature is available in Firefox 49+, Chromium 62+and Safari 10+ (WebKit bug), but neither in IE nor in Edge. According to caniuse, such notation is supported by 75% of browsers, so it probably cannot yet be used in the wild, though can be used in browser add-ons.
在CSS颜色模块级别4规范草案增加了8位十六进制表示法#RRGGBBAA
和#RGBA
:即 G。#00ffaa80
相当于rgba(0, 255, 170, .5)
。该功能在Firefox 49+、Chromium 62+和 Safari 10+(WebKit 错误)中可用,但在 IE 和 Edge 中均不可用。根据caniuse 的说法,75% 的浏览器都支持这种表示法,因此它可能还不能在野外使用,但可以在浏览器附加组件中使用。
回答by Usagi Ito
HEX is stable for an enterprise service. It's easy to maintain (preprocessor and work people friendly), and many browsers are supported.
HEX 对于企业服务是稳定的。它易于维护(预处理器和工作人员友好),并且支持许多浏览器。
But, color-name is not bad if you like. It's fun to choose from the color palette, and many colors are defined already.
但是,如果您愿意,颜色名称也不错。从调色板中进行选择很有趣,并且已经定义了许多颜色。
And so, it's an appendix. I'm using HLSAcolor pattern in my private experimentalwebsite now. see also if you have an interest to the new CSS3 draft: http://www.w3.org/TR/css3-color/.It's very intuitive if you know the color rule of the nature phenomenon.
所以,这是一个附录。我现在在我的私人实验网站中使用HLSA颜色模式。如果您对新的 CSS3 草案感兴趣,也可以看看:http: //www.w3.org/TR/css3-color/。如果您知道自然现象的颜色规则,那将非常直观。
回答by Rayraegah
If its for devices (VGA etc) -> use RGBA values, otherwise HEX HSL and pretty much anything works.
如果它用于设备(VGA 等)-> 使用 RGBA 值,否则 HEX HSL 和几乎任何东西都可以工作。