在线文档教程

颜色 | color

color

colorCSS属性设置元素的文本内容和文本修饰的前景色值。它还设置currentcolor值,可用作其他属性的间接值,并且是其他颜色属性(如边框颜色)的默认值。

有关在HTML中使用颜色的概述,请参阅使用CSS将颜色应用于HTML元素

/* Keyword values */ color: currentcolor; /* <named-color> values */ color: red; color: orange; color: tan; color: rebeccapurple; /* <hex-color> values */ color: #090; color: #009900; color: #090a; color: #009900aa; /* <rgb()> values */ 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 /* <hsl()> values */ 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 /* Global values */ color: inherit; color: initial; color: unset;

请注意,该值必须是统一的color。它不能是一个<gradient>,这实际上是一种<image>类型。

初始值Varies from one browser to another
适用元素all elements. It also applies to ::first-letter and ::first-line.
是否是继承属性yes
适用媒体visual
计算值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 typea color
正规顺序the unique non-ambiguous order defined by the formal grammar

语法

color属性被指定为单个color值。

<color>设置元素的文本和装饰部分的颜色。

正式语法

<color>where <color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color> where <rgb()> = rgb( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] ) <rgba()> = rgba( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] ) <hsl()> = hsl( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <hue>, <percentage>, <percentage>, <alpha-value>? ] ) <hsla()> = hsla( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <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% translucent */ p { color: rgba(255, 0, 0, 0.5 } p { color: hsla(0, 100%, 50%, 0.5 }

规范

SpecificationStatusComment
CSS Color Module Level 4The definition of 'color' in that specification.Editor's DraftAdds 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 TransitionsThe definition of 'color' in that specification.Working DraftDefines color as animatable.
CSS Color Module Level 3The definition of 'color' in that specification.RecommendationDeprecates system-colors. Adds SVG colors. Adds the rgba(), hsl(), and hsla() functions.
CSS Level 2 (Revision 1)The definition of 'color' in that specification.RecommendationAdds the orange color and the system colors.
CSS Level 1The definition of 'color' in that specification.RecommendationInitial definition.

浏览器兼容性

FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
keywords colors1.01.0 (1.0)3.013.51.0 (85)
#RRGGBB, #RGB1.01.0 (1.0)3.03.51.0 (85)
rgb()1.01.0 (1.0)4.03.51.0 (85)
hsl()1.01.0 (1.5)9.09.53.1 (525)
rgba(), hsla()1.03.0 (1.9)9.010.03.1 (525)
currentcolor1.01.5 (1.8)9.09.54.0 (528)
transparent1.03.0 (1.9)9.0210.03.1 (525)
rebeccapurple38.033 (33)1125.07.1
#RRGGBBAA, #RGBANo support349 (49)?No support49.1
rgba() and hsla() as aliases of rgb() and hsl() Space-separated function parameters rather than commas Percentages for alpha values Angles for the hue component in hsl() colors.?52 (52)???

FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support(Yes)(Yes)(Yes)(Yes)(Yes)
rgba(), hsla()(Yes)(Yes)(Yes)(Yes)(Yes)
rebeccapurple(Yes)33.0 (33)??8
#RRGGBBAA, #RGBANo support349.0 (49)No supportNo support?
rgba() and hsla() as aliases of rgb() and hsl() Space-separated function parameters rather than commas Percentages for alpha values Angles for the hue component in hsl() colors.?52.0 (52)???