How to use Component-Level Color Mode In Bootstrap
For component-level color mode control, add the data-bs-theme attribute to specific elements. This allows you to apply different color modes to different parts of your website.
Example: In this example we demonstrates Bootstrap Dark Mode by setting data-bs-theme=”dark” for specific components. It offers a dropdown menu to switch between dark and light themes, enhancing user experience.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Bootstrap Dark Mode</title>
<link href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity=
"sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous">
<script src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous">
</script>
</head>
<body>
<div class="container text-center">
<div class="mt-1">
<h2 class="text-success">
w3wiki
</h2>
<div class="container d-flex
justify-content-around align-items-center">
<!-- By using data-bs-theme="dark" inside
div it will be component specific -->
<div data-bs-theme="dark">
<button class="btn btn-secondary dropdown-toggle"
type="button" id="dropdownMenuButtonLight"
data-bs-toggle="dropdown"
aria-expanded="false">
Dark Theme
</button>
<ul class="dropdown-menu"
aria-labelledby="dropdownMenuButtonLight">
<li>
<a class="dropdown-item active" href="#">HTML</a>
</li>
<li>
<a class="dropdown-item" href="#">CSS</a>
</li>
</ul>
</div>
<div data-bs-theme="light">
<button class="btn btn-secondary dropdown-toggle"
type="button"
id="dropdownMenuButtonLight"
data-bs-toggle="dropdown"
aria-expanded="false">
Light Theme
</button>
<ul class="dropdown-menu"
aria-labelledby="dropdownMenuButtonLight">
<li>
<a class="dropdown-item active"
href="#">HTML</a>
</li>
<li>
<a class="dropdown-item"
href="#">CSS</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</body>
</html>
Output:
Bootstrap Dark Mode
Bootstrap Dark Mode is a feature that allows websites or web applications built with Bootstrap to switch to a dark color scheme, enhancing readability in low-light environments and providing a modern aesthetic option for users.
To implement Dark Mode in Bootstrap 5, utilize its built-in dark mode utility classes like ‘bg-dark’, ‘text-white’, and ‘bg-dark’ for backgrounds, text, and borders, respectively.
We have the following approaches to implement the dark mode in Bootstrap 5:
Table of Content
- Using Global Color Mode Toggle
- Using Component-Level Color Mode
- Using Media Queries
Contact Us