Classic color formats: named colors, hex, RGB, and HSL
Modern color syntax with space-separated values
OKLCH: the modern perceptually uniform color space
color-mix() for dynamic color blending
Gradient interpolation in different color spaces
Special color keywords: currentColor and transparent
Creating consistent color palettes with OKLCH
Best practices for choosing color formats
Introduction to CSS Colors
CSS color capabilities have evolved dramatically over the years. What started with 147 named
colors and basic hex values has grown into a sophisticated system with perceptually uniform
color spaces, dynamic color mixing, and interpolation control.
Modern CSS colors go beyond aesthetics—they enable accessible design systems, vibrant
gradients, and precise color control that matches how humans actually perceive color.
Named Colors and Hexadecimal
The classics: 147 named colors and hexadecimal notation. These remain the most common
color formats due to their simplicity and wide support.
Named Colors
CSS defines 147 named colors from red to rebeccapurple. While
convenient for quick prototyping, they offer limited control and aren't suitable for
precise design systems.
Hexadecimal Format
Hex colors use the format #RRGGBB where each pair represents red, green,
and blue values in hexadecimal (00-FF). You can also use 3-digit shorthand (#RGB)
and 8-digit format with alpha (#RRGGBBAA).
RGB (Red, Green, Blue) and HSL (Hue, Saturation, Lightness) are the workhorses of CSS
colors. They come in both classic comma-separated syntax and modern space-separated syntax.
OKLCH (OK Lightness, Chroma, Hue) is a perceptually uniform color space, meaning equal
numeric changes produce equal visual changes. This is revolutionary for creating consistent
color palettes and gradients.
Why Perceptual Uniformity Matters
In HSL, hsl(120 50% 50%) (green) appears much brighter than
hsl(240 50% 50%) (blue) even though they have the same lightness value.
OKLCH fixes this—colors with the same lightness actually look equally bright.
The color-mix() function lets you blend two colors together in a specified
color space, without needing a preprocessor. It's perfect for creating hover states,
tints, shades, and dynamic color variations.
Modern CSS allows you to specify which color space to use when interpolating gradients.
This has a dramatic impact on the visual result.
The Problem with sRGB
By default, gradients interpolate in sRGB, which often creates muddy, grayish midpoints
between vibrant colors. This happens because sRGB isn't perceptually uniform.
The Solution: OKLCH Interpolation
Interpolating in OKLCH maintains saturation throughout the gradient, producing vibrant,
appealing transitions.
/* Old way - muddy middle colors */
background: linear-gradient(to right, red, blue);
background: linear-gradient(to right in srgb, red, blue);
/* Modern way - vibrant throughout! */
background: linear-gradient(to right in oklch, red, blue);
background: linear-gradient(to right in oklch, yellow, blue);
/* Also works with radial and conic gradients */
background: radial-gradient(in oklch, #ff0000, #0000ff);
background: conic-gradient(in oklch, red, yellow, green, blue, red);
Special Color Keywords
CSS provides special keywords that enable dynamic, context-aware coloring without
hard-coding values.
currentColor
The currentColor keyword references the current value of the
color property. It's perfect for icons, borders, and decorative elements
that should match the text color.
.icon-box {
color: #e91e63;
border: 3px solid currentColor; /* Matches text color */
}
.icon-box svg {
fill: currentColor; /* Icon matches text color */
}
/* Hover example */
.link {
color: blue;
text-decoration-color: currentColor;
}
.link:hover {
color: red;
/* text-decoration automatically updates! */
}
transparent
The transparent keyword is equivalent to rgba(0, 0, 0, 0).
It's commonly used for removing backgrounds or creating fade-to-nothing effects.
/* Remove background */
background: transparent;
/* Fade to transparent gradient */
background: linear-gradient(to right,
red,
rgba(255, 0, 0, 0.5),
transparent
);
/* Border trick for triangles */
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
border-left: 30px solid blue;
Creating Color Palettes with OKLCH
One of OKLCH's superpowers is generating entire color palettes programmatically. By
keeping chroma and hue constant while varying lightness, you create visually consistent
scales.