Below are descriptions of the allowed values for both absolute and relative colors.
Absolute value syntax
lab(L a b[ / A])
The parameters are as follows:
L
-
A <number>
between 0
and 100
, a <percentage>
between 0%
and 100%
, or the keyword none
(equivalent to 0%
in this case). This value specifies the color's lightness. Here the number 0
corresponds to 0%
(black) and the number 100
corresponds to 100%
(white).
a
-
A <number>
between -125
and 125
, a <percentage>
between -100%
and 100%
, or the keyword none
(equivalent to 0%
in this case). This value specifies the color's distance along the a
axis, which defines how green (moving towards -125
) or red (moving towards +125
) the color is. Note that these values are signed (allowing both positive and negative values) and theoretically unbounded, meaning that you can set values outside the ±125
(±100%
) limits. In practice, values cannot exceed ±160
.
b
-
A <number>
between -125
and 125
, a <percentage>
between -100%
and 100%
, or the keyword none
(equivalent to 0%
in this case). This value specifies the color's distance along the b
axis, which defines how blue (moving towards -125
) or yellow ( moving towards +125
) the color is. Note that these values are signed (allowing both positive and negative values) and theoretically unbounded, meaning that you can set values outside the ±125
(±100%
) limits. In practice, values cannot exceed ±160
.
-
A
Optional
-
An <alpha-value>
representing the alpha channel value of the color, where the number 0
corresponds to 0%
(fully transparent) and 1
corresponds to 100%
(fully opaque). Additionally, the keyword none
can be used to explicitly specify no alpha channel. If the A
channel value is not explicitly specified, it defaults to 100%. If included, the value is preceded by a slash (/
).
Relative value syntax
lab(from <color> L a b[ / A])
The parameters are as follows:
from <color>
-
The keyword from
is always included when defining a relative color, followed by a <color>
value representing the origin color. This is the original color that the relative color is based on. The origin color can be any valid <color>
syntax, including another relative color.
L
-
A <number>
between 0
and 100
, a <percentage>
between 0%
and 100%
, or the keyword none
(equivalent to 0%
in this case) This value represents the lightness of the output color. Here the number 0
corresponds to 0%
(black) and the number 100
corresponds to 100%
(white).
a
-
A <number>
between -125
and 125
, a <percentage>
between -100%
and 100%
, or the keyword none
(equivalent to 0%
in this case). This value represents the output color's distance along the a
axis, which defines how green (moving towards -125
) or red (moving towards +125
) the color is. Note that these values are signed (allowing both positive and negative values) and theoretically unbounded, meaning that you can set values outside the ±125
(±100%
) limits. In practice, values cannot exceed ±160
.
b
-
A <number>
between -125
and 125
, a <percentage>
between -100%
and 100%
, or the keyword none
(equivalent to 0%
in this case). This value represents the output color's distance along the b
axis, which defines how blue (moving towards -125
) or yellow (moving towards +125
) the color is. Note that these values are signed (allowing both positive and negative values) and theoretically unbounded, meaning that you can set values outside the ±125
(±100%
) limits. In practice, values cannot exceed ±160
.
-
A
Optional
-
An <alpha-value>
representing the alpha channel value of the output color, where the number 0
corresponds to 0%
(fully transparent) and 1
corresponds to 100%
(fully opaque). Additionally, the keyword none
can be used to explicitly specify no alpha channel. If the A
channel value is not explicitly specified, it defaults to the alpha channel value of the origin color. If included, the value is preceded by a slash (/
).
Note: Usually when percentage values have a numeric equivalent in CSS, 100%
is equal to the number 1
. This is not always the case for LAB's lightness and a
and b
axes, as mentioned above. With L
, the range is from 0 to 100, with 100%
equal to 100
. The a
and b
values support both negative and positive values, with 100%
being equal to 125
and -100%
being equal to -125
.
Defining relative color output channel components
When using relative color syntax inside a lab()
function, the browser converts the origin color into an equivalent Lab color (if it is not already specified as such). The color is defined as three distinct color channel values — l
(lightness), a
(green/red axis), and b
(blue/yellow axis) — plus an alpha channel value (alpha
). These channel values are made available inside the function to be used when defining the output color channel values:
- The
l
channel value is resolved to a <number>
between 0
and 100
, inclusive. - The
a
and b
channels are each resolved to a <number>
between -125
and 125
, inclusive. - The
alpha
channel is resolved to a <number>
between 0
and 1
, inclusive.
When defining a relative color, the different channels of the output color can be expressed in several different ways. Below, we'll study some examples to illustrate these.
In the first two examples below, we are using relative color syntax. However, the first one outputs the same color as the origin color and the second one outputs a color not based on the origin color at all. They don't really create relative colors! You'd be unlikely to ever use these in a real codebase, and would probably just use an absolute color value instead. We included these examples as a starting point for learning about relative lab()
syntax.
Let's start with an origin color of hsl(0 100% 50%)
(equivalent to red
). The following function outputs the same color as the origin color — it uses the origin color's l
, a
, and b
channel values (54.29
, 80.8198
, and 69.8997
) as the output channel values:
lab(from hsl(0 100% 50%) l a b)
This function's output color is lab(54.29 80.8198 69.8997)
.
The next function uses absolute values for the output color's channel values, outputting a completely different color not based on the origin color:
lab(from hsl(0 100% 50%) 29.692% 44.89% -29.034%)
In the above case, the output color is lab(29.692 56.1125 -36.2925)
.
The following function creates a relative color based on the origin color:
lab(from hsl(0 100% 50%) l -100 b)
This example:
- Converts the
hsl()
origin color to an equivalent lab()
color — lab(54.29 80.8198 69.8997)
. - Sets the
l
and b
channel values for the output color to those of the origin lab()
equivalent's L
and b
channel values — those values are 54.29
and 69.8997
, respectively. - Sets the output color's
a
channel value to a new value not based on the origin color: -100
.
The final output color is lab(54.29 -100 69.8997)
.
Note: As mentioned above, if the output color is using a different color model to the origin color, the origin color is converted to the same model as the output color in the background so that it can be represented in a way that is compatible (i.e. using the same channels).
In the examples we've seen so far in this section, the alpha channels have not been explicitly specified for either the origin or output colors. When the output color alpha channel is not specified, it defaults to the same value as the origin color alpha channel. When the origin color alpha channel is not specified (and it is not a relative color), it defaults to 1
. Therefore, the origin and output alpha channel values are 1
for the above examples.
Let's look at some examples that specify origin and output alpha channel values. The first one specifies the output alpha channel value as being the same as the origin alpha channel value, whereas the second one specifies a different output alpha channel value, unrelated to the origin alpha channel value.
lab(from hsl(0 100% 50% / 0.8) l a b / alpha)
/* Computed output color: lab(54.29 80.8198 69.8997 / 0.8) */
lab(from hsl(0 100% 50% / 0.8) l a b / 0.5)
/* Computed output color: lab(54.29 80.8198 69.8997 / 0.5) */
In the following example, the hsl()
origin color is again converted to the lab()
equivalent — lab(54.29 80.8198 69.8997)
. calc()
calculations are applied to the L
, a
, b
, and A
values, resulting in an output color of lab(74.29 60.8198 29.8997 / 0.9)
:
lab(from hsl(0 100% 50%) calc(l + 20) calc(a - 20) calc(b - 40) / calc(alpha - 0.1))
Note: Because the origin color channel values are resolved to <number>
values, you have to add numbers to them when using them in calculations, even in cases where a channel would normally accept <percentage>
, <angle>
, or other value types. Adding a <percentage>
to a <number>
, for example, doesn't work.