Using color-mix() & color-contrast() functions | CSS Color

Understanding color-mix()

The color-mix() function allows you to blend two or more colors together. It takes a mixing mode and a list of colors as arguments.

Syntax:

CSS

color-mix(mode, color1, color2, …)

Common Mixing Modes:

  • add: Adds the colors together.
  • subtract: Subtracts the second color from the first.
  • multiply: Multiplies the colors together.
  • screen: Inverts the colors and adds them together.
  • overlay: Blends the colors based on their relative luminance.
  • lighten: Lightens the first color by the second color.
  • darken: Darkens the first color by the second color.
  • saturation: Saturates the first color by the second color.
  • desaturate: Desaturates the first color by the second color.

Example:

CSS

.element {

  color: color-mix(add, red, blue);

}

Understanding color-contrast()

The color-contrast() function calculates the contrast ratio between two colors. It returns a number between 1 and 21, where a higher number indicates a higher contrast.

Syntax:

CSS

color-contrast(color1, color2)

Example:

CSS

.element {

  color: color-contrast(black, white); /* Returns 21 */

}

Using color-mix() and color-contrast() in Responsive Web Design

  • Creating dynamic color palettes: Use color-mix() to create color palettes that adapt to different themes or user preferences.
  • Improving accessibility: Use color-contrast() to ensure that text and background colors have sufficient contrast for readability.
  • Creating custom gradients: Combine color-mix() with gradients to create unique and visually appealing effects.

Example:

CSS

.element {

  background-image: linear-gradient(to right, color-mix(add, red, blue), color-mix(add, blue, green));

}

Note: These functions are relatively new and may not be fully supported by all browsers. You may need to use a polyfill or fallback mechanism. By effectively using color-mix() and color-contrast(), you can create more dynamic and visually appealing web designs.

OKLab & OKLCH | CSS Color
Introduction and Objectives | Stunning Aesthetics with CSS

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?