<color>

The <color> CSS data type represents a color. A <color> may also include an alpha-channel transparency value, indicating how the color should composite with its background.

A <color> can be defined in any of the following ways:

Note: This article describes the <color> data type in detail. To learn more about using color in HTML, see Applying color to HTML elements using CSS.

Syntax

The <color> data type is specified using one of the options listed below.

Note: Although <color> values are precisely defined, their actual appearance may vary (sometimes significantly) from device to device. This is because most devices are not calibrated, and some browsers do not support output devices' color profiles.

Color keywords

Color keywords are case-insensitive identifiers that represent a specific color, such as red, blue, black, or lightseagreen. Although the names more or less describe their respective colors, they are essentially artificial, without a strict rationale behind the names used.

The complete list of such keywords is available here.

transparent keyword

The transparent keyword represents a fully transparent color. This makes the background behind the colored item completely visible. Technically, transparent is a shortcut for rgba(0,0,0,0).

Note: To prevent unexpected behavior, such as in a <gradient>, the current CSS spec states that transparent should be calculated in the alpha-premultiplied color space. However, be aware that older browsers may treat it as black with an alpha value of 0.

Note: transparent wasn't a true color in CSS Level 2 (Revision 1). It was a special keyword that could be used instead of a regular <color> value on two CSS properties: background and border. It was essentially added to allow developers to override an inherited solid color. With the advent of alpha channels in CSS Colors Level 3, transparent was redefined as a true color. It can now be used wherever a <color> value can be used.

currentcolor keyword

The currentcolor keyword represents the value of an element's color property. This lets you use the color value on properties that do not receive it by default.

If currentcolor is used as the value of the color property, it instead takes its value from the inherited value of the color property.

<div style="color: blue; border: 1px dashed currentcolor;">
  The color of this text is blue.
  <div style="background: currentcolor; height:9px;"></div>
  This block is surrounded by a blue border.
</div>

RGB color model

The RGB color model defines a given color in the sRGB color space according to its red, green, and blue components. An optional alpha component represents the color's transparency.

RGB colors can be expressed through both hexadecimal (prefixed with #) and functional (rgb(), rgba()) notations.

Hexadecimal notation: #RGB[A] or#RRGGBB[AA]

R (red), G (green), B (blue), and A (alpha) are hexadecimal characters (0–9, A–F). A is optional. For example, #ff0000 is equivalent to #ff0000ff. The three-digit notation (#RGB) is a shorter version of the six-digit form (#RRGGBB). For example, #f09 is the same color as #ff0099. Likewise, the four-digit RGB notation (#RGBA) is a shorter version of the eight-digit form (#RRGGBBAA). For example, #0f38 is the same color as #00ff3388.

rgb() or rgba(): rgb[a](R, G, B[, A]) or rgb[a](R G B[ / A])

R (red), G (green), and B (blue) can be either <number>s or <percentage>s, where the number 255 corresponds to 100%. A (alpha) can be a <number> between 0 and 1, or a <percentage>, where the number 1 corresponds to 100% (full opacity).

HSL color model

The HSL color model defines a given color in the sRGB color space according to its hue, saturation, and lightness components. An optional alpha component represents the color's transparency.

Many designers find HSL more intuitive than RGB, since it allows hue, saturation, and lightness to each be adjusted independently. HSL can also make it easier to create a set of matching colors (such as when you want multiple shades of a single hue). However, using HSL to create color variations can produce surprising results, as it is not perceptually uniform. For example, both hsl(240 100% 50%) and hsl(60 100% 50%) have the same lightness, even though the former is much darker than the latter.

HSL colors are expressed through the functional hsl() and hsla() notations.

hsl() or hsla(): hsl[a](H, S, L[, A]) or hsl[a](H S L[ / A])

H (hue) is an <angle> of the color circle given in degs, rads, grads, or turns in the CSS Color specification. When written as a unitless <number>, it is interpreted as degrees, as specified in the CSS Color Level 3 specification. By definition, red=0deg=360deg, with the other colors spread around the circle, so green=120deg, blue=240deg, etc. As an <angle>, it implicitly wraps around such that -120deg=240deg, 480deg=120deg, -1turn=1turn, etc.

S (saturation) and L (lightness) are percentages. 100% saturation is completely saturated, while 0% is completely unsaturated (gray). 100% lightness is white, 0% lightness is black, and 50% lightness is "normal".

A (alpha) can be a <number> between 0 and 1, or a <percentage>, where the number 1 corresponds to 100% (full opacity).

HWB color model

Similar to HSL color model, the HWB color model defines a given color in the sRGB color space according to its hue, whiteness and blackness components.

As with HSL, HWB can be more intuitive to use than RGB. A hue is specified in the same way, followed by the amount of whiteness and blackness, respectively, in percentage values. This function also accepts a alpha value.

Note: There is no separate hwba() function as there is with HSL, the alpha value is an optional parameter, if it is not specified an alpha value of 1 (or 100%) is used. To specify this value a forward slash (/) must follow the blackness value before the alpha value is specified.

HWB colors are expressed through the functional hwb() notation.

Note: The HWB function does not use commas to separate it's values as with previous color functions.

hwb(): hwb(H W B[ / A])

Same as HSL: H (hue) is an <angle> of the color circle given in degs, rads, grads, or turns in the CSS Color specification. When written as a unitless <number>, it is interpreted as degrees, as specified in the CSS Color Level 3 specification. By definition, red=0deg=360deg, with the other colors spread around the circle, so green=120deg, blue=240deg, etc. As an <angle>, it implicitly wraps around such that -120deg=240deg, 480deg=120deg, -1turn=1turn, etc.

W (whiteness) and B (blackness) are percentages. These two colors mix, so you would need 0% whiteness and 100% blackness to produce the color black. And vice versa 100% whiteness and 0% blackness for the color white. 50% of both values renders a mid grey and any other variations a shade of the hue specified.

A (alpha), optional, can be a <number> between 0 and 1, or a <percentage>, where the number 1 corresponds to 100% (full opacity). When specifying an alpha value it must be preceded with a forward slash (/).

System Colors

In forced colors mode (detectable with the forced-colors media query), most colors are restricted into a user- and browser-defined palette. These system colors are exposed by the following keywords, which can be used to ensure that the rest of the page integrates well with the restricted palette. These values may also be used in other contexts, but are not widely supported by browsers.

The keywords in the following list are defined by the CSS Color Module Level 4 specification: ActiveText, ButtonBorder, ButtonFace, ButtonText, Canvas, CanvasText, Field, FieldText, GrayText, Highlight, HighlightText, LinkText, Mark, MarkText, VisitedText.

Note: Note that these keywords are case insensitive, but are listed here with mixed case for readability.

Lab colors

CSS Color 4 introduced Lab colors. Lab colors are specified via the lab() functional notation. They are not limited to a specific color space, and can represent the entire spectrum of human vision.

LCH color model

CSS Color 4 introduced LCH colors. LCH colors are specified via the lch() functional notation. They are not limited to a specific color space, and can represent the entire spectrum of human vision.

In fact, LCH is the polar form of Lab. It is more human friendly than Lab, as its chroma and hue components specify qualities of the desired color, as opposed to mixing. It is similar to HSL in that way, although it is far more perceptually uniform. Unlike HSL that describes both hsl(60 100% 50%) hsl(240 100% 50%) as having the same lightness, LCH (and Lab) correctly ascribes different lightnesses to them: the former (yellow) has an L of 97.6 and the latter (blue) an L of 29.6. Therefore, LCH can be used to create palettes across entirely different colors, with predictable results. Please note that LCH hue is not the same as HSL hue and LCH chroma is not the same as HSL saturation, although they do share some conceptual similarities.

color() colors

CSS Color 4 introduced this notation. Colors specified via the color() function can specify a color in any of the predefined color spaces, as well as custom color spaces, defined via the @color-profile rule.

Interpolation

In animations and gradients, <color> values are interpolated on each of their red, green, and blue components. Each component is interpolated as a real, floating-point number. Note that interpolation of colors happens in the alpha-premultiplied sRGBA color space to prevent unexpected gray colors from appearing. In animations, the interpolation's speed is determined by the timing function.

Accessibility considerations

Some people have difficulty distinguishing colors. The WCAG 2.0 recommendation strongly advises against using color as the only means of conveying a specific message, action, or result. See Color and color contrast for more information.

Examples

Color value tester

In this example we provide a <div> and a text input. Entering a valid color into the input causes the <div> to adopt that color, allowing you to test our color values.

HTML

<div></div>
<hr>
<label for="color">Enter a valid color value:</label>
<input type="text" id="color">

CSS

div {
  width: 100%;
  height: 200px;
}

Result

RGB syntax variations

This example shows the many ways in which a single color can be created with the various RGB color syntaxes.

/* These syntax variations all specify the same color: a fully opaque hot pink. */

/* Hexadecimal syntax */
#f09
#F09
#ff0099
#FF0099

/* Functional syntax */
rgb(255,0,153)
rgb(255, 0, 153)
rgb(255, 0, 153.0)
rgb(100%,0%,60%)
rgb(100%, 0%, 60%)
rgb(100%, 0, 60%) /* ERROR! Don't mix numbers and percentages. */
rgb(255 0 153)

/* Hexadecimal syntax with alpha value */
#f09f
#F09F
#ff0099ff
#FF0099FF

/* Functional syntax with alpha value */
rgb(255, 0, 153, 1)
rgb(255, 0, 153, 100%)

/* Whitespace syntax */
rgb(255 0 153 / 1)
rgb(255 0 153 / 100%)

/* Functional syntax with floats value */
rgb(255, 0, 153.6, 1)
rgb(2.55e2, 0e0, 1.53e2, 1e2%)

RGB transparency variations

/* Hexadecimal syntax */
#3a30                    /*   0% opaque green */
#3A3F                    /* full opaque green */
#33aa3300                /*   0% opaque green */
#33AA3380                /*  50% opaque green */

/* Functional syntax */
rgba(51, 170, 51, .1)    /*  10% opaque green */
rgba(51, 170, 51, .4)    /*  40% opaque green */
rgba(51, 170, 51, .7)    /*  70% opaque green */
rgba(51, 170, 51,  1)    /* full opaque green */

/* Whitespace syntax */
rgba(51 170 51 / 0.4)    /*  40% opaque green */
rgba(51 170 51 / 40%)    /*  40% opaque green */

/* Functional syntax with floats value */
rgba(51, 170, 51.6, 1)
rgba(5.1e1, 1.7e2, 5.1e1, 1e2%)

HSL syntax variations

/* These examples all specify the same color: a lavender. */
hsl(270,60%,70%)
hsl(270, 60%, 70%)
hsl(270 60% 70%)
hsl(270deg, 60%, 70%)
hsl(4.71239rad, 60%, 70%)
hsl(.75turn, 60%, 70%)

/* These examples all specify the same color: a lavender that is 15% opaque. */
hsl(270, 60%, 50%, .15)
hsl(270, 60%, 50%, 15%)
hsl(270 60% 50% / .15)
hsl(270 60% 50% / 15%)

HWB syntax variations

/* These examples all specify varying shades of a lime green. */
hwb(90 10% 10%)
hwb(90 50% 10%)
hwb(90deg 10% 10%)
hwb(1.5708rad 60% 0%)
hwb(.25turn 0% 40%)

/* Same lime green but with an alpha value */
hwb(90 10% 10% / 0.5)
hwb(90 10% 10% / 50%)

Fully saturated colors

Notation Description Live
hsl(0, 100%, 50%) red
hsl(30, 100%, 50%) orange
hsl(60, 100%, 50%) yellow
hsl(90, 100%, 50%) lime green
hsl(120, 100%, 50%) green
hsl(150, 100%, 50%) blue-green
hsl(180, 100%, 50%) cyan
hsl(210, 100%, 50%) sky blue
hsl(240, 100%, 50%) blue
hsl(270, 100%, 50%) purple
hsl(300, 100%, 50%) magenta
hsl(330, 100%, 50%) pink
hsl(360, 100%, 50%) red

Lighter and darker greens

Notation Description Live
hsl(120, 100%, 0%) black
hsl(120, 100%, 20%)
hsl(120, 100%, 40%)
hsl(120, 100%, 60%)
hsl(120, 100%, 80%)
hsl(120, 100%, 100%) white

Saturated and desaturated greens

Notation Description Live
hsl(120, 100%, 50%) green
hsl(120, 80%, 50%)
hsl(120, 60%, 50%)
hsl(120, 40%, 50%)
hsl(120, 20%, 50%)
hsl(120, 0%, 50%) gray

HSL transparency variations

hsla(240, 100%, 50%, .05)     /*   5% opaque blue */
hsla(240, 100%, 50%, .4)      /*  40% opaque blue */
hsla(240, 100%, 50%, .7)      /*  70% opaque blue */
hsla(240, 100%, 50%, 1)       /* full opaque blue */

/* Whitespace syntax */
hsla(240 100% 50% / .05)      /*   5% opaque blue */

/* Percentage value for alpha */
hsla(240 100% 50% / 5%)       /*   5% opaque blue */

Specifications

Specification
CSS Color Module Level 4
# color-syntax

Browser compatibility

BCD tables only load in the browser

See also