✦ A decade of Canvas craft, now driven by AI — describe it, watch it build live.Start building
Bootstrap 5Utilities

Bootstrap 5 Dark Mode Toggle

A Dark Mode Toggle is a UI control — typically a checkbox-based switch or button — that allows users to switch between light and dark color schemes on a webpage. In Bootstrap 5, it works by toggling the `data-bs-theme` attribute on the `<html>` or a parent element, which activates Bootstrap's built-in dark theme variables. Use it whenever your site targets users in low-light environments, creative tools, developer dashboards, or any context where eye comfort and aesthetic preference matter.

Primary Class

.dark-mode-toggle

Common Use Cases

  • SaaS dashboards where developers or analysts work long hours and prefer dark interfaces to reduce eye strain during late-night sessions
  • Portfolio sites for designers and developers where dark mode signals technical credibility and matches the aesthetic expectations of the industry
  • Documentation portals (like component libraries or API references) where users spend extended periods reading code snippets and prose
  • E-commerce storefronts that want to offer a premium, personalized experience by persisting the user's theme preference in localStorage across sessions

Variants & Classes

VariantDescription
Dark Mode Toggle DefaultStandard dark mode toggle with Bootstrap's default styling.
Dark Mode Toggle ResponsiveResponsive variant that adapts to different screen sizes.

Code Example

<div class="d-flex align-items-center gap-2">
  <span id="theme-label" class="form-label mb-0 text-body">Dark Mode</span>
  <div class="form-check form-switch mb-0">
    <input
      class="form-check-input"
      type="checkbox"
      role="switch"
      id="darkModeToggle"
      aria-labelledby="theme-label"
    />
  </div>
</div>

<script>
  const toggle = document.getElementById('darkModeToggle');
  const root = document.documentElement;

  // Load saved preference
  const saved = localStorage.getItem('theme');
  if (saved === 'dark') {
    root.setAttribute('data-bs-theme', 'dark');
    toggle.checked = true;
  }

  toggle.addEventListener('change', () => {
    if (toggle.checked) {
      root.setAttribute('data-bs-theme', 'dark');
      localStorage.setItem('theme', 'dark');
    } else {
      root.setAttribute('data-bs-theme', 'light');
      localStorage.setItem('theme', 'light');
    }
  });
</script>

Live Examples

Basic Dark Mode Toggle

Example 1

Canvas Framework Variants

The Canvas template extends Bootstrap 5 with 1,658+ component variants. Generate any of these using Canvas Builder:

  • Canvas Builder generated dark mode toggle with custom colours
  • Dark Mode Toggle with interactive states
  • Responsive dark mode toggle for all screen sizes

Best Practices

Set the theme on <html>, not <body>

Apply `data-bs-theme="dark"` to `document.documentElement` (the `<html>` element) rather than `<body>` — this ensures Bootstrap's CSS variables cascade correctly to all components including modals and dropdowns that render outside the body flow.

Respect the OS preference on first load

Before reading localStorage, check `window.matchMedia('(prefers-color-scheme: dark)').matches` as a fallback — this way users get the correct theme immediately without a flash of the wrong mode on their first visit.

Avoid a flash of unstyled theme (FOUT)

Place your theme-detection script in a `<script>` tag directly inside `<head>` — not deferred or async — so the `data-bs-theme` attribute is set before the browser paints, preventing a visible flicker between light and dark.

Use Bootstrap's color-mode utility classes for fine-grained control

Bootstrap 5.3+ introduces `data-bs-theme` at the component level, so you can pin specific cards or sidebars to `data-bs-theme="dark"` regardless of the global setting — useful for sticky navbars that need to stay dark even in light mode.

FAQ

How does Bootstrap 5's dark mode actually work under the hood?
Bootstrap 5.3 introduced a color mode system based on CSS custom properties. When `data-bs-theme="dark"` is set on a parent element, Bootstrap re-maps its CSS variables (like `--bs-body-bg`, `--bs-body-color`, `--bs-border-color`) to their dark-mode values defined in `_variables-dark.scss`. This means all Bootstrap components — buttons, cards, navbars, forms — automatically adapt without any additional class changes. You do not need a separate dark stylesheet; it is all handled through variable overrides.
How do I style custom elements that Bootstrap doesn't cover in dark mode?
Use the `[data-bs-theme='dark']` CSS selector to write scoped overrides for your custom components. For example: `[data-bs-theme='dark'] .custom-card { background-color: #1e1e2e; color: #cdd6f4; }`. You can also define your own CSS custom properties and reassign them inside that selector, mirroring the pattern Bootstrap uses internally. Avoid hardcoding colors in your base styles — always rely on variables so the override block stays minimal.
How does Canvas Builder handle dark mode toggle generation?
Canvas Builder automatically injects the dark mode toggle with pre-wired JavaScript that reads and writes to localStorage and respects `prefers-color-scheme` on first load. When you define brand colors in your Canvas project, the generator maps them to Bootstrap's CSS variable override system so your custom palette adapts correctly in both light and dark states — you are not left with brand buttons that look wrong in dark mode.