
Dynamic Palette Generation with CSS Relative Color Syntax
Design systems traditionally define dozens of static color tokens—hover shades, muted borders, focus rings, and translucent overlays—even when every variant derives mathematically from one brand hue. Until recently, automating those derivations required build-time Sass functions (darken(), mix(), rgba()) or JavaScript color libraries, both of which bake static hex values into compiled stylesheets and break down when users switch themes or customize accent colors dynamically at runtime.
CSS Relative Color Syntax from CSS Color Module Level 5, supported across all major evergreen browsers (Chrome, Edge, Safari, and Firefox), solves this directly in native CSS using the from keyword inside color functions. Paired with the perceptually uniform oklch() color space, a single base custom property can be destructured into its l (lightness), c (chroma), h (hue), and alpha channels and transformed with calc() right in the browser, keeping lightness shifts consistent across blues, yellows, and greens while automatically adapting to light and dark modes.
Example
:root {
--brand-accent: #2563eb;
}
.action-button {
background-color: var(--brand-accent);
border: 1px solid oklch(from var(--brand-accent) calc(l - 0.1) c h);
box-shadow: 0 6px 18px oklch(from var(--brand-accent) l c h / 0.28);
color: #ffffff;
}
.action-button:hover {
background-color: oklch(from var(--brand-accent) calc(l - 0.07) c h);
}
Implementation
/* ==========================================================================
Runtime Theme Palette Derived from a Single Base Token
========================================================================== */
:root {
/* Override only this single token to re-theme the entire component set */
--theme-accent: #0f766e;
/* Safe static fallbacks for older environments */
--accent-surface: #f0fdfa;
--accent-border: #99f6e4;
--accent-hover: #115e59;
--accent-ring: rgba(15, 118, 110, 0.35);
}
@supports (color: oklch(from red l c h)) {
:root {
/* Subtle tinted surface using lightness boost and reduced chroma */
--accent-surface: oklch(from var(--theme-accent) 0.96 calc(c * 0.3) h);
/* Crisp border one step darker and less saturated */
--accent-border: oklch(from var(--theme-accent) 0.82 calc(c * 0.55) h);
/* Perceptually uniform hover darkening */
--accent-hover: oklch(from var(--theme-accent) calc(l - 0.08) c h);
/* Translucent focus halo preserving original hue and chroma */
--accent-ring: oklch(from var(--theme-accent) l c h / 0.35);
/* Automatic complementary badge accent via hue rotation */
--accent-complement: oklch(from var(--theme-accent) l c calc(h + 150));
}
@media (prefers-color-scheme: dark) {
:root {
--accent-surface: oklch(from var(--theme-accent) 0.24 calc(c * 0.45) h);
--accent-border: oklch(from var(--theme-accent) 0.42 calc(c * 0.65) h);
--accent-hover: oklch(from var(--theme-accent) calc(l + 0.08) c h);
}
}
}
.callout-panel {
display: grid;
gap: 0.75rem;
padding: 1.25rem;
border: 1px solid var(--accent-border);
border-radius: 0.75rem;
background-color: var(--accent-surface);
color: inherit;
}
.callout-panel__action {
justify-self: start;
padding: 0.625rem 1.125rem;
border: 1px solid transparent;
border-radius: 0.5rem;
background-color: var(--theme-accent);
color: #ffffff;
font-weight: 600;
cursor: pointer;
transition: background-color 150ms ease, box-shadow 150ms ease;
}
.callout-panel__action:hover {
background-color: var(--accent-hover);
}
.callout-panel__action:focus-visible {
outline: 2px solid var(--theme-accent);
outline-offset: 2px;
box-shadow: 0 0 0 5px var(--accent-ring);
}
<section class="callout-panel" aria-labelledby="palette-heading">
<h2 id="palette-heading">Runtime Accent Customizer</h2>
<p>Pick any color to regenerate borders, surfaces, and focus rings live in CSS.</p>
<label for="accent-picker">
Base accent token:
<input id="accent-picker" type="color" value="#0f766e">
</label>
<button type="button" class="callout-panel__action">Apply Workspace Theme</button>
</section>
<script>
const picker = document.getElementById('accent-picker');
picker.addEventListener('input', (event) => {
document.documentElement.style.setProperty('--theme-accent', event.target.value);
});
</script>
Main Point: CSS Relative Color Syntax paired with oklch() derives perceptually consistent surfaces, borders, hover states, and alpha rings from a single CSS variable at runtime.
Credits
Photo by Cristina Gottardi on Unsplash