π What is the color() Function?
color() is a powerful CSS function that allows you to specify colors from any supported color space (like srgb, display-p3, a98-rgb, prophoto-rgb, rec2020) and even perform adjustments like modifying lightness, saturation, or alpha.
Note
π― Why Use color()?
- Access wide-gamut colors (much richer than standard sRGB)
- Use color spaces used in photography and cinema
- Fine-tune colors with alpha + adjustment syntax
- Create consistent color systems across displays
π Basic Syntax
color() Syntax
color(<color-space> <component1> <component2> <component3> / <alpha>)Example spaces:srgb, display-p3, a98-rgb, prophoto-rgb, rec2020, xyz-d50, xyz-d65.
π¨ Example 1 β sRGB (Standard Web Color)
sRGB example
.box {
background: color(srgb 1 0 0); /* Red */
}Values range from 0 β 1 (not 0 β 255).
π¨ Example 2 β display-p3 (Wide Gamut)
display-p3 example
.bright {
background: color(display-p3 1 0.4 0.2);
}This produces brighter, more saturated colors on P3 screens (most modern phones).
Note
π¨ Example 3 β With Alpha Channel
Alpha example
.overlay {
background: color(srgb 0 0 0 / 0.5); /* 50% transparent black */
}π Supported Color Spaces
| Color Space | Description |
|---|---|
| srgb | Standard web color space |
| srgb-linear | Better mathematical blending |
| display-p3 | Wide gamut (brighter colors) |
| a98-rgb | Adobe RGB β photography-oriented |
| prophoto-rgb | Very wide gamut β photography |
| rec2020 | Ultra wide gamut β TV and cinema |
| xyz-d50 / xyz-d65 | Absolute scientific color spaces |
Note
π§ Why Use Values Between 0 and 1?
Because these color spaces use **fractional values**, giving greater precision than hex or rgb() values.
π₯ Example β Wide Gamut Gradients
P3 Gradient
.gradient {
background: linear-gradient(
to right,
color(display-p3 1 0 0),
color(display-p3 1 0.8 0)
);
}Gradients become smoother and more vibrant on supported displays.
π Example β Theme with color()
Theme Tokens
:root {
--brand: color(display-p3 0.8 0.2 0.5);
}
button {
background: var(--brand);
}Themes scale beautifully across different monitor types.
π― Using Color Adjustments
The color() function allows future-level adjustments, such as tweaking lightness, saturation, or hue (coming with Level 5 syntax).
Experimental Adjustments
color(display-p3 1 0 0 / 1 adjust(lightness 20%))Note
π§ͺ Mixing with color-mix()
Combine with color-mix()
.soft-brand {
background: color-mix(
in oklab,
color(display-p3 1 0.2 0.3) 70%,
white 30%
);
}You can mix advanced color spaces for perfect UI shades.
β οΈ Common Mistakes
- β Assuming color() supports hex inside the function (it doesn't)
- β Mixing srgb and p3 values incorrectly
- β Forgetting values must be 0β1
- β Expecting P3 colors on non-P3 screens
Note
π₯ Summary
The color() function brings modern, wide-gamut, high-precision color management to CSS. It enables vibrant themes, accurate gradients, color transformations, and future-friendly UI color systems far beyond classic hex or rgb().
Learn more βMDN Docs π