Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS color property

CSS color property

Example

Set text-color for different elements:

body

{

color:

red

;

}

h1

{

color:

#00ff00

;

}

p

{

color:

rgb

(

0

,

0

,

255

)

;

}

Try it »


Property definition and usage

The color property specifies the color of text.

Default value: Not specified
Inherited: Is
Version: CSS1
JavaScript syntax: object.style.color="#FF0000"

Browser support

The numbers in the table indicate the first browser version that supports this property.

A number immediately before -webkit-, -ms-, or -moz- indicates the first browser version that supports that prefixed property.

property
color 1.0 3.0 1.0 1.0 3.5

Tips and Notes

** Tip:** Use a sensible combination of background and text colors to improve text readability.


Attribute values

Color values can be specified in the following ways:

Value Description Example
Color names Color names such as red, blue, brown, lightseagreen, etc., are case-insensitive.

   

color:red; /* red / color:black; / black / color:gray; / gray / color:white; / white / color:purple; / purple / | | Hexadecimal | Hexadecimal notation #RRGGBB and #RGB (e.g. #ff0000). The "#" is followed by 6 or 3 hexadecimal characters (0-9, A-F). |

#f03
#F03
#ff0033
#FF0033
rgb(255,0,51)
rgb(255, 0, 51)
| | RGB, red-green-blue (RGB) | Specifies a color as an rgb code, in the functional form rgb(R,G,B), with values that can be integers from 0-255 or percentages. |
rgb(255,0,51)
rgb(255, 0, 51)
rgb(100%,0%,20%)
rgb(100%, 0%, 20%)
| | RGBA, red-green-blue-alpha (RGBa) | RGBa extends the RGB color model by including an alpha channel, which allows setting the opacity of a color. a stands for alpha: 0=transparent; 1=opaque. |
rgba(255,0,0,0.1)    / 10% 不透明 /
rgba(255,0,0,0.4) /
40% 不透明 /
rgba(255,0,0,0.7) /
70% 不透明 /
rgba(255,0,0, 1) /
不透明,即红色 /
| | HSL, hue-saturation-lightness | Hue is represented as an angle on the color wheel (a circle representing the rainbow).
Saturation and lightness are expressed as percentages.
100% is full saturation, while 0% is a shade of gray.
100% lightness is white, 0% lightness is black, and 50% lightness is "normal". |
hsl(120,100%,25%)    / 深绿色 /
hsl(120,100%,50%) /
绿色 /
hsl(120,100%,75%) /
浅绿色 /
| | HSLA, hue-saturation-lightness-alpha (HSLa) | HSLa extends the HSL color model by including an alpha channel, which allows specifying the opacity of a color. a stands for alpha: 0=transparent; 1=opaque. |
hsla(240,100%,50%,0.05)   / 5% 不透明 /
hsla(240,100%,50%, 0.4) /
40% 不透明 /
hsla(240,100%,50%, 0.7) /
70% 不透明 /
hsla(240,100%,50%, 1) /
完全不透明 */
|


More examples

Example

Color as a hexadecimal value:

body

{

color:

#92a8d1

;

}

Try it »

Example

Color value as RGB:

body

{

color:

rgb

(

255

,

0

,

51

)

;

}

Try it »

Example

Color value as RGBA:

body

{

color:

rgba

(

255

,

0

,

0

,

0.7

)

;

}

Try it »

Example

Color value as HSL:

body

{

color:

hsl

(

120

,

100

%

,

25

%

)

;

}

Try it »

Example

Color value as HSLA:

body

{

color:

hsla

(

240

,

100

%

,

50

%

,

0.7

)

;

}

Try it »


Related Articles

CSS Tutorial: CSS Text Formatting