HSL Channel Variables for Opacity Modifiers

Store HSL channel values in CSS variables to enable alpha/opacity modifiers on any color, compatible with Tailwind CSS and modern frameworks.

Design Tokens

Detailed Explanation

HSL Channel Variables

Storing colors as separate HSL channels unlocks a powerful pattern: applying arbitrary opacity to any color without defining new variables for each alpha value.

The Problem

With hex or full HSL values, you cannot add transparency:

:root {
  --primary: #3b82f6;
}
.overlay {
  /* Cannot add alpha to var(--primary) */
  background: var(--primary); /* no opacity control */
}

The Solution — Channel Variables

Store only the channel values, then reconstruct with hsl():

:root {
  --primary-h: 217;
  --primary-s: 91%;
  --primary-l: 60%;
}

.button {
  background: hsl(var(--primary-h) var(--primary-s) var(--primary-l));
}

.button-ghost {
  background: hsl(var(--primary-h) var(--primary-s) var(--primary-l) / 0.1);
}

.focus-ring {
  box-shadow: 0 0 0 3px hsl(var(--primary-h) var(--primary-s) var(--primary-l) / 0.4);
}

Tailwind Integration

This pattern enables Tailwind's opacity modifier syntax (bg-primary/50):

// tailwind.config.js
colors: {
  primary: 'hsl(var(--primary-h) var(--primary-s) var(--primary-l) / <alpha-value>)',
}

Generating Channel Variables

The CSS Variable Generator can produce shade scales as hex values. To convert to channel variables, extract the H, S, L components:

/* From: --primary-500: #3b82f6; */
/* To: */
--primary-500-h: 217;
--primary-500-s: 91%;
--primary-500-l: 60%;

Modern Alternative — relative color syntax

CSS relative color syntax (color-mix(), hsl(from var(--primary) h s l / 0.5)) will eventually make channel variables unnecessary. Until browser support is universal, the channel pattern remains the most reliable approach.

Use Case

Projects using Tailwind CSS or custom utility systems that need alpha/opacity variants of theme colors without defining separate variables for every opacity level.

Try It — CSS Variable Generator

Open full tool