Course contentsShow
← Web Development
Lesson 492 of 2,9828. Advanced CSS Selectors and FeaturesFree lesson

The color() Function and Specifying Color Spaces

Use the color() function to specify colors in different color spaces like display-p3, rec2020, and a98-rgb for richer hues.

The color() Function and Specifying Color Spaces

What you'll learn: How to use the color() function to access wider color gamuts beyond sRGB for more vibrant, accurate colors.

The Core Idea

In the previous lesson, you learned that different color spaces exist—like sRGB (the web default) and Display-P3 (which can show more vivid colors). But how do you actually use these richer colors in CSS? That's where the color() function comes in.

The color() function lets you specify a color in a particular color space by naming the space and providing the color values. Think of it like choosing which box of crayons to use: sRGB is your standard 64-pack, while Display-P3 is the deluxe 120-pack with colors that simply don't exist in the smaller set.

Basic Syntax

.vibrant-element {
  /* Using Display-P3 color space */
  background-color: color(display-p3 1 0.5 0);
  
  /* Using Rec2020 color space */
  color: color(rec2020 0.8 0.2 0.4);
  
  /* Using A98-RGB color space */
  border-color: color(a98-rgb 0.6 0.9 0.3);
}

The numbers after the color space name represent red, green, and blue values, typically ranging from 0 to 1 (similar to how rgb() uses 0-255, but normalized).

Why Use Different Color Spaces?

Modern displays (especially newer phones, tablets, and monitors) can show colors beyond sRGB's range. Using display-p3, rec2020, or a98-rgb lets you tap into these richer hues—making oranges more vibrant, greens more lush, and reds more saturated on compatible devices. Older displays will simply show the closest equivalent they can.

Key Takeaway: The color() function unlocks wider color gamuts by letting you specify colors in spaces like display-p3, rec2020, and a98-rgb, delivering richer, more accurate colors on modern displays.