One of the most significant upgrades in Bootstrap 5.3+ is the introduction of Native Color Modes. Rather than duplicating thousands of CSS classes or managing separate stylesheets, Bootstrap now uses CSS custom properties that dynamically recalculate background colors, border tones, and text contrast on the fly when data-bs-theme="dark" is toggled.
To switch your entire website to Dark Mode, simply add the data-bs-theme="dark" attribute to your root <html> tag:
<!doctype html>
<!-- Entire site in Dark Mode -->
<html lang="en" data-bs-theme="dark">
<head>...</head>
<body>
<!-- Content automatically renders with dark backgrounds and light text! -->
</body>
</html>
You can also apply Dark Mode selectively to individual components (e.g. keeping your site in light mode while giving the navbar or footer a dark theme):
<!-- Only this card renders in dark mode -->
<div class="card p-4 shadow" data-bs-theme="dark">
<h4>Dark Card Component</h4>
<p>This element inherits dark theme custom variables regardless of parent page theme.</p>
</div>
Here is a production JavaScript snippet that detects the user's OS system preference (prefers-color-scheme: dark), allows manual button toggling, and persists the choice across page loads using localStorage:
// theme-switcher.js - Persistent Theme Toggler
(() => {
'use strict';
const getStoredTheme = () => localStorage.getItem('theme');
const setStoredTheme = theme => localStorage.setItem('theme', theme);
const getPreferredTheme = () => {
const stored = getStoredTheme();
if (stored) return stored;
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
};
const setTheme = theme => {
document.documentElement.setAttribute('data-bs-theme', theme);
};
// Set initial theme immediately before render to avoid flash
setTheme(getPreferredTheme());
window.addEventListener('DOMContentLoaded', () => {
const toggleBtn = document.getElementById('themeToggleBtn');
if (toggleBtn) {
toggleBtn.addEventListener('click', () => {
const currentTheme = document.documentElement.getAttribute('data-bs-theme');
const newTheme = (currentTheme === 'dark') ? 'light' : 'dark';
setStoredTheme(newTheme);
setTheme(newTheme);
});
}
});
})();
Strengthen your responsive web design mastery by studying these connected classes:
Explore how color variables shift between light and dark themes.
Customize --bs-body-bg and --bs-body-color for custom theme modes.
Generate custom dark mode color palettes using Sass compilation.
Implement a persistent light/dark mode switcher using localStorage.
--bs-body-bg (shifts to #212529), --bs-body-color (shifts to #dee2e6), --bs-border-color, and all subtle backgrounds (--bs-primary-bg-subtle) automatically invert.
<head> of your document before the DOM renders.
[data-bs-theme='blue'] { --bs-body-bg: #0f172a; --bs-body-color: #f8fafc; }.
Advance your practical frontend engineering skills with the next topic in our synchronized curriculum.
Continue to Class 39: Sass Customization → Previous: ← Class 37: Ratio, Stacks & Helpers | Full Syllabus