CSS Color-Mix in Practice: Building Themeable UI Components Without Preprocessors

December 1, 2026

Why color-mix() belongs in a component library

If you maintain buttons, cards, alerts, or inputs across multiple products, you have probably shipped a long map of color variables: --color-primary, --primary-hover, --primary-active, --primary-border, plus another set for dark mode. color-mix() lets the component derive those states from one consumer-provided color.

css code editor colors

The syntax is simple:

color-mix(in srgb, var(--brand) 88%, black)

That produces a color that is 88% brand and 12% black in the sRGB color space. No Sass, no JavaScript color parser, and no build-time theme generation required.

ui design system buttons

Start with semantic tokens, not one-off mixtures

Do not sprinkle color-mix() through every selector. Define component-level custom properties once. Consumers still get stable names such as --button-bg, while the library controls derived states.

.button {
  --brand: #2563eb;
  --button-bg: var(--brand);
  --button-color: #fff;
  --button-border: color-mix(in srgb, var(--button-bg) 72%, black);
  --button-hover-bg: color-mix(in srgb, var(--button-bg) 88%, black);
  --button-active-bg: color-mix(in srgb, var(--button-bg) 78%, black);
  --focus-ring: color-mix(in srgb, var(--button-bg) 35%, transparent);
}

A practical starting rule is:

  • Hover: mix 12% black or white into the base color.
  • Active/pressed: mix 22% black or white.
  • Border: mix 25–35% black for light surfaces, or white for dark surfaces.
  • Focus ring: mix the base color with 60–75% transparent.

These are starting values, not universal truths. Use the browser preview and a contrast checker to tune them.

Choosing the color space and mixing partner

srgb: predictable component defaults

in srgb is the safest default for UI libraries. It behaves like the familiar hex/RGB interpolation model and is easy to reason about when mixing with white, black, or transparent. Use it when broad consistency and simple fallback generation matter.

oklch: better perceptual ramps, with caution

in oklch often creates smoother perceived lightness and hue shifts, which can be useful for generating multi-step palettes. The risk is that two colors may travel through unexpected hue angles, and older browsers do not support the syntax as widely. Use oklch for curated palette scales, but verify every swatch visually.

white, black, surface, and transparent

Mixing with white lightens a color; mixing with black darkens it. Mixing with a semantic --surface token is often better because the result adapts to cards, subtle backgrounds, and inverted sections. Use transparent for overlays, focus rings, scrims, and ghost-button hover washes.

A common mistake is treating transparent as a paint color for text or borders. A translucent border can disappear on a different background. For durable borders, mix with the actual surface or use a more opaque color.

Complete button and card example

This example keeps public hooks simple: --button-bg, --card-accent, and data-theme. Consumers can override a variant with .button--secondary or set a brand color on any wrapper.

<div class="surface" data-theme="light">
  <button class="button">Save changes</button>
  <button class="button button--secondary">Cancel</button>

  <article class="card">
    <h2>Deployment</h2>
    <p>Your project is ready to publish.</p>
    <button class="button">Continue</button>
  </article>
</div>
.surface {
  --brand: #2563eb;
  --surface-bg: #ffffff;
  --surface-text: #111827;
  --surface-border: #d7dce3;

  background: var(--surface-bg);
  color: var(--surface-text);
}

.surface[data-theme="dark"] {
  --brand: #8ab4ff;
  --surface-bg: #111827;
  --surface-text: #f3f6fb;
  --surface-border: #313a49;
}

.button {
  --button-bg: var(--brand);
  --button-fg: #ffffff;
  --button-border: color-mix(in srgb, var(--button-bg) 70%, black);
  --button-hover: color-mix(in srgb, var(--button-bg) 88%, black);
  --button-active: color-mix(in srgb, var(--button-bg) 78%, black);
  --focus-ring: color-mix(in srgb, var(--button-bg) 35%, transparent);
  --ghost-hover: color-mix(in srgb, var(--button-bg) 12%, transparent);

  border: 1px solid var(--button-border);
  background: var(--button-bg);
  color: var(--button-fg);
  border-radius: 0.625rem;
  padding: 0.625rem 1rem;
  font: inherit;
}

.button:hover {
  background: var(--button-hover);
}

.button:active {
  background: var(--button-active);
}

.button:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.button--secondary {
  --button-bg: #64748b;
}

.surface[data-theme="dark"] .button {
  --button-fg: #0b1220;
  --button-border: color-mix(in srgb, var(--button-bg) 72%, white);
  --button-hover: color-mix(in srgb, var(--button-bg) 86%, white);
  --button-active: color-mix(in srgb, var(--button-bg) 74%, white);
}

.card {
  --card-accent: var(--brand);

  border: 1px solid var(--surface-border);
  border-top: 4px solid color-mix(in srgb, var(--card-accent) 75%, black);
  background: color-mix(in srgb, var(--card-accent) 7%, var(--surface-bg));
  border-radius: 0.875rem;
  padding: 1.25rem;
}

.card .button {
  margin-top: 1rem;
}

Browser support and fallback

Modern browsers support color-mix(), but component libraries should still serve a readable baseline. The safest pattern is to declare static colors first and enhance with mixed colors inside @supports.

.button {
  --button-bg: #2563eb;
  --button-fg: #ffffff;
  --button-border: #1e40af;
  --button-hover: #1d4ed8;
  --button-active: #1e40af;

  background: var(--button-bg);
  border-color: var(--button-border);
}

@supports (background: color-mix(in srgb, red, white)) {
  .button {
    --button-border: color-mix(in srgb, var(--button-bg) 70%, black);
    --button-hover: color-mix(in srgb, var(--button-bg) 88%, black);
    --button-active: color-mix(in srgb, var(--button-bg) 78%, black);
  }
}

@supports not (background: color-mix(in srgb, red, white)) {
  .button {
    background: var(--button-bg);
    border-color: var(--button-border);
  }

  .button:hover {
    background: var(--button-hover);
  }
}

Handle invalid custom property declarations carefully

If a browser does not understand a declaration, it discards that declaration at parse time. This can matter when several variables are declared together:

.bad {
  --button-hover: #1d4ed8;
  --button-active: color-mix(in srgb, var(--button-bg) 78%, black); /* unsupported */
}

A supporting browser uses both. An older browser keeps the first valid declaration and ignores the unsupported one, so --button-active may be inherited or invalid. Put baseline values in a separate rule and put the full enhanced set inside the positive @supports block. That makes fallback behavior explicit instead of relying on inheritance.

If consumers can pass arbitrary brand colors, document that legacy browsers use the closest static palette. Generating every possible fallback on the client is unnecessary for most design systems and usually requires JavaScript.

Accessibility checks

Color mixing is convenient, but it does not guarantee accessible contrast. Check the final computed colors against the actual background.

Button text against background

WCAG 2.1 Level AA requires a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text (18pt, or 14pt bold and above). UI icon graphics and meaningful graphical objects generally need 3:1 against adjacent colors.

For example, white text on #2563eb passes at about 4.6:1 for normal text. White text on a lightened hover such as #93b4fc can fall below 2.5:1 and fails. Do not assume hover can ignore contrast; keyboard users may focus the same control and screen magnifier users need the state to remain readable.

Border against background

A primary button border is decorative if the button fill already defines the shape, but an outline or secondary button often depends on its border. Aim for at least 3:1 between the border and adjacent background when the border communicates the control boundary.

A 1px #cbd5e1 border on white may be around 1.5:1 and fails that boundary check. A darker #64748b border on white is above 4.5:1 and passes comfortably.

Ghost hover and focus states

Ghost buttons are risky because a 8–10% brand wash may be invisible. Start with a 12% hover overlay, then test it on your surface. More importantly, never use color alone for focus. A 3:1 focus ring is the minimum target, but a 2px outer ring plus a 2px transparent offset is usually easier to see.

For a ghost button on white, a hover made from color-mix(in srgb, #2563eb 12%, transparent) gives a subtle blue wash. It may work as a hover cue, but it should be paired with an underline, icon, background shape, or border change. The focus ring should be much stronger, such as a 35–45% opaque brand ring.

Dark mode links and brand colors

Colors that pass on white often fail in dark mode. A saturated #2563eb link on #111827 can be close to 3:1, which is not enough for normal body text. A lighter #8ab4ff usually clears 7:1 on that background and is a better dark-theme brand token.

Use browser DevTools to inspect the computed color, then paste foreground and background hex values into a contrast checker. Test these combinations:

  • default label/background and hover label/background;
  • border/background for outline controls and input fields;
  • focus ring/surface and focus ring/control;
  • link/text surface in both light and dark themes;
  • disabled styles, while confirming disabled controls are not the only way to understand an action.

Practical theming guidance

Let consumers override semantic component variables rather than internal mixtures. A consumer should set --button-bg or --brand, not --button-hover, unless they intentionally need a custom state. This keeps the API small while allowing variants such as .button--secondary or scoped themes through data-theme.

For a production component, I keep the base token, one surface token, and derived state tokens in the same component block. Then I test the default theme, a dark theme, and one custom brand color chosen specifically because it is awkward: very light yellow, deep purple, or low-chroma gray. Those cases expose weak text contrast and muddy mixes quickly.

Conclusion

color-mix() removes much of the repetitive color work from component authoring. You can keep a simple consumer API, derive hover and active states, adapt borders to surfaces, and create translucent overlays without a preprocessor. The production-ready approach is conservative: define semantic variables, start with fixed mixing rules, provide static fallbacks, and verify every final color for contrast. Once that pipeline is in place, theming a component becomes a matter of changing one color instead of maintaining a dozen files.