TExcelTAD
Home › Sass › Theming with Sass

Theming with Sass

A theme is a set of design decisions — colours, radii, fonts, shadows — that you can swap without rewriting components. Sass gives you two main strategies:

Strategy How Switch at runtime?
Compile-time themes Configure Sass variables / maps; build one CSS file per theme No (load a different file)
Runtime themes Sass generates CSS custom properties per theme; components use var() Yes (change a class or attribute)

Most modern sites use runtime themes for light/dark mode, and compile-time configuration for brand-level choices in shared libraries.

Theme maps

Describe each theme as a map with the same keys:

scss
$themes: (
  light: (bg: #ffffff, text: #222222, brand: #cc6699, surface: #f6f1f4),
  dark:  (bg: #18151a, text: #eeeeee, brand: #f48fb1, surface: #262028),
);

Runtime themes with custom properties

Loop over the themes and output each one as a set of custom properties, scoped to a data-theme attribute:

scss · try it yourself
$themes: (
  light: (bg: #ffffff, text: #222222, brand: #cc6699, surface: #f6f1f4),
  dark: (bg: #18151a, text: #eeeeee, brand: #f48fb1, surface: #262028),
);

@each $name, $theme in $themes {
  [data-theme="#{$name}"] {
    @each $key, $value in $theme {
      --#{$key}: #{$value};
    }
  }
}

.panel {
  background: var(--bg);
  color: var(--text);
  padding: 16px;
  border-radius: 10px;
}

.panel__chip {
  background: var(--surface);
  color: var(--brand);
  padding: 2px 10px;
  border-radius: 999px;
}
[data-theme=light] {
  --bg: #ffffff;
  --text: #222222;
  --brand: #cc6699;
  --surface: #f6f1f4;
}

[data-theme=dark] {
  --bg: #18151a;
  --text: #eeeeee;
  --brand: #f48fb1;
  --surface: #262028;
}

.panel {
  background: var(--bg);
  color: var(--text);
  padding: 16px;
  border-radius: 10px;
}

.panel__chip {
  background: var(--surface);
  color: var(--brand);
  padding: 2px 10px;
  border-radius: 999px;
}

The component rules are written once. Switching theme is just document.documentElement.dataset.theme = "dark" in JavaScript.

Respecting the user's preference

Use prefers-color-scheme to pick the dark theme automatically, while an explicit data-theme still wins:

scss · try it yourself
$light: (bg: #fff, text: #222);
$dark: (bg: #18151a, text: #eee);

@mixin vars($map) {
  @each $key, $value in $map {
    --#{$key}: #{$value};
  }
}

:root { @include vars($light); }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { @include vars($dark); }
}

[data-theme="dark"] { @include vars($dark); }
:root {
  --bg: #fff;
  --text: #222;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme=light]) {
    --bg: #18151a;
    --text: #eee;
  }
}
[data-theme=dark] {
  --bg: #18151a;
  --text: #eee;
}

Deriving theme colours

Let Sass compute supporting colours so each theme needs only a few inputs:

scss · try it yourself
@use "sass:color";

@function build-theme($brand, $mode: light) {
  @if $mode == light {
    @return (
      brand: $brand,
      brand-hover: color.scale($brand, $lightness: -15%),
      brand-soft: color.scale($brand, $lightness: 85%),
    );
  }
  @return (
    brand: color.scale($brand, $lightness: 30%),
    brand-hover: color.scale($brand, $lightness: 45%),
    brand-soft: color.scale($brand, $lightness: -70%),
  );
}

.theme-ocean {
  @each $k, $v in build-theme(#0277bd) {
    --#{$k}: #{$v};
  }
}
.theme-ocean {
  --brand: #0277bd;
  --brand-hover: rgb(0.6666666667%, 39.6666666667%, 63%);
  --brand-soft: rgb(81.4317831845%, 92.9263935941%, 99.8035109332%);
}

Compile-time themes with @use … with

Libraries expose !default variables so each project can configure them when loading:

scss
// _button-lib.scss
$brand: #0d6efd !default;
$radius: 6px !default;
.btn { background: $brand; border-radius: $radius; }
scss
// brand-a.scss
@use "button-lib" with ($brand: #cc6699, $radius: 999px);

// brand-b.scss
@use "button-lib" with ($brand: #2e7d32);

Compiling brand-a.scss and brand-b.scss gives two separate CSS files — great for white-label products, where each customer loads only their own.

Tip: Name tokens by role (--surface, --text-muted, --brand), not by colour (--pink). A role keeps its meaning in every theme.

Test yourself

Which approach lets users switch themes without reloading CSS?

  1. Compile-time Sass variables
  2. CSS custom properties generated by Sass
  3. @use … with
  4. @extend

Why should every theme map have the same keys?

  1. Sass requires it
  2. So components can use the same var(--key) names in every theme
  3. It makes CSS smaller
  4. Maps can't have different keys

What does @use "lib" with ($brand: red) configure?

  1. Any variable in lib
  2. Only variables declared with !default in lib
  3. Custom properties
  4. Nothing — it's invalid