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)