Bootstrap 5 Dark Mode
Since version 5.3, Bootstrap supports colour modes. Add data-bs-theme="dark" to an element and Bootstrap switches that element and everything inside it to a dark palette: backgrounds, text, borders, forms, dropdowns, tables, cards — all of it. No extra stylesheet is needed.
Dark mode for the whole page
Put the attribute on the <html> element to make the entire page dark.
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<h4>Dark page 🌙</h4>
<div class="card mb-2">
<div class="card-body py-2">Cards, forms and tables follow the theme.</div>
</div>
<input class="form-control mb-2" placeholder="A dark input">
<table class="table table-sm table-striped mb-2">
<tr><td>Row 1</td><td>✔</td></tr>
<tr><td>Row 2</td><td>✔</td></tr>
</table>
<button class="btn btn-primary">Primary</button>
<button class="btn btn-outline-light">Outline light</button>
</body>
</html>Change dark to light and run again — the same markup now renders in the default light theme.
Dark mode for one component
data-bs-theme works on any element, so you can mix modes: a dark navbar on a light page, a light card in a dark sidebar, and so on.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="row g-3">
<div class="col-6">
<div class="card h-100">
<div class="card-body">
<h6>Light card</h6>
<select class="form-select form-select-sm"><option>Light select</option></select>
</div>
</div>
</div>
<div class="col-6" data-bs-theme="dark">
<div class="card h-100">
<div class="card-body">
<h6>Dark card</h6>
<select class="form-select form-select-sm"><option>Dark select</option></select>
</div>
</div>
</div>
</div>
</body>
</html>A theme switcher
Toggling the mode is just changing the attribute with JavaScript. A real site would also remember the user's choice (for example in localStorage).
<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="d-flex justify-content-between align-items-center mb-3">
<h5 class="mb-0">Settings</h5>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="darkSwitch">
<label class="form-check-label" for="darkSwitch">Dark mode</label>
</div>
</div>
<div class="alert alert-info py-2">Alerts get darker, more muted colours.</div>
<p class="text-body-secondary">Secondary text adapts too.</p>
<script>
const root = document.documentElement;
document.getElementById('darkSwitch').addEventListener('change', (e) => {
root.setAttribute('data-bs-theme', e.target.checked ? 'dark' : 'light');
console.log('theme:', root.getAttribute('data-bs-theme'));
});
</script>
</body>
</html>Following the operating system setting
Users can set a dark or light preference in their operating system. The prefers-color-scheme media query exposes it, and you can read it from JavaScript with matchMedia:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script>
// Run early (in <head>) so the page never flashes the wrong theme
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
document.documentElement.setAttribute('data-bs-theme', prefersDark ? 'dark' : 'light');
</script>
</head>
<body class="p-3">
<h5>Auto theme</h5>
<p>This page uses your system's <code>prefers-color-scheme</code> setting.</p>
<script>console.log('system prefers dark:', prefersDark);</script>
</body>
</html>Theme-aware classes
Use colour classes that adapt to the mode instead of fixed ones. For example, .bg-light stays light even in dark mode, while .bg-body-tertiary follows the theme.
| Prefer (adapts) | Instead of (fixed) |
|---|---|
.bg-body, .bg-body-secondary, .bg-body-tertiary |
.bg-white, .bg-light |
.text-body, .text-body-secondary, .text-body-emphasis |
.text-dark, .text-muted |
.border (uses --bs-border-color) |
.border-dark |
.bg-{color}-subtle + .text-{color}-emphasis |
Hard-coded pastel colours |
data-bs-theme="dark" on navbars |
.navbar-dark (deprecated in 5.3) |
Under the hood, each mode is a set of CSS variables. You can override them for a mode in your own CSS:
[data-bs-theme="dark"] {
--bs-body-bg: #10131a;
--bs-primary: #9d7ae0;
--bs-primary-rgb: 157, 122, 224;
}Tip: Test both modes. Hard-coded colours in your own CSS (like
color: #222) won't change automatically — use Bootstrap's variables such asvar(--bs-body-color)instead.
Test yourself
Which attribute switches Bootstrap 5.3 to dark mode?
- class="dark"
- data-bs-theme="dark"
- data-theme="dark"
- data-bs-mode="night"
Where can you put data-bs-theme?
- Only on <html>
- Only on <body>
- On any element; it applies to that element and its children
- Only on navbars
Which background class adapts to dark mode?
- .bg-light
- .bg-white
- .bg-body-tertiary
- .bg-gray