One Stylesheet, Many Hues

Before CSS custom properties existed, shipping multiple color schemes on one site typically meant maintaining separate stylesheets. Defining variables in a single sheet lets CSS handle the switching instead. Theming is useful even without user-selectable themes: splitting different areas of a site into their own color schemes is a common pattern.

The example below changes only the color hue between sections; lightness variations stay fixed. In HSL, that means a single value per theme, where an RGB palette would require editing several.

Same layout, different colors.

Custom Properties as the Foundation

Custom properties are widely supported, and polyfills and other solutions exist for IE 11. The syntax mirrors ordinary CSS declarations. Defining variables on :root is common—it is always <html> in HTML but with higher specificity—yet variables can live on any element, which is handy for scoping them. For example, a set of variables can be attached to data attributes.

Deriving Values With calc()

Variables need not be fixed values; calc() can compute them while keeping a uniform pattern across the palette. Since CSS has no loops, a preprocessor is convenient for generating part of the output. One caveat: CSS variables are not the same as Sass variables—the latter compile to values at build time and are no longer reachable in the DOM.

Wiring Themes to Sections

The goal is to restyle the same component across areas of a page. Three sections—#food, #lifestyle, and #travel, shown as tabs—each map to a different hue. The data-theme attribute on the div.wrapper element decides which hue is active.

With #travel active, --first-hue resolves to 180°, flows into --hue on the section, and produces a teal result:

<div class="wrapper">
.wrapper[data-theme="travel"] {
  --hue: var(--first-hue);  /* = 180° = teal */
}

A tab click updates the data-theme attribute to the section's ID with the hash removed. This calls for a small amount of JavaScript. Being accessible and manipulable from JavaScript is one advantage of CSS custom properties over preprocessor variables.

<li><a href="#food">Food</a></li>
const wrapper = document.querySelector('.wrapper');
document.querySelector("nav").addEventListener('click', e => {
  // Get theme name from URL and ditch the hash
  wrapper.dataset.theme = e.target.getAttribute('href').substr(1);
})

When JavaScript Is Unavailable

Scripts must account for users who have disabled JavaScript, or the UI becomes inaccessible. The snippet below keeps content reachable in that scenario:

// progressive enhancement:
// without JavaScript all sections are displayed, the theme is only set when the page loads
wrapper.dataset.theme = wrapper.querySelector('section').id;

The fallback lets the tabs scroll the page to the matching section. Theming is lost, but delivering content matters more. While this example is single-page, the sections could also be served as separate pages with [data-theme] set server-side.

Partial Patterns Still Work

Linear color changes suit the mathematical approach, but the same idea helps even when only part of the palette follows a pattern. If lightness is regular while hue is not, the stylesheet can be split accordingly:

<head>
  <style>
    :root {
      --hue: 260;
    }
  </style>
  <link rel="stylesheet" href="stylesheet-with-calculations-based-on-any-hue.css">
</head>

Web Components and host-context()

Web components are a compelling, evolving concept: encapsulated pieces reused anywhere and themed per context—one component, many contexts. Theming them with custom properties requires the :host-context() pseudo-selector.

:host-context(body[data-theme="color-1"]) {
  --shade-1: var(--outsideHSL);
}

Takeaways

Custom properties make theming far easier than earlier workarounds. The result is more maintainable (a single stylesheet), leaner (less code), and opens possibilities that preprocessor variables can't offer (JavaScript access). Teamed with HSL colors and calc(), the approach is stronger still. The tab example is only one case of theming a component by section; letting users pick themes themselves is another.