Color mixing and relative colors
Definition and purpose
color-mix() blends colors in a chosen interpolation space. Relative color syntax derives components from an existing color and optionally modifies them.
Equal sRGB mix
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>#a { color: purple; color: color-mix(in srgb, red, blue); }</style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>Equal weights give half red and half blue channel values.
Syntax and mental model
color-mix(in srgb, red 50%, blue) interpolates encoded sRGB channels; other spaces can yield different results. rgb(from red r g b / .5) retains source channels and changes alpha. Use an earlier fallback and choose the interpolation space deliberately.
Relative alpha
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>#a { color: rgb(from red r g b / .5); } #b { color: red; }</style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>A retains red with alpha .5, while B remains opaque.
Common mistakes
Mixing colors is not the same as overlaying two translucent elements. Do not assume a mix is accessible without checking the final foreground and background.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Modern color spaces. Next lesson: Box edges and spacing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Color mixing and relative colors — knowledge check
Five questions on color mixing and relative colors.
Which argument chooses the interpolation space?
What is the missing weight after white 75%?
What does from introduce in rgb(from red …)?
Which component follows slash in rgb()?
Why provide an earlier conventional color?