01. CSS Color Systems (HEX, RGB, HSL, OKLCH, color-mix)

🟢 Beginner

1. Definition

CSS supports multiple color models: HEX (#2563eb), RGB (rgb(37 99 235 / 0.8)), HSL (hsl(222 83% 53%)), modern wide-gamut OKLCH (oklch(0.6 0.25 250)), and functional color mixing with color-mix(in srgb, blue 50%, white).

2. Why It Is Used

Color models define visual identity and accessibility contrast. Modern OKLCH provides uniform lightness across all hues, solving the gray dead-zones of older sRGB models.

3. How It Works

OKLCH uses Lightness (0-1), Chroma (purity), and Hue angle (0-360 deg). color-mix() interpolates between two colors in a specified color space.

4. Syntax

.primary-box {
  background-color: oklch(0.6 0.25 250);
  color: color-mix(in srgb, var(--primary) 80%, black);
}

5. Color Models Reference Table

Color ModelSyntax FormatColor Space SupportBest Practice Usage
HEX#2563eb / #2563eb80sRGBQuick design token definitions
RGBrgb(37 99 235 / 0.8)sRGBDynamic alpha channel opacity
HSLhsl(222 83% 53%)sRGBIntuitive hue/saturation manipulation
OKLCHoklch(0.6 0.25 250)P3 & Wide-GamutModern High-DPI Color Systems

6. Beginner Example

.btn {
  background-color: #2563eb;
  color: #ffffff;
}

7. Intermediate Example

.overlay {
  background-color: rgb(15 23 42 / 0.75); /* Modern space-separated RGB with alpha */
}

8. Advanced Wide-Gamut Color Mixing Example

.badge-tint {
  background-color: oklch(0.65 0.2 240);
  border-color: color-mix(in oklch, oklch(0.65 0.2 240) 70%, white);
}

9. Line-by-Line Code Explanation

  • color-mix(in oklch, ... 70%, white): Dynamically generates a lighter border tint by mixing 70% primary OKLCH color with 30% white.

10. Expected Terminal / Browser Output

Vibrant, high-contrast UI components utilizing modern wide-gamut display color capabilities.

11. Common Mistakes & Pitfalls

  • Using comma syntax in modern functions: Writing rgb(37, 99, 235, 0.8) instead of modern space-separated rgb(37 99 235 / 0.8).

12. Important Rules & Limitations

  • Provide sRGB fallbacks when using P3 wide-gamut colors in older legacy browsers.

13. Multilingual Explanation: English

CSS supports HEX, RGB, HSL, and modern OKLCH color systems. Use OKLCH for uniform lightness and color-mix() for dynamic color tints.

14. Multilingual Explanation: Hindi (Roman Script)

CSS mein HEX, RGB, HSL aur OKLCH color spaces hote hain. Dynamic tinting ke liye color-mix() ka istemaal karein.

15. Multilingual Explanation: Marathi (Roman Script)

CSS madhye HEX, RGB, HSL ani modern OKLCH colors vaparta yetat. Color tints sathi color-mix() vapara.

16. Practice Questions

  1. Why is OKLCH superior to HSL for generating accessible color palettes?
  2. How does color-mix() simplify theme tint generation?

17. Practical Coding Challenge

Create a CSS rule for a .alert-banner using oklch(0.7 0.18 140) background and a border created via color-mix().

18. Related Topics