Open In App

Bootstrap 5 Dark Mode

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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: Implementation to show dark mode in Bootstrap.

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=
          rel="stylesheet"
          integrity=
"sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
          crossorigin="anonymous">
</head>
 
<body>
    <div class="container text-center">
        <div class="mt-1">
            <h2 class="text-success">
                GeeksforGeeks
            </h2>
            <h2>
                Component with dark theme
            </h2>
        </div>
    </div>
    <script src=
            integrity=
"sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
               crossorigin="anonymous">
      </script>
</body>
 
</html>


Output:

Screenshot-from-2024-02-16-06-35-00

Global Color Mode Toggle

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: Implementation to show dark mode in Bootstrap using component level color mode.

HTML




<!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=
          rel="stylesheet"
          integrity=
"sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
          crossorigin="anonymous">
    <script src=
            integrity=
"sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
            crossorigin="anonymous">
      </script>
</head>
 
<body>
    <div class="container text-center">
        <div class="mt-1">
            <h2 class="text-success">
                GeeksforGeeks
            </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:

loi

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: Implementation to set custom theme mode in Bootstrap.

HTML




<!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=
          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">
                GeeksforGeeks
            </h2>
            <h2>It's custom theme!</h2>
        </div>
    </div>
    <script src=
            integrity=
"sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
            crossorigin="anonymous">
      </script>
</body>
 
</html>


Output:

Screenshot-2024-02-22-121422



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads