Providing full Bootstrap 5 admin panel with dynamic theme stylesheet switcher
Here's a complete HTML example of a Bootstrap 5 admin panel that swaps the stylesheet href to change themes and persists the choice in localStorage while preventing flash of unstyled content by setting the theme link in the head script.
Below is a minimal Bootstrap 5 “admin” layout that swaps the entire stylesheet when you change the theme. The choice is stored in localStorage, and the correct stylesheet is injected as early as possible to prevent a flash of unstyled content.
For the example, assume you have two files:
css/bootstrap-light.min.css
css/bootstrap-dark.min.css
You can generate these via Bootswatch or your own customized builds.
<!doctype html>
<html lang="en" data-bs-theme="light">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bootstrap Admin with Theme Switch</title>
<!-- Inject the chosen theme before paint to avoid FOUC -->
<script>
(function() {
const savedTheme = localStorage.getItem('theme') || 'light';
const link = document.createElement('link');
link.id = 'themeStylesheet';
link.rel = 'stylesheet';
link.href = savedTheme === 'dark'
? 'css/bootstrap-dark.min.css'
: 'css/bootstrap-light.min.css';
document.documentElement.setAttribute('data-bs-theme', savedTheme);
document.head.appendChild(link);
})();
</script>
</head>
<body>
<header class="navbar navbar-expand-lg navbar-light bg-light border-bottom">
<div class="container-fluid">
<a class="navbar-brand" href="#">Admin Panel</a>
<button class="navbar-toggler" type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="d-flex align-items-center ms-auto">
<div class="me-2">Theme:</div>
<select id="themeToggle" class="form-select form-select-sm w-auto">
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</div>
</div>
</header>
<div class="container-fluid mt-3">
<div class="row">
<nav class="col-md-3 col-lg-2 d-md-block bg-body-tertiary sidebar collapse show" id="sidebarMenu">
<div class="position-sticky pt-3">
<ul class="nav flex-column">
<li class="nav-item"><a class="nav-link active" href="#">Dashboard</a></li>
<li class="nav-item"><a class="nav-link" href="#">Orders</a></li>
<li class="nav-item"><a class="nav-link" href="#">Products</a></li>
<li class="nav-item"><a class="nav-link" href="#">Customers</a></li>
</ul>
</div>
</nav>
<main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
<h1 class="h2 mt-3">Dashboard</h1>
<p class="lead">A simple admin layout with a theme switcher that swaps the stylesheet.</p>
<div class="row">
<div class="col-12 col-md-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">Widget 1</h5>
<p class="card-text">Some text inside the widget.</p>
</div>
</div>
</div>
<div class="col-12 col-md-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">Widget 2</h5>
<p class="card-text">Some text inside the widget.</p>
</div>
</div>
</div>
<div class="col-12 col-md-4">
<div class="card">
<div class="card-body">
<h5 class="card-title">Widget 3</h5>
<p class="card-text">Some text inside the widget.</p>
</div>
</div>
</div>
</div>
</main>
</div>
</div>
<!-- Bootstrap JS Bundle (requires Popper) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-..."
crossorigin="anonymous"></script>
<script>
(function() {
const select = document.getElementById('themeToggle');
// Initialize the select based on stored value
select.value = localStorage.getItem('theme') || 'light';
select.addEventListener('change', (e) => {
const theme = e.target.value;
const link = document.getElementById('themeStylesheet');
// Swap stylesheet href
link.href = theme === 'dark'
? 'css/bootstrap-dark.min.css'
: 'css/bootstrap-light.min.css';
// Reflect theme in data attribute if you use it elsewhere
document.documentElement.setAttribute('data-bs-theme', theme);
// Persist user choice
localStorage.setItem('theme', theme);
});
})();
</script>
</body>
</html>
Key points:
- Two separate CSS files are referenced; the switcher changes the
href of a single <link id="themeStylesheet">.
- The self-invoking script in the
<head> reads localStorage synchronously and appends the correct stylesheet before the page paints, preventing a flash of unstyled content.
- The selected theme is saved to
localStorage, so user preference persists across page loads.