
Bs5 Dark Mode in Bootstrap
Bootstrap 5 doesn’t have a built-in automatic dark mode toggle out of the box, but it supports dark mode styling via utility classes and you can easily create dark-themed components and layouts.
Here’s how you can work with dark mode in BS5:
? 1. Using Dark Background and Text Utilities
<div class="bg-dark text-white p-3"> This is a dark background with white text.</div>
Use
.bg-dark
for dark backgroundsUse
.text-white
for white text on dark backgrounds
? 2. Dark Themed Components
Many Bootstrap components support .bg-dark
and .text-white
together:
<nav class="navbar navbar-dark bg-dark"> <a class="navbar-brand" href="#">Dark Navbar</a></nav>
? 3. Using .table-dark
Tables also support dark mode styling:
<table class="table table-dark"> <thead> <tr><th>#</th><th>Name</th></tr> </thead> <tbody> <tr><td>1</td><td>John</td></tr> </tbody></table>
?? 4. Custom Dark Mode with CSS Variables (Recommended)
Bootstrap 5 uses CSS variables for colors, so you can override them to create dark mode:
:root { --bs-body-bg: #121212; --bs-body-color: #e0e0e0; --bs-primary: #bb86fc; --bs-secondary: #03dac6; /* override other vars as needed */}
Toggle this with a class on <body>
:
body.dark-mode { --bs-body-bg: #121212; --bs-body-color: #e0e0e0; /* other variable overrides */}
? 5. Toggle Dark Mode with JavaScript
const toggleBtn = document.getElementById('darkModeToggle');toggleBtn.addEventListener('click', () => { document.body.classList.toggle('dark-mode');});
? Summary
Bootstrap’s utilities let you quickly make dark backgrounds and text.
Override CSS variables for a full dark theme.
Combine with JavaScript for toggling dark mode.
Would you like a full dark mode toggle example with Bootstrap 5, or tips on integrating with CSS frameworks like Tailwind or using SCSS variables?