CSS Legal Color Values
CSS color values can be specified in several ways, including named colors, hexadecimal, RGB, RGBA, HSL, HSLA, and color functions.
The following are several common legal color value formats:
**Named colors:** Use the color name directly, for example: red, blue, green, etc.
color: red;
**Hexadecimal colors:** Use a hexadecimal value in the form #RRGGBB or #RGB.
color: #ff0000; /* 红色 */
color: #f00; /* 简写形式,红色 */
**RGB colors:** Use the rgb() function to specify the values of red, green, and blue, each between 0 and 255.
color: rgb(255, 0, 0); /* 红色 */
**RGBA colors:** Similar to rgb(), but with an added alpha channel to specify transparency, with values ranging from 0 (fully transparent) to 1 (fully opaque).
color: rgba(255, 0, 0, 0.5); /* 半透明红色 */
**HSL colors:** Use the hsl() function to specify hue (0-360°), saturation (0%-100%), and lightness (0%-100%).
color: hsl(0, 100%, 50%); /* 红色 */
**HSLA colors:** Similar to hsl(), but with an added alpha channel for transparency control.
color: hsla(0, 100%, 50%, 0.5); /* 半透明红色 */
**CSS color function color():** Defines a color using a color space, such as color(display-p3 1 0 0). This approach allows the use of a wider range of color spaces.
color: color(display-p3 1 0 0);
Hexadecimal colors
Hexadecimal color values are supported by all major browsers.
A hexadecimal color is specified as: #RRGGBB, where RR (red), GG (green), and BB (blue). All values must be between 0 and FF.
For example, the value #0000FF appears blue because the blue component is set to the highest value (FF) and the others are set to 0.
Example
p {
background-color:#ff0000;
}
Try it »
RGB colors
RGB color values are supported in all major browsers.
RGB color values are specified as: rgb(red, green, blue). Each parameter (red, green, and blue) defines the brightness of the color, as an integer between 0 and 255, or a percentage value (from 0% to 100%).
For example, the value rgb(0,0,255) appears blue because the blue parameter is set to the highest value (255) and the others are set to 0.
In addition, the following values define the same color: rgb(0,0,255), rgb(0%,0%,100%).
Example
p {
background-color:rgb(255,0,0);
}
Try it »
RGBA colors
RGBA color values are supported by IE9, Firefox 3+, Chrome, Safari, and Opera 10+.
RGBA color values are an extension of RGB color values with an alpha channel - specifying the opacity of an object.
RGBA color values are specified as: rgba(red, green, blue, alpha). The alpha parameter is a value between 0.0 (fully transparent) and 1.0 (fully opaque).
Example
p {
background-color:rgba(255,0,0,0.5);
}
Try it »
HSL colors
IE9, Firefox, Chrome, Safari, and Opera 10+ support HSL color values.
HSL stands for hue, saturation, and lightness - represented using cylindrical color coordinates.
HSL color values are specified as: hsl(hue, saturation, lightness).
Hue is a degree on the color wheel (from 0 to 360) - 0 (or 360) is red, 120 is green, 240 is blue. Saturation is a percentage value; 0% means a shade of gray and 100% is full color. Lightness is also a percentage; 0% is black and 100% is white.
Example
p {
background-color:hsl(120,65%,75%);
}
Try it »
HSLA colors
HSLA color values are supported by IE9, Firefox 3+, Chrome, Safari, and Opera 10+.
HSLA color values are an extension of HSL color values with an alpha channel - specifying the opacity of an object.
HSLA color values are specified as: hsla(hue, saturation, lightness, alpha), where alpha defines the opacity. The alpha parameter is a value between 0.0 (fully transparent) and 1.0 (fully opaque).
Example
p {
background-color:hsla(120,65%,75%,0.3);
}
Try it »
Predefined/Cross-browser Color Names
147 color names are predefined in the HTML and CSS color specifications. You can view our predefined color names table.