color - CSS(层叠样式表) 编辑
color
CSS属性设置颜色值的前景色以及文本装饰,并设置currentcolor
值。currentcolor
可以对其他颜色属性用作不直接的值,比如border-color
。对于在HTML中使用颜色的概述,参考使用CSS对HTML元素实现颜色。语法
/* 关键词 */
color: currentcolor;
/* <named-color>值 */
color: red;
color: orange;
color: tan;
color: rebeccapurple;
/* <hex-color>值 */
color: #090;
color: #009900;
color: #090a;
color: #009900aa;
/* <rgb()>值 */
color: rgb(34, 12, 64, 0.6);
color: rgba(34, 12, 64, 0.6);
color: rgb(34 12 64 / 0.6);
color: rgba(34 12 64 / 0.3);
color: rgb(34.0 12 64 / 60%);
color: rgba(34.6 12 64 / 30%);
/* <hsl()>值 */
color: hsl(30, 100%, 50%, 0.6);
color: hsla(30, 100%, 50%, 0.6);
color: hsl(30 100% 50% / 0.6);
color: hsla(30 100% 50% / 0.6);
color: hsl(30.0 100% 50% / 60%);
color: hsla(30.2 100% 50% / 60%);
/* 全局值 */
color: inherit;
color: initial;
color: unset;
color
属性被指定为一个单个的 <color>
值。
注意值必须是规则的 color
。它不可以是<gradient>
(实际上为<image>
的一部分)。
值
<color>
- 设置元素的文本或装饰的颜色。
Accessibility concerns
It is important to ensure that the contrast ratio between the color of the text and the background the text is placed over is high enough that people experiencing low vision conditions will be able to read the content of the page.
Color contrast ratio is determined by comparing the luminosity of the text and background color values. In order to meet current Web Content Accessibility Guidelines (WCAG), a ratio of 4.5:1 is required for text content and 3:1 for larger text such as headings. Large text is defined as 18.66px and bold or larger, or 24px or larger.
- WebAIM: Color Contrast Checker
- MDN Understanding WCAG, Guideline 1.4 explanations
- Understanding Success Criterion 1.4.3 | W3C Understanding WCAG 2.0
正规定义
初始值 | Varies from one browser to another |
---|---|
适用元素 | all elements. It also applies to ::first-letter and ::first-line . |
是否是继承属性 | yes |
计算值 | If the value is translucent, the computed value will be the rgba() corresponding one. If it isn't, it will be the rgb() corresponding one. The transparent keyword maps to rgba(0,0,0,0) . |
Animation type | a color |
正规语法
<color>where
<color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
where
<rgb()> = rgb( <percentage>{3} [ / <alpha-value> ]? ) | rgb( <number>{3} [ / <alpha-value> ]? ) | rgb( <percentage>#{3} , <alpha-value>? ) | rgb( <number>#{3} , <alpha-value>? )
<rgba()> = rgba( <percentage>{3} [ / <alpha-value> ]? ) | rgba( <number>{3} [ / <alpha-value> ]? ) | rgba( <percentage>#{3} , <alpha-value>? ) | rgba( <number>#{3} , <alpha-value>? )
<hsl()> = hsl( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsl( <hue>, <percentage>, <percentage>, <alpha-value>? )
<hsla()> = hsla( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsla( <hue>, <percentage>, <percentage>, <alpha-value>? )where
<alpha-value> = <number> | <percentage>
<hue> = <number> | <angle>
示例
使文本为红色
以下方式可以使得文本显示为红色:
p { color: red; }
p { color: #f00; }
p { color: #ff0000; }
p { color: rgb(255,0,0); }
p { color: rgb(100%, 0%, 0%); }
p { color: hsl(0, 100%, 50%); }
/* 50%透明 */
p { color: #ff000080; }
p { color: rgba(255, 0, 0, 0.5); }
p { color: hsla(0, 100%, 50%, 0.5); }
指定
Specification | Status | Comment |
---|---|---|
CSS Color Module Level 4 color | Working Draft | Adds commaless syntaxes for the rgb() , rgba() , hsl() , and hsla() functions. Allows alpha values in rgb() and hsl() , turning rgba() and hsla() into (deprecated) aliases for them.Adds color keyword rebeccapurple .Adds 4- and 8-digit hex color values, where the last digit(s) represents the alpha value. Adds hwb() , device-cmyk() , and color() functions. |
CSS Color Module Level 3 color | Recommendation | Deprecates system-colors. Adds SVG colors. Adds the rgba() , hsl() , and hsla() functions. |
CSS Level 2 (Revision 1) color | Recommendation | Adds the orange color and the system colors. |
CSS Level 1 color | Recommendation | Initial definition. |
浏览器兼容性
BCD tables only load in the browser
另请参阅
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论