Creating a dark theme mode is a common trend today, which is understandable because it is a preferred style for many of us.

Using Spruce CSS, you can make any theme, which means you overwrite the default, generated CSS color custom properties.

Spruce has an empty $dark-colors map for the dark colors that we can use to attach the new values.

We need the config structure from our first how-to post.

scss
.
└── scss/
β”œβ”€β”€ config/
β”‚ β”œβ”€β”€ _index.scss
β”‚ β”œβ”€β”€ _config.scss
β”‚ β”œβ”€β”€ _dark-colors.scss
β”‚ β”œβ”€β”€ _dark-mode.scss
β”‚ └── _styles.scss
└── main.scss

Define the Colors in _dark-colors.scss

Declaring the colors can be extensive, so it is best to separate it into its file. In the file, you need the colors in the same format as under the $colors map.

scss
@use 'sass:color';
$color-primary: hsl(261 54% 70%);
$color-secondary: hsl(227 92% 55%);
$color-black: hsl(206 100% 7%);
$color-white: hsl(0 0% 95%);
$color-gray: hsl(0 0% 97%);
$color-gray-dark: hsl(0 0% 100% / 8%);
$color-danger: hsl(0 71% 51%);
$color-success: hsl(150 100% 33%);
$colors: (
base: (
card-border: hsl(207 90% 13%),
primary: $color-primary,
secondary: $color-secondary,
background: $color-black,
heading: $color-white,
text: $color-gray,
link: $color-primary,
link-hover: color.scale($color-primary, $lightness: -20%),
border: $color-gray-dark,
mark-background: hsl(50 100% 80%),
mark-foreground: $color-black,
marker: $color-primary,
code-background: hsl(207 64% 21%),
code-foreground: $color-white,
blockquote-border: $color-primary,
footer-background: hsl(0 0% 0% / 0.15)
),
...
);

Under the _config.scss file load these colors:

scss
@use 'dark-colors' as dark;
@use '~sprucecss/scss/spruce' with (
$dark-colors: dark.$colors
);

Generate the Colors in _dark-mode.scss

If you create a dark mode, you must identify the theme mode somehow. A typical pattern is to set a data attribute at your root element like this:

html
<html lang="en" data-theme-mode="dark">

After this, we can target our <html> element (with :root) with the data-theme-mode="dark" attribute selector. Hence overwrite the default colors if the attribute with the correct value is present.

scss
@use '~sprucecss/scss/spruce';
@include spruce.generate-color-variables(
spruce.$dark-colors,
':root[data-theme-mode="dark"]'
);

Additional Settings

Styling an extra theme mode can be tricky; in some cases overwriting just the colors isn’t enough. Sometimes you have to use the previous selector to target any element you want to restyle.

Here it is also a good idea to set the color-scheme to dark:

scss
[data-theme-mode='dark'] {
color-scheme: dark;
}

This will control your browser’s UI color, like the dark sidebar.

In Spruce, the icon of the select element is an inline SVG, so we can’t use CSS variables. Just typical color values to color it. Because of this, we must regenerate it under our theme modes (if needed).

scss
@use '~sprucecss/scss/spruce';
[data-theme-mode='dark'] {
select.form-control:not([multiple]):not([size]) {
@include spruce.field-icon(
spruce.$select-icon,
spruce.color(select-foreground, form, true, spruce.$dark-colors)
);
}
}