CSS Colors, Gradients & Background Mastery

From traditional sRGB and HSL to wide-gamut Display P3, perceptually uniform OKLCH, multi-layer gradients, and advanced blend modes.

1. CSS Color Spaces: sRGB, Display P3, and OKLCH

For decades, the web was trapped in the sRGB color space, designed for CRT monitors in 1996. Modern smartphone screens, laptops, and OLED displays support the Display P3 gamut, displaying up to 25% richer, more vibrant colors that legacy hex codes cannot express.

FormatSyntax ExampleGamut & Perceptual NatureBest Use Case
Hexadecimal#2563eb or #2563eb80sRGB only. Fixed 8-bit channels.Legacy design handoffs; compact strings.
RGB / RGBArgb(37 99 235 / 0.8)sRGB. Modern slash notation handles alpha cleanly.Direct hardware red/green/blue channel mapping.
HSL / HSLAhsl(221deg 83% 53% / 0.8)sRGB. Hue, Saturation, Lightness.Intuitive color generation; prone to perceptual brightness distortion.
OKLCH
Modern Standard
oklch(0.6 0.22 250 / 0.8)Wide Gamut (P3). Perceptually Uniform.Dynamic design systems, theme generation, accessible contrast math.

CSS Example

/* Modern color syntax demonstration */
.color-p3 {
  background: oklch(0.65 0.25 142); /* Vivid electric green impossible in sRGB */
  color: #0f172a;
  padding: 16px;
  border-radius: 8px;
  font-weight: 600;
  text-align: center;
}

2. CSS Gradients: Linear, Radial, and Conic

Gradients are classified as CSS <image> types, meaning they are applied using the background-image property, not background-color.

A. Linear Gradients

Transitions smoothly between colors along a straight axis. Angles can be declared via keywords (to right, to bottom right) or degrees (135deg).

B. Radial Gradients

Radiates outward from a central focal point. You can specify shapes (circle or ellipse) and size boundaries (closest-side, farthest-corner).

C. Conic Gradients

Rotates colors around a central 360° pivot point like a radar sweep. This is the secret to building pure CSS pie charts and color wheels!

CSS Example

/* Diverse gradient demonstrations */
.linear-demo {
  background: linear-gradient(135deg, #6366f1 0%, #ec4899 100%);
  color: white;
  padding: 16px;
  border-radius: 8px;
  margin-bottom: 10px;
  text-align: center;
}

.conic-pie {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: conic-gradient(#3b82f6 0% 40%, #10b981 40% 75%, #f59e0b 75% 100%);
  margin: 10px auto;
}

3. Background Positioning, Sizing & Text Clipping

Mastering background mechanics requires understanding how the browser places and bounds images:

  • background-size: cover: Scales the image so both width and height completely fill the container, cropping excess edges if aspect ratios differ.
  • background-size: contain: Scales the image so its entire contents are visible without cropping, potentially leaving empty letterboxing.
  • background-clip: text: Clips the background to the foreground text glyphs, creating stunning gradient headlines!

CSS Example

/* Vibrant gradient typography with background-clip */
.gradient-headline {
  font-size: 32px;
  font-weight: 800;
  background: linear-gradient(to right, #2563eb, #9333ea, #db2777);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
  text-align: center;
}

4. Multiple Backgrounds & Blend Modes

CSS allows multiple backgrounds layered on a single element, separated by commas.

Layering Order Rule: The first background specified in the CSS rule is rendered on the very top. The last background listed is rendered on the bottom, with background-color serving as the base canvas.

Blend Modes: Using background-blend-mode: overlay or mix-blend-mode: multiply combines textures, patterns, and gradients seamlessly without Photoshop exports.

CSS Example

/* Layered multiple background pattern */
.multi-bg {
  background-image: 
    radial-gradient(circle at 20% 20%, rgba(255, 255, 255, 0.4) 0%, transparent 40%),
    linear-gradient(45deg, #1e293b 25%, transparent 25%),
    linear-gradient(-45deg, #1e293b 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #1e293b 75%),
    linear-gradient(-45deg, transparent 75%, #1e293b 75%);
  background-size: 100% 100%, 20px 20px, 20px 20px, 20px 20px, 20px 20px;
  background-position: 0 0, 0 0, 0 10px, 10px -10px, -10px 0px;
  background-color: #0f172a;
  color: #f8fafc;
  padding: 30px;
  border-radius: 8px;
  text-align: center;
}

5. Frequently Asked Questions (FAQ)

Under WCAG 2.1 Level AA, standard body text must have a minimum contrast ratio of 4.5:1 against its background. Large text (at least 18pt or 14pt bold) requires a minimum ratio of 3:1. Level AAA requires 7:1 for standard text.

In traditional sRGB gradients (e.g. blue to yellow), interpolating in sRGB passes through a "dead zone" of murky greyish-brown hues. In modern CSS, using linear-gradient(in oklch, blue, yellow) interpolates through perceptually uniform color space, keeping colors vivid throughout the gradient transition.

background-blend-mode controls how multiple background images and background colors on the same element blend together. mix-blend-mode controls how an element blends with whatever DOM elements and backgrounds sit underneath it in the stacking context.

Explore Related Tools

Hand-picked utilities and calculators related to this tool.

Related Tools