Bootstrap Dark Mode Toggle

Bootstrap 5 Dark Mode Toggle

Responsive Dark Mode theme Toggle built with Bootstrap 5 - toggle button that switches between dark and light themes.


Toggle example

To allow visitors or users to toggle color modes, you’ll need to create a toggle element to control the data-mdb-theme attribute on the root element, <html>. We’ve built a toggler in our documentation that initially defers to a user’s current system color mode, but provides an option to override that and pick a specific color mode.

Here’s a simplified version of code we used in own documentation navbar. You can see how it’s implemented using HTML and CSS from our own components. It is suggested to include the JavaScript at the top of your page to reduce potential screen flickering during reloading of your site.

If you want to see a working example of this switcher, preview it in the upper right corner.

        
            
          <li class="nav-item align-items-center d-flex" >
            <i class="fas fa-sun"></i>
            <!-- Default switch -->
            <div class="ms-2 form-check form-switch">
              <input class="form-check-input" type="checkbox" role="switch" id="themingSwitcher" />
            </div>
            <i class="fas fa-moon"></i>
          </li>
        
        
    

Place the code from the snippet below in JS file that you will link on every page at the end of a <body> tag. This code will check your system default settings and set theme based on that.

        
            
        const themeStitcher = document.getElementById("themingSwitcher");
        const isSystemThemeSetToDark = window.matchMedia("(prefers-color-scheme: dark)").matches;

        // set toggler position based on system theme
        if (isSystemThemeSetToDark) {
          themeStitcher.checked = true;
        }

        // add listener to theme toggler
        themeStitcher.addEventListener("change", (e) => {
          toggleTheme(e.target.checked);
        });

        const toggleTheme = (isChecked) => {
          const theme = isChecked ? "dark" : "light";

          document.documentElement.dataset.mdbTheme = theme;
        }

        // add listener to toggle theme with Shift + D
        document.addEventListener("keydown", (e) => {
          if (e.shiftKey && e.key === "D") {
            themeStitcher.checked = !themeStitcher.checked;
            toggleTheme(themeStitcher.checked);
          }
        });
      
        
    

The last part is added in the head of an head of a <html> page. It prevents flickering of the colors by setting the saved theme before the whole page is loaded.

        
            
      <script>
        const isSystemThemeSetToDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
  
        if (isSystemThemeSetToDark) {
          document.documentElement.dataset.mdbTheme = "dark";
        };
  
      </script>

      
        
    

Examples of light and dark mode components

You can change the mode to dark only for the specific component. For example, to change the color mode of a card, add data-mdb-theme="light" or data-mdb-theme="dark" to the div with .card class. Now, no matter the global color mode, these cards will display with the specified theme value.

Card title

Some quick example text to build on the card title and make up the bulk of the card's content.

Card title

Some quick example text to build on the card title and make up the bulk of the card's content.

        
            
    
                <div class="row gx-lg-5">
                
                  <div class="col-lg-6 mb-5 mb-lg-0">
            
                    <div class="mb-4">
                      <div class="card" data-mdb-theme="light">
                        <div class="card-body">
                          <h5 class="card-title">Card title</h5>
                          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                          <button type="button" class="btn btn-primary" data-mdb-ripple-init>Button</button>
                        </div>
                      </div>
                    </div>
            
                    <div class="mb-4">
                      <!-- Navbar -->
            <nav class="navbar navbar-expand-lg navbar-light bg-body-tertiary" data-mdb-theme="light">
              <!-- Container wrapper -->
              <div class="container-fluid">
                <!-- Toggle button -->
                <button
                  data-mdb-collapse-init
                  class="navbar-toggler"
                  type="button"
                  data-mdb-target="#navbarSupportedContent"
                  aria-controls="navbarSupportedContent"
                  aria-expanded="false"
                  aria-label="Toggle navigation"
                >
                  <i class="fas fa-bars"></i>
                </button>
            
                <!-- Collapsible wrapper -->
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                  <!-- Navbar brand -->
                  <a class="navbar-brand mt-2 mt-lg-0" href="#">
                    <img
                      src="https://mdbcdn.b-cdn.net/img/logo/mdb-transaprent-noshadows.webp"
                      height="15"
                      alt="MDB Logo"
                      loading="lazy"
                    />
                  </a>
                  <!-- Left links -->
                  <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                      <a class="nav-link" href="#">Dashboard</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="#">Team</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="#">Projects</a>
                    </li>
                  </ul>
                  <!-- Left links -->
                </div>
                <!-- Collapsible wrapper -->
            
                <!-- Right elements -->
                <div class="d-flex align-items-center">
                  <!-- Icon -->
                  <a class="text-dark me-3" href="#">
                    <i class="fas fa-shopping-cart"></i>
                  </a>
            
                  <!-- Notifications -->
                  <div class="dropdown">
                    <a
                      data-mdb-dropdown-init
                      class="text-dark me-3 dropdown-toggle hidden-arrow"
                      href="#"
                      id="navbarDropdownMenuLink"
                      role="button"
                      aria-expanded="false"
                    >
                      <i class="fas fa-bell"></i>
                      <span class="badge rounded-pill badge-notification bg-danger">1</span>
                    </a>
                    <ul
                      class="dropdown-menu dropdown-menu-end"
                      aria-labelledby="navbarDropdownMenuLink"
                    >
                      <li>
                        <a class="dropdown-item" href="#">Some news</a>
                      </li>
                      <li>
                        <a class="dropdown-item" href="#">Another news</a>
                      </li>
                      <li>
                        <a class="dropdown-item" href="#">Something else here</a>
                      </li>
                    </ul>
                  </div>
                  <!-- Avatar -->
                  <div class="dropdown">
                    <a
                      data-mdb-dropdown-init
                      class="dropdown-toggle d-flex align-items-center hidden-arrow"
                      href="#"
                      id="navbarDropdownMenuAvatar"
                      role="button"
                      aria-expanded="false"
                    >
                      <img
                        src="https://mdbcdn.b-cdn.net/img/new/avatars/2.webp"
                        class="rounded-circle"
                        height="25"
                        alt="Black and White Portrait of a Man"
                        loading="lazy"
                      />
                    </a>
                    <ul
                      class="dropdown-menu dropdown-menu-end"
                      aria-labelledby="navbarDropdownMenuAvatar"
                    >
                      <li>
                        <a class="dropdown-item" href="#">My profile</a>
                      </li>
                      <li>
                        <a class="dropdown-item" href="#">Settings</a>
                      </li>
                      <li>
                        <a class="dropdown-item" href="#">Logout</a>
                      </li>
                    </ul>
                  </div>
                </div>
                <!-- Right elements -->
              </div>
              <!-- Container wrapper -->
            </nav>
            <!-- Navbar -->
                    </div>
                    <div class="mb-4">
                      <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary" data-mdb-ripple-init data-mdb-modal-init data-mdb-target="#exampleModal">
              Launch demo modal
            </button>
            
            <!-- Modal -->
            <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" data-mdb-theme="light">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header" data-mdb-theme="dark">
                    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                    <button type="button" class="btn-close" data-mdb-ripple-init data-mdb-dismiss="modal" aria-label="Close"></button>
                  </div>
                  <div class="modal-body">...</div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-mdb-ripple-init data-mdb-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" data-mdb-ripple-init>Save changes</button>
                  </div>
                </div>
              </div>
            </div>
                    </div>
                    <div class="bg-white p-2" data-mdb-theme="light">
                      <div class="form-outline" data-mdb-datepicker-init data-mdb-input-init>
                        <input type="text" class="form-control" id="exampleDatepicker11" />
                        <label for="exampleDatepicker11" class="form-label">Select a date</label>
                      </div>
                    </div>
                
                  </div>
                
                  <div class="col-lg-6 mb-5 mb-lg-0">
            
                    <div class="mb-4">
                      <div class="card" data-mdb-theme="dark">
                        <div class="card-body">
                          <h5 class="card-title">Card title</h5>
                          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                          <button type="button" class="btn btn-primary" data-mdb-ripple-init>Button</button>
                        </div>
                      </div>
                    </div>
            
                    <div class="mb-4">
                      <!-- Navbar -->
            <nav class="navbar navbar-expand-lg navbar-light bg-body-tertiary" data-mdb-theme="dark">
              <!-- Container wrapper -->
              <div class="container-fluid">
                <!-- Toggle button -->
                <button
                  data-mdb-collapse-init
                  class="navbar-toggler"
                  type="button"
                  data-mdb-target="#navbarSupportedContent"
                  aria-controls="navbarSupportedContent"
                  aria-expanded="false"
                  aria-label="Toggle navigation"
                >
                  <i class="fas fa-bars"></i>
                </button>
            
                <!-- Collapsible wrapper -->
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                  <!-- Navbar brand -->
                  <a class="navbar-brand mt-2 mt-lg-0" href="#">
                    <img
                      src="https://mdbcdn.b-cdn.net/img/logo/mdb-transaprent-noshadows.webp"
                      height="15"
                      alt="MDB Logo"
                      loading="lazy"
                    />
                  </a>
                  <!-- Left links -->
                  <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                      <a class="nav-link" href="#">Dashboard</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="#">Team</a>
                    </li>
                    <li class="nav-item">
                      <a class="nav-link" href="#">Projects</a>
                    </li>
                  </ul>
                  <!-- Left links -->
                </div>
                <!-- Collapsible wrapper -->
            
                <!-- Right elements -->
                <div class="d-flex align-items-center">
                  <!-- Icon -->
                  <a class="text-white me-3" href="#">
                    <i class="fas fa-shopping-cart"></i>
                  </a>
            
                  <!-- Notifications -->
                  <div class="dropdown">
                    <a
                      data-mdb-dropdown-init
                      class="text-white me-3 dropdown-toggle hidden-arrow"
                      href="#"
                      id="navbarDropdownMenuLink"
                      role="button"
                      aria-expanded="false"
                    >
                      <i class="fas fa-bell"></i>
                      <span class="badge rounded-pill badge-notification bg-danger">1</span>
                    </a>
                    <ul
                      class="dropdown-menu dropdown-menu-end"
                      aria-labelledby="navbarDropdownMenuLink"
                    >
                      <li>
                        <a class="dropdown-item" href="#">Some news</a>
                      </li>
                      <li>
                        <a class="dropdown-item" href="#">Another news</a>
                      </li>
                      <li>
                        <a class="dropdown-item" href="#">Something else here</a>
                      </li>
                    </ul>
                  </div>
                  <!-- Avatar -->
                  <div class="dropdown">
                    <a
                      data-mdb-dropdown-init
                      class="dropdown-toggle d-flex align-items-center hidden-arrow"
                      href="#"
                      id="navbarDropdownMenuAvatar"
                      role="button"
                      aria-expanded="false"
                    >
                      <img
                        src="https://mdbcdn.b-cdn.net/img/new/avatars/2.webp"
                        class="rounded-circle"
                        height="25"
                        alt="Black and White Portrait of a Man"
                        loading="lazy"
                      />
                    </a>
                    <ul
                      class="dropdown-menu dropdown-menu-end"
                      aria-labelledby="navbarDropdownMenuAvatar"
                    >
                      <li>
                        <a class="dropdown-item" href="#">My profile</a>
                      </li>
                      <li>
                        <a class="dropdown-item" href="#">Settings</a>
                      </li>
                      <li>
                        <a class="dropdown-item" href="#">Logout</a>
                      </li>
                    </ul>
                  </div>
                </div>
                <!-- Right elements -->
              </div>
              <!-- Container wrapper -->
            </nav>
            <!-- Navbar -->
                    </div>
                    <div class="mb-4">
                      <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary" data-mdb-ripple-init data-mdb-modal-init data-mdb-target="#exampleModalDark">
              Launch demo modal
            </button>
            
            <!-- Modal -->
            <div class="modal fade" id="exampleModalDark" tabindex="-1" aria-labelledby="exampleModalLabelDark" aria-hidden="true" data-mdb-theme="dark">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabelDark">Modal title</h5>
                    <button type="button" class="btn-close" data-mdb-ripple-init data-mdb-dismiss="modal" aria-label="Close"></button>
                  </div>
                  <div class="modal-body">...</div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-mdb-ripple-init data-mdb-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" data-mdb-ripple-init>Save changes</button>
                  </div>
                </div>
              </div>
            </div>
                    </div>
                    <div class="p-2" style="background-color: #303030;" data-mdb-theme="dark">
                      <div class="form-outline" data-mdb-datepicker-init data-mdb-input-init>
                        <input type="text" class="form-control" id="exampleDatepicker2" />
                        <label for="exampleDatepicker11" class="form-label">Select a date</label>
                      </div>
                    </div>
                    
                  </div>
                
                </div>