Bootstrap, within the present newest model, v5.3.0, has began supporting coloration modes. Because of this in your Bootstrap layouts you’ll be able to select from its default mild or darkish mode, and even create a brand new one.
Within the Bootstrap docs, they’ve carried out a dropdown coloration mode toggle that may work as a start line for anybody who needs one thing comparable. However there’s no precise switcher element included in Bootstrap.



What if we need to construct our personal customized coloration mode switcher whereas respecting Bootstrap’s built-in darkish mode types? That is completely doable. Lately, we constructed a theme switcher, so let’s make it work with Bootstrap.
What we constructed prior to now
As a reminder, right here’s the sunshine/darkish toggle change element that we constructed within the earlier tutorial:
Bootstrap coloration mode switcher
To show it right into a Bootstrap-based toggle, we’ll make the next modifications:
- First, we’ll apply the darkish mode utilizing the
data-bs-theme="darkish"
HTML attribute as a substitute of thetheme-dark
class.
- Subsequent, we’ll take away all our darkish mode CSS variables as Bootstrap has built-in darkish mode types.
1 |
/*No want for them*/
|
2 |
|
3 |
:root { |
4 |
--white: #fff; |
5 |
--black: black; |
6 |
--text-color: var(--black); |
7 |
--bg-color: var(--white); |
8 |
}
|
9 |
|
10 |
.theme-dark { |
11 |
color-scheme: darkish; |
12 |
--text-color: var(--white); |
13 |
--bg-color: var(--black); |
14 |
}
|
- Lastly, to keep away from conflicts with the earlier demo, we’ll add the
bs
prefix earlier than all our native storage objects. For instance, we’ll change thedark-mode
key with thebs-dark-mode
one.
Up to date JavaScript
Right here’s the brand new required JavaScript code:
1 |
const html = doc.documentElement; |
2 |
const switches = doc.querySelector(".switches"); |
3 |
const inputs = switches.querySelectorAll("enter"); |
4 |
|
5 |
if (localStorage.getItem("bs-dark-mode")) { |
6 |
html.setAttribute("data-bs-theme", "darkish"); |
7 |
}
|
8 |
|
9 |
if (localStorage.getItem("bs-selected-radio")) { |
10 |
switches.querySelector( |
11 |
`#${localStorage.getItem("bs-selected-radio")}` |
12 |
).checked = "true"; |
13 |
}
|
14 |
|
15 |
const setTheme = (theme) => { |
16 |
if (theme === "darkish") { |
17 |
html.setAttribute("data-bs-theme", "darkish"); |
18 |
localStorage.setItem("bs-dark-mode", "true"); |
19 |
} else { |
20 |
html.removeAttribute("data-bs-theme"); |
21 |
localStorage.removeItem("bs-dark-mode"); |
22 |
}
|
23 |
};
|
24 |
|
25 |
const handleMediaChange = (e) => { |
26 |
if (switches.querySelector('[type="radio"]:checked').id === "auto") { |
27 |
setTheme(e.matches ? "darkish" : "mild"); |
28 |
}
|
29 |
};
|
30 |
|
31 |
const handleInputChange = (e) => { |
32 |
const themeMode = e.goal.id; |
33 |
if ( |
34 |
themeMode === "darkish" || |
35 |
(themeMode === "auto" && |
36 |
window.matchMedia("(prefers-color-scheme: darkish)").matches) |
37 |
) { |
38 |
setTheme("darkish"); |
39 |
} else { |
40 |
setTheme("mild"); |
41 |
}
|
42 |
localStorage.setItem("bs-selected-radio", themeMode); |
43 |
};
|
44 |
|
45 |
window
|
46 |
.matchMedia("(prefers-color-scheme: darkish)") |
47 |
.addEventListener("change", handleMediaChange); |
48 |
|
49 |
inputs.forEach((enter) => enter.addEventListener("enter", handleInputChange)); |
And the ensuing demo:
Override Bootstrap’s darkish mode variables
The Bootstrap built-in darkish mode is nice, however it could be nicer if we knew how one can customise these types.
For this demonstration, I created a brand new Bootstrap mission on GitHub. This time, I put in and included it through npm. Additionally, I used the Prepros app for simpler compilation of Sass information.
As one other reminder, a number of years in the past, I went by way of how one can customise Bootstrap’s Sass information. The idea stays the identical.
By default, Bootstrap shops all its darkish mode-specific Sass variables within the _variables-dark.scss
file.



In our case, let’s customise the default foreground and background web page colours that Bootstrap makes use of in darkish mode. To take action, we’ll navigate to our customized fundamental.scss
Sass file, goal the related variables, and modify their values, like this:






This leads to an look like this one:



We will additionally use Bootstrap’s color-mode
mixin to use extra types in darkish mode like this:
1 |
@embrace color-mode(darkish) { |
2 |
physique { |
3 |
border: 1px strong pink; |
4 |
}
|
5 |
}
|
This outputs to:
1 |
[data-bs-theme=dark] physique { |
2 |
border: 1px strong pink; |
3 |
}
|
Conclusion
That’s all, of us! Shortly, Bootstrap would possibly present an official toggle change element for overriding coloration schemes. However for now, you’ll be able to make the most of the one we constructed right here!
As at all times, thanks rather a lot for studying!