1. Introduction
This section is not normative.
Tests
This section is not normative, it does not need tests.
This module describes CSS properties which allow authors to specify the foreground color and opacity of the text content of an element. This module also describes in detail the CSS <color> value type.
It not only defines the color-related properties and values that already exist in CSS1, CSS2, and CSS Color 3, but also defines new properties and values.
In particular, it allows specifying colors in other color spaces than sRGB; previously, the more saturated colors outside the sRGB gamut could not be used in CSS even if the display device supported them.
A draft implementation report is available.
1.1. Value Definitions
This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.
2. Color Terminology
Tests
This section provides definitions used later, it does not need tests.
A color is a definition (numeric or textual) of the human visual perception of a light or a physical object illuminated with light. The objective study of human color perception is termed colorimetry.
The color of a physical object depends on how much light it reflects at each visible wavelength, plus the actual color of the light illuminating it (again, the amount of light at each wavelength). It is measured by a spectrophotometer .
The color of something that emits light (including colors on a computer screen) depends on how much light it emits at each visible wavelength. It is measured by a spectroradiometer.
If two objects have different spectra, but still produce the same physical sensation, we say they have the same color. We can calculate whether two colors are the same by converting the spectra to CIE XYZ (three numbers).
A color space is an organization of colors with respect to an underlying colorimetric model, such that there is a clear, objectively-measurable meaning for any color in that color space. This also means that the same color can be expressed in multiple color spaces, or transformed from one color space to another, while still looking the same.
A leaf is measured with a spectrophotometer and found to have the color lch(51.2345% 21.2 130) which is lab(51.2345% -13.6271 16.2401).
This same color could be expressed in various color spaces:
color ( sRGB0.41587 0.503670 0.36664 ); color ( display-p30.43313 0.50108 0.37950 ); color ( a98-rgb0.44091 0.49971 0.37408 ); color ( prophoto-rgb0.36589 0.41717 0.31333 ); color ( rec20200.6295 0.9657 0.3633 );
An additive color space means that the coordinate system is linear in light intensity. The CIE XYZ color space is an additive color space. The Y component of XYZ is the luminance, the light intensity per unit area, or 'how bright it is'. Luminance is measured in candelas per square meter. cd/m², also called nits.
In an additive color space, calculations can be done to accurately predict color mixing. Most RGB spaces are not additive, because the components are gamma encoded. Undoing this gamma encoding produces linear-light values.
If we have two differently colored spotlights shining on a stage, and one has the measured value color(xyz 0.15 0.24 0.17) while the other is color(xyz 0.11 0.06 0.06) then we can accurately predict that if the colored beams are made to overlap, the color of the mixture will be the sum of the XYZ component values, or color(xyz 0.26 0.30 0.23).
A chromaticity is a color measurement where the lightness component has been factored out. From the identical lights example above, the u',v' chromaticity with one light is (0.2537, 0.5268) and the chromaticity is the same with both lights (they are the same color, it is just brighter).
Chromaticities are additive, so they accurately predict the chromaticity (but not the resulting lightness) of a mixture. Being two-dimensional, chromaticity is easily represented on a chromaticity diagram to predict the chromaticity of a color mixture. Any two colors can be mixed, and the resulting colors will lie on the line joining them on the diagram. Three colors form a plane, and the resulting colors will lie in the triangle they form on the diagram.
Thus, once linearized, RGB color spaces are additive, and their gamut is defined by the chromaticities of the red, green and blue primaries, plus the chromaticity of the white point (the color formed by all three primaries at full intensity).
Most color spaces use one of a few daylight-simulating white points, which are named by the correlated color temperature (CCT) [Understanding_CCT] of the corresponding black-body radiator. For example, D65 is a daylight whitepoint corresponding to a correlated color temperature of 6500 Kelvin (actually 6504, because the value of Plank’s constant has changed since the color was originally defined).
To avoid cumulative round-trip errors, it is important that the identical chromaticity values are used consistently, at all places in a calculation. Thus, for maximum compatibility, for this specification, the following two standard daylight-simulating white points are defined:
Name | x | y | CCT |
---|---|---|---|
D50 | 0.345700 | 0.358500 | 5003K |
D65 | 0.312700 | 0.329000 | 6504K |
When the measured physical characteristics (such as the chromaticities of the primary colors it uses, or the colors produced in response to a given set of inputs) of a color space or a color-producing device are known, it is said to be characterized.
If in addition adjustments have been made so that a device meets calibration targets such as white point, neutrality of greys, predictability and consistency of tone response, then it is said to be calibrated.
Real physical devices cannot yet produce every possible color that the human eye can see. The range of colors that a given device can produce is termed the gamut (not to be confused with gamma). Devices with a limited gamut cannot produce very saturated colors, like those found in a rainbow.
The gamuts of different color spaces may be compared by looking at the volume (in cubic Lab units) of colors that can be expressed. The following table examines the predefined color spaces available in CSS.
color space | Volume (million Lab units) |
---|---|
sRGB | 0.820 |
display-p3 | 1.233 |
a98-rgb | 1.310 |
prophoto-rgb | 2.896 |
rec2020 | 2.042 |
A color in CSS is either an invalid color, as described below for each syntactic form, or a valid color.
Any color which is not an invalid color is a valid color.
A color may be a valid color but still be outside the range of colors that can be produced by an output device (a screen, projector, or printer)
It is said to be out of gamut.
Each valid color is either in-gamut for a particular output device (screen, or printer) or it is out of gamut.
color ( prophoto-rgb0.88 0.45 0.10 )
because, expressed in display-p3, one or more coordinates are either greater that 1.0 or less than 0.0:
color ( display-p31.0844 0.43 0.1 )
This color is valid, and could, for example, be used as a gradient stop, but would need to be CSS gamut mapped for display, producing a similar-looking but lower chroma (less saturated) color.
3. Applying Color in CSS
3.1. Accessibility and Conveying Information By Color
Tests
This section provides authoring guidance, it does not need tests.
Although colors can add significant information to documents and make them more readable, color by itself should not be the sole means to convey important information. Authors should consider the W3C Web Content Accessibility Guidelines [WCAG21] when using color in their documents.
3.2. Foreground Color: the color property
Name: | color |
---|---|
Value: | <color> |
Initial: | CanvasText |
Applies to: | all elements and text |
Inherited: | yes |
Percentages: | N/A |
Computed value: | computed color, see resolving color values |
Canonical order: | per grammar |
Animation type: | by computed value type |
Tests
- color-001.html (live test) (source)
- color-002.html (live test) (source)
- color-003.html (live test) (source)
- inheritance.html (live test) (source)
- color-interpolation.html (live test) (source)
- color-initial-canvastext.html (live test) (source)
- color-valid.html (live test) (source)
- color-invalid.html (live test) (source)
This property specifies the primary foreground color of the element. This is used as the fill color of its text content, and in addition specifies the used value that currentcolor resolves to, which allows indirect references to this foreground color and affects the initial values of various other color properties such as border-color and text-emphasis-color.
- <color>
- Sets the primary foreground color to the specified <color>.
em{ color : lime; } /* color keyword */ em{ color : rgb ( 0 255 0 ); } /* RGB range 0-255 */ em{ color : rgb ( 0 % 100 % 0 % ); } /* RGB range 0%-100% */ em{ color : color ( sRGB0 1 0 ); } /* sRGB range 0.0-1.0 */
When applied to text, this property, including its alpha component,
has no effect on “color glyphs” (such as the emoji in some fonts),
which are colored by a built-in palette.
However, some colored fonts are able to refer to a contextual “foreground color”,
such as by palette entry 0xFFFF in the COLR
table of OpenType,
or by the context-fill value in SVG-in-OpenType.
In such cases, the foreground color is set by this property,
identical to how it sets the currentcolor value.
3.3. Transparency: the opacity property
Opacity can be thought of as a postprocessing operation. Conceptually, after the element (including its descendants) is rendered into an RGBA offscreen image, the opacity setting specifies how to blend the offscreen rendering into the current composite rendering. See simple alpha compositing for details.
Name: | opacity |
---|---|
Value: | <opacity-value> |
Initial: | 1 |
Applies to: | all elements |
Inherited: | no |
Percentages: | map to the range [0,1] |
Computed value: | specified number, clamped to the range [0,1] |
Canonical order: | per grammar |
Animation type: | by computed value type |
Tests
- clip-opacity-out-of-flow.html (live test) (source)
- t32-opacity-basic-0.0-a.xht (live test) (source)
- t32-opacity-basic-0.6-a.xht (live test) (source)
- t32-opacity-basic-1.0-a.xht (live test) (source)
- t32-opacity-clamping-0.0-b.xht (live test) (source)
- t32-opacity-clamping-1.0-b.xht (live test) (source)
- t32-opacity-offscreen-b.xht (live test) (source)
- t32-opacity-offscreen-multiple-boxes-1-c.xht (live test) (source)
- t32-opacity-offscreen-multiple-boxes-2-c.xht (live test) (source)
- t32-opacity-offscreen-with-alpha-c.xht (live test) (source)
- t32-opacity-zorder-c.xht (live test) (source)
- opacity-computed.html (live test) (source)
- opacity-valid.html (live test) (source)
- opacity-invalid.html (live test) (source)
- composited-filters-under-opacity.html (live test) (source)
- filters-under-will-change-opacity.html (live test) (source)
- color-composition.html (live test) (source)
- opacity-interpolation.html (live test) (source)
- canvas-change-opacity.html (live test) (source)
- opacity-animation-ending-correctly-001.html (live test) (source)
- opacity-animation-ending-correctly-002.html (live test) (source)
- <opacity-value>
-
The opacity to be applied to the element.
The resulting opacity is applied to the entire element,
rather than a particular color.
Opacity values outside the range [0,1] are not invalid, and are preserved in specified values, but are clamped to the range [0, 1] in computed values.
Opacity in CSS is represented using the <opacity-value> syntax, for example in the opacity property.
<opacity-value> = <number> | <percentage>
Represented as a <number>, the useful range of the value is 0 (representing full transparency) to 1 (representing full opacity). It can also be written as a <percentage>, which computes to the equivalent <number> (0% to 0, 100% to 1).
The opacity property applies the specified opacity to the element as a whole, including its contents, rather than applying it to each descendant individually. This means that, for example, an opaque child occluding part of the element’s background will continue to do so even when opacity is less than 1, but the element and child as a whole will show the underlying page through themselves.
It also means that the glyphs corresponding to all characters in the element are treated as a whole; any overlapping portions do not increase the opacity.
If separate opacity for each glyph is desired, it can be achieved by using a color value which includes alpha, rather than setting the opacity property.
If a box has opacity less than 1, it forms a stacking context for its children. (This prevents its contents from interleaving in the z-axis with content outside it.)
Furthermore, if the z-index property applies to the box, the auto value is treated as 0 for the element; it is otherwise painted on the same layer within its parent stacking context as positioned elements with stack level 0 (as if it were a positioned element with z-index:0).
See section 9.9 and Appendix E of [CSS2] for more information on stacking contexts.
These rules about z-order do not apply to SVG elements, since SVG has its own rendering model ([SVG11], Chapter 3).
The value of the opacity property does not affect hit testing.
3.4. Color Space of Tagged Images
An tagged image is an image that is explicitly assigned a color profile, as defined by the image format. This is usually done by including an International Color Consortium (ICC) profile [ICC].
For example JPEG [JPEG], PNG [PNG] and TIFF [TIFF] all specify a means to embed an ICC profile.
Image formats may also use other, equivalent methods, often for brevity.
For example, PNG specifies a means (the sRGB chunk) to explicitly tag an image as being in the sRGB color space, without including the sRGB ICC profile.
Similarly, PNG specifies a compact means (the cICP chunk) to explicitly tag an image as being one of various SDR or HDR color spaces, such as Display P3 or BT.2100 HLG, without including an ICC profile.
Tagged RGB images, and tagged images using a transformation of RGB such as YCbCr, if the color profile or other identifying information is valid, must be treated as being in the specified color space.
Tests
For example, when a browser running on a system with a Display P3 monitor displays an JPEG image tagged as being in the ITU Rec BT.2020 [Rec.2020] color space, it must convert the colors from ITU Rec BT.2020 to Display P3 so that they display correctly. It must not treat the ITU Rec BT.2020 values as if they were Display P3 values, which would produce incorrect colors.
If the color profile or other identifying information is invalid, the image is treated as described for untagged images.
3.5. Color Spaces of Untagged Colors
For compatibility, colors specified in HTML, and untagged images must be treated as being in the sRGB color space ([SRGB]) unless otherwise specified.
An untagged image is an image that is not explicitly assigned a color profile, as defined by the image format.
This rule does not apply to untagged videos, since untagged video should be presumed to be in an ITU-defined color space.
-
At below 720p, it is Recommendation ITU-R BT.601 [ITU-R-BT.601]
-
At 720p, it is SMPTE ST 296 (same colorimetry as 709) [SMPTE296]
-
At 1080p, it is Recommendation ITU-R BT.709 [ITU-R-BT.709]
-
At 4k (UHDTV) and above, it is ITU-R BT.2020 [Rec.2020] for SDR video
4. Representing Colors: the <color> type
Tests
This section describes a type, it is primarily tested where that type is used.
Colors in CSS are represented as a list of color components, also sometimes called “channels”, representing axises in the color space. Each component has a minimum and maximum value, and can take any value between those two. Additionally, every color is accompanied by an alpha component, indicating how transparent it is, and thus how much of the backdrop one can see through the color.
CSS has several syntaxes for specifying color values:
-
the sRGB hex color notation which represents the RGB and alpha components in hexadecimal notation
-
the various color functions which can represent colors using a variety of color spaces and coordinate systems
-
the constant named color keywords
-
the variable <system-color> keywords and currentColor keyword.
The color functions use CSS functional notation to represent colors in a variety of color spaces by specifying their component coordinates. Some of these use a cylindrical polar color model, specifying color by a <hue> angle, a central axis representing lightness (black-to-white), and a radius representing saturation or chroma (how far the color is from a neutral grey). The others use a rectangular orthogonal color model, specifying color using three orthogonal component axes.
The color functions available in Level 4 are
-
rgb() and its rgba() alias, which (like the hex color notation) specify sRGB colors directly by their red/green/blue/alpha components.
-
hsl() and its hsla() alias, which specify sRGB colors by hue, saturation, and lightness using the HSL cylindrical coordinate model.
-
hwb(), which specifies an sRGB color by hue, whiteness, and blackness using the HWB cylindrical coordinate model.
-
lab(), which specifies a CIELAB color by CIE Lightness and its a- and b-axis hue coordinates (red/green-ness, and yellow/blue-ness) using the CIE LAB rectangular coordinate model.
-
lch() , which specifies a CIELAB color by CIE Lightness, Chroma, and hue using the CIE LCH cylindrical coordinate model
-
oklab(), which specifies an Oklab color by Oklab Lightness and its a- and b-axis hue coordinates (red/green-ness, and yellow/blue-ness) using the Oklab rectangular coordinate model.
-
oklch() , which specifies an Oklab color by Oklab Lightness, Chroma, and hue using the OkLCh cylindrical coordinate model.
-
color(), which allows specifying colors in a variety of color spaces including sRGB, Linear-Light sRGB, Display P3, Linear-Light Display P3, A98 RGB, ProPhoto RGB, ITU-R BT.2020-2, and CIE XYZ.
For easy reference in other specifications, opaque black is defined as the color rgb(0 0 0 / 100%); transparent black is the same color, but fully transparent—i.e. rgb(0 0 0 / 0%).
Tests
4.1. The <color> syntax
Tests
This section provides definitions used later, it does not need tests.
Colors in CSS are represented by the <color> type:
<color> = <color-base> | currentColor | <system-color> <color-base> = <hex-color> | <color-function> | <named-color> | transparent <color-function> = <rgb () > | <rgba () > | <hsl () > | <hsla () > | <hwb () > | <lab () > | <lch () > | <oklab () > | <oklch () > | <color () >
An absolute color is a <color> whose computed value has an absolute, colorimetric interpretation. This means that the value is not:
-
currentColor (which depends on the value of the color property)
-
a <system-color> (which depends on the color mode)
The colors that resolve to sRGB are:
The functions that support legacy color syntax are: