CSS Color Properties You Might Not Know About

color() function, accent-color, currentColor, color-mix(), Wide Gamut Colors, Relative Colors, Interpolation Color Spaces, forced-colors

CSS offers various color-related properties and functions that give developers greater control over visual elements. This article introduces some lesser-known CSS color properties, which can help you manage colors more flexibly.

1. color() Function

The color() function is a new method in CSS for defining colors, allowing developers to specify different color spaces like srgb, display-p3, and more. Compared to traditional rgb() and hsl() functions, color() is more versatile, especially for ensuring color accuracy on wide-gamut displays.

The basic syntax of the color() function is:

color(colorspace c1 c2 c3[ / A])

// Relative value syntax
color(from <color> colorspace c1 c2 c3[ / A])

Here, c1, c2, and c3 can be numbers or percentages representing components of the color space, while A is an optional alpha channel. The relative value syntax lets you derive one color from another, though it’s less commonly used.

The color() function is particularly useful for creating websites that need to look great on wide-gamut devices. For example, Apple’s Retina displays support the P3 color space, which offers more colors than standard sRGB.

Additionally, color() can be used in gradients and other effects. For instance:

.element {
  width: 200px;
  height: 200px;
  background: linear-gradient(
    to right,
    color(display-p3 1 0 0),
    color(display-p3 0 0 1)
  );
}

This gradient transitions from pure red to pure blue in the P3 color space, resulting in smoother color transitions than traditional rgb() gradients.

2. accent-color

The accent-color property allows developers to customize the colors of user interface elements (such as checkboxes and radio buttons) without needing to rewrite extensive styles. This is particularly useful in modern web design since form elements often default to system styles. With accent-color, you can easily match these elements to your website’s branding.

A common use case is ensuring that form controls align with a brand’s color scheme without manually styling each one.

3. currentColor

currentColor is a keyword in CSS that references the current text color of an element. This reduces redundant color definitions and ensures consistent styling across different states. For example, if you want a border color to match the text color, you can use currentColor instead of specifying a color value manually.:

.element {
  color: #3498db; /* Blue text */
  border: 2px solid currentColor; /* Border matches text color */
}

This approach simplifies code and offers flexibility. When the primary color of a page changes, styles like borders and shadows that rely on currentColor will automatically update, maintaining consistency.

4. color-mix()

color-mix() is a powerful CSS function that allows you to blend two colors at a specified ratio to create a new color. This is especially useful for creating dynamic color transitions or gradient effects where UI elements require blended colors.

The basic syntax is:

color-mix(in color-space, color1 percentage1, color2 percentage2)

Here, color-space defines the color space for the mix, color1 and color2 are the colors being mixed, and percentage1 and percentage2 define the ratio of each color. For example:

Subscribe to Premium to read the rest.

Become a paying subscriber of Premium to get access to this post and other subscriber-only content.

Already a paying subscriber? Sign In.

A subscription gets you:

  • • 🚀 Join 10K+ Subscribers and Grow Together with 500+ Members
  • • 📅 Latest Web Insights Every Wednesday and Sunday
  • • 🎯 Tailored Web Development Content to Help You Grow
  • • 💌 Exclusive Member Emails Every Saturday
  • • 🔓 Cancel Anytime, No Hassle