W3cubDocs

/CSS

<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: These color values can be used to apply 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.

Named colors

Named colors 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 described on the <named-color> page.

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() (and the legacy rgba()): rgb(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.

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

Where the H is the hue, taking as a value an <angle> of the color circle given in degs, rads, grads, or turns. By definition, red is 0deg, yellow is 60deg, green is 120deg, cyan is 180deg, blue is 240deg, and magenta is 300deg. When written as a unitless <number>, it is interpreted as degrees. The S is the saturation as a <percentage> value where 100% is completely saturated, while 0% is completely unsaturated (gray). The L is the lightness as a <percentage> value where 100% is white, 0% is black, and 50% is "normal". The optional A is alpha transparency as a <percentage> or a <number> between 0 and 1, where the number 1 or 100% and means full opacity and 0 or 0% and means fully transparent.

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() notation.

Note: The legacy hsla() syntax is an alias for hsl(), accepting the same parameters and behaving in the same way.

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 an 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 its 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: AccentColor, AccentColorText, ActiveText, ButtonBorder, ButtonFace, ButtonText, Canvas, CanvasText, Field, FieldText, GrayText, Highlight, HighlightText, LinkText, Mark, MarkText, SelectedItem, SelectedItemText, 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 which describes both hsl(60 100% 50%) and hsl(240 100% 50%) as having the same lightness, LCH (and Lab) correctly ascribes different lightness 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. By default, animation occurs in an RGBA color space, with interpolation's speed being determined by the timing function in transitions and animations. To prevent unexpected colors from appearing, consider using color-mix() functional notation.

Interpolating the <hue> of two colors in functions that accept a hue angle is also possible and is described in more detail in the color-mix() functional notation documentation.

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.

Formal syntax

<color> = 
<absolute-color-base> |
currentcolor |
<system-color>

<absolute-color-base> =
<hex-color> |
<absolute-color-function> |
<named-color> |
transparent

<absolute-color-function> =
<rgb()> |
<rgba()> |
<hsl()> |
<hsla()> |
<hwb()> |
<lab()> |
<lch()> |
<oklab()> |
<oklch()> |
<color()>

<rgb()> =
rgb( [ <percentage> | none ]{3} [ / [ <alpha-value> | none ] ]? ) |
rgb( [ <number> | none ]{3} [ / [ <alpha-value> | none ] ]? )

<hsl()> =
hsl( [ <hue> | none ] [ <percentage> | none ] [ <percentage> | none ] [ / [ <alpha-value> | none ] ]? )

<hwb()> =
hwb( [ <hue> | none ] [ <percentage> | none ] [ <percentage> | none ] [ / [ <alpha-value> | none ] ]? )

<lab()> =
lab( [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )

<lch()> =
lch( [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <hue> | none ] [ / [ <alpha-value> | none ] ]? )

<oklab()> =
oklab( [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ / [ <alpha-value> | none ] ]? )

<oklch()> =
oklch( [ <percentage> | <number> | none ] [ <percentage> | <number> | none ] [ <hue> | none ] [ / [ <alpha-value> | none ] ]? )

<color()> =
color( <colorspace-params> [ / [ <alpha-value> | none ] ]? )

<alpha-value> =
<number> |
<percentage>

<hue> =
<number> |
<angle>

<colorspace-params> =
<predefined-rgb-params> |
<xyz-params>

<predefined-rgb-params> =
<predefined-rgb> [ <number> | <percentage> | none ]{3}

<xyz-params> =
<xyz-space> [ <number> | <percentage> | none ]{3}

<predefined-rgb> =
srgb |
srgb-linear |
display-p3 |
a98-rgb |
prophoto-rgb |
rec2020

<xyz-space> =
xyz |
xyz-d50 |
xyz-d65

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, .5)
rgb(255, 0, 153, 0.5)
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, 0.1)    /*  10% opaque green */
rgba(51, 170, 51, 0.4)    /*  40% opaque green */
rgba(51, 170, 51, 0.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(0.75turn, 60%, 70%)

/* These examples all specify the same color: a lavender that is 15% opaque. */
hsl(270, 60%, 50%, 0.15)
hsl(270, 60%, 50%, 0.15)
hsl(270, 60%, 50%, 15%)
hsl(270 60% 50% / 0.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(0.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

hsl(240 100% 50% / 0.05)    /*   5% opaque blue */
hsl(240 100% 50% / 0.4)     /*  40% opaque blue */
hsl(240 100% 50% / 0.7)     /*  70% opaque blue */
hsl(240 100% 50% / 1)       /* full opaque blue */

/* Comma syntax */
hsl(240, 100%, 50%, 0.05)     /*   5% opaque blue */

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

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
color_value 1 12 1 3 3.5 1 4.4 18 4 10.1 1 1.0
color 111 111 111 No 97 15
10.1–15Only supports display-p3 and srgb predefined color profiles.
111 111 No No 15
10.3–15Only supports display-p3 and srgb predefined color profiles.
No
color-contrast No No No No No 15 No No No No 15 No
color-mix 111 111 88 No 97 16.215 111 111 No No 16.215 No
currentcolor 1 12 1.5 9 9.5 4 37 18 4 14 3.2 1.0
hsl 1 12 1 9 9.5 3.1 ≤37 18 4 10.1 2 1.0
hsla 1 12 3 9 10 3.1 ≤37 18 4 10.1 2 1.0
hwb 101 101 96 No 87 15 101 101 96 70 15 19.0
lab 111 111 111 No 97 15 111 111 No No 15 No
lch 111 111 111 No 97 15 111 111 No No 15 No
named-color 1 12 1
3Internet Explorer 8 and later support gray color keywords spelled with an e (grey, darkgrey, darkslategrey, dimgrey, lightgrey, and lightslategrey). Internet Explorer 3 to Internet Explorer 7 only support the keywords spelled with a (gray, darkgray, darkslategray, dimgray, lightgray, and lightslategray).
3.5 1 ≤37 18 4 10.1 1 1.0
oklab 111 111 111 No 97 15.4 111 111 No No 15.4 No
oklch 111 111 111 No 97 15.4 111 111 No No 15.4 No
rgb 1 12 1 4 3.5 1 ≤37 18 4 10.1 1 1.0
rgb_hexadecimal_notation 1 12 1 3 3.5 1 ≤37 18 4 10.1 1 1.0
rgba 1 12 3 9 10 3.1 ≤37 18 4 10.1 2 1.0
system-color 1 12 1 3 3.5 1 ≤37 18 4 10.1 1 1.0
transparent 1 12 3 9 10 3.1 37 18 4 10.1 2 1.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/color_value