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
Using Global Color Mode Toggle
To enable dark mode globally, simply add data-bs-theme=”dark” to the <html> element. This attribute will apply the dark color mode to all components and elements unless overridden by specific data-bs-theme attributes.
Example: In this example we enables Bootstrap Dark Mode by setting the data-bs-theme=”dark” attribute in the <html> tag, providing a dark color scheme for the Bootstrap-based webpage.
<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">
<head>
<title>Bootstrap Dark Mode</title>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<link href=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity=
"sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous">
</head>
<body>
<div class="container text-center">
<div class="mt-1">
<h2 class="text-success">
w3wiki
</h2>
<h2>
Component with dark theme
</h2>
</div>
</div>
<script src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous">
</script>
</body>
</html>
Output:
Using Component-Level Color Mode
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:
Using Media Queries
To implement a custom theme in Bootstrap 5, set the data-bs-theme attribute to a custom value, such as `”gfg”`, on the <html> element. Then, define your custom theme using CSS variables within a style block, overriding Bootstrap’s default variables. This allows you to tailor the appearance of elements according to your desired color scheme and aesthetics.
Example: In this example we demonstrates Bootstrap custom theme by setting data-bs-theme=”gfg”, altering text color to black and background to dark green, providing a unique visual experience.
<!DOCTYPE html>
<!-- Custom theme -->
<html lang="en"
data-bs-theme="gfg">
<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">
<style>
[data-bs-theme="gfg"] {
--bs-body-color: black; /* Change text color to black */
--bs-body-bg: rgba(34, 36, 35, 0.7); /* Change background color to dark green */
}
</style>
</head>
<body>
<div class="container text-center">
<div class="mt-1">
<h2 class="text-success">
w3wiki
</h2>
<h2>It's custom theme!</h2>
</div>
</div>
<script src=
"https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
integrity=
"sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous">
</script>
</body>
</html>
Output:
Contact Us