How to use Global Color Mode Toggle In Bootstrap

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.

HTML
<!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:

Bootstrap Dark Mode Example 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

Similar Reads

Using Global Color Mode Toggle

To enable dark mode globally, simply add data-bs-theme=”dark” to the element. This attribute will apply the dark color mode to all components and elements unless overridden by specific data-bs-theme attributes....

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....

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 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....

Contact Us