Added in v5.3

Color modes

Bootstrap now supports color modes, or themes, as of v5.3.0. Explore our default light color mode and the new dark mode, or create your own using our styles as your template.

Try it yourself! Download the source code and working demo for using Bootstrap with Stylelint, and the color modes from the twbs/examples repository. You can also open the example in StackBlitz.

Dark mode

Bootstrap now supports color modes, starting with dark mode! With v5.3.0 you can implement your own color mode toggler (see below for an example from Bootstrap’s docs) and apply the different color modes as you see fit. We support a light mode (default) and now dark mode. Color modes can be toggled globally on the <html> element, or on specific components and elements, thanks to the data-bs-theme attribute.

Alternatively, you can also switch to a media query implementation thanks to our color mode mixin—see the usage section for details. Heads up though—this eliminates your ability to change themes on a per-component basis as shown below.

Example

For example, to change the color mode of a dropdown menu, add data-bs-theme="light" or data-bs-theme="dark" to the parent .dropdown. Now, no matter the global color mode, these dropdowns will display with the specified theme value.

Color modes - 图1

html

  1. <div class="dropdown" data-bs-theme="light">
  2. <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButtonLight" data-bs-toggle="dropdown" aria-expanded="false">
  3. Default dropdown
  4. </button>
  5. <ul class="dropdown-menu" aria-labelledby="dropdownMenuButtonLight">
  6. <li><a class="dropdown-item active" href="#">Action</a></li>
  7. <li><a class="dropdown-item" href="#">Action</a></li>
  8. <li><a class="dropdown-item" href="#">Another action</a></li>
  9. <li><a class="dropdown-item" href="#">Something else here</a></li>
  10. <li><hr class="dropdown-divider"></li>
  11. <li><a class="dropdown-item" href="#">Separated link</a></li>
  12. </ul>
  13. </div>
  14. <div class="dropdown" data-bs-theme="dark">
  15. <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButtonDark" data-bs-toggle="dropdown" aria-expanded="false">
  16. Dark dropdown
  17. </button>
  18. <ul class="dropdown-menu" aria-labelledby="dropdownMenuButtonDark">
  19. <li><a class="dropdown-item active" href="#">Action</a></li>
  20. <li><a class="dropdown-item" href="#">Action</a></li>
  21. <li><a class="dropdown-item" href="#">Another action</a></li>
  22. <li><a class="dropdown-item" href="#">Something else here</a></li>
  23. <li><hr class="dropdown-divider"></li>
  24. <li><a class="dropdown-item" href="#">Separated link</a></li>
  25. </ul>
  26. </div>

How it works

  • As shown above, color mode styles are controlled by the data-bs-theme attribute. This attribute can be applied to the <html> element, or to any other element or Bootstrap component. If applied to the <html> element, it will apply to everything. If applied to a component or element, it will be scoped to that specific component or element.

  • For each color mode you wish to support, you’ll need to add new overrides for the shared global CSS variables. We do this already in our _root.scss stylesheet for dark mode, with light mode being the default values. In writing color mode specific styles, use the mixin:

    1. // Color mode variables in _root.scss
    2. @include color-mode(dark) {
    3. // CSS variable overrides here...
    4. }
  • We use a custom _variables-dark.scss to power those shared global CSS variable overrides for dark mode. This file isn’t required for your own custom color modes, but it’s required for our dark mode for two reasons. First, it’s better to have a single place to reset global colors. Second, some Sass variables had to be overridden for background images embedded in our CSS for accordions, form components, and more.

Usage

Enable dark mode

Enable the built in dark color mode across your entire project by adding the data-bs-theme="dark" attribute to the <html> element. This will apply the dark color mode to all components and elements, other than those with a specific data-bs-theme attribute applied. Building on the quick start template:

  1. <!doctype html>
  2. <html lang="en" data-bs-theme="dark">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Bootstrap demo</title>
  7. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
  8. </head>
  9. <body>
  10. <h1>Hello, world!</h1>
  11. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
  12. </body>
  13. </html>

Bootstrap does not yet ship with a built-in color mode picker, but you can use the one from our own documentation if you like. Learn more in the JavaScript section.

Building with Sass

Our new dark mode option is available to use for all users of Bootstrap, but it’s controlled via data attributes instead of media queries and does not automatically toggle your project’s color mode. You can disable our dark mode entirely via Sass by changing $enable-dark-mode to false.

We use a custom Sass mixin, color-mode(), to help you control how color modes are applied. By default, we use a data attribute approach, allowing you to create more user-friendly experiences where your visitors can choose to have an automatic dark mode or control their preference (like in our own docs here). This is also an easy and scalable way to add different themes and more custom color modes beyond light and dark.

In case you want to use media queries and only make color modes automatic, you can change the mixin’s default type via Sass variable. Consider the following snippet and its compiled CSS output.

  1. $color-mode-type: data;
  2. @include color-mode(dark) {
  3. .element {
  4. color: var(--bs-primary-text-emphasis);
  5. background-color: var(--bs-primary-bg-subtle);
  6. }
  7. }

Outputs to:

  1. [data-bs-theme=dark] .element {
  2. color: var(--bs-primary-text-emphasis);
  3. background-color: var(--bs-primary-bg-subtle);
  4. }

And when setting to media-query:

  1. $color-mode-type: media-query;
  2. @include color-mode(dark) {
  3. .element {
  4. color: var(--bs-primary-text-emphasis);
  5. background-color: var(--bs-primary-bg-subtle);
  6. }
  7. }

Outputs to:

  1. @media (prefers-color-scheme: dark) {
  2. .element {
  3. color: var(--bs-primary-text-emphasis);
  4. background-color: var(--bs-primary-bg-subtle);
  5. }
  6. }

Custom color modes

While the primary use case for color modes is light and dark mode, custom color modes are also possible. Create your own data-bs-theme selector with a custom value as the name of your color mode, then modify our Sass and CSS variables as needed. We opted to create a separate _variables-dark.scss stylesheet to house Bootstrap’s dark mode specific Sass variables, but that’s not required for you.

For example, you can create a “blue theme” with the selector data-bs-theme="blue". In your custom Sass or CSS file, add the new selector and override any global or component CSS variables as needed. If you’re using Sass, you can also use Sass’s functions within your CSS variable overrides.

site/assets/scss/_content.scss

  1. [data-bs-theme="blue"] {
  2. --bs-body-color: var(--bs-white);
  3. --bs-body-color-rgb: #{to-rgb($white)};
  4. --bs-body-bg: var(--bs-blue);
  5. --bs-body-bg-rgb: #{to-rgb($blue)};
  6. --bs-tertiary-bg: #{$blue-600};
  7. .dropdown-menu {
  8. --bs-dropdown-bg: #{mix($blue-500, $blue-600)};
  9. --bs-dropdown-link-active-bg: #{$blue-700};
  10. }
  11. .btn-secondary {
  12. --bs-btn-bg: #{mix($gray-600, $blue-400, .5)};
  13. --bs-btn-border-color: #{rgba($white, .25)};
  14. --bs-btn-hover-bg: #{darken(mix($gray-600, $blue-400, .5), 5%)};
  15. --bs-btn-hover-border-color: #{rgba($white, .25)};
  16. --bs-btn-active-bg: #{darken(mix($gray-600, $blue-400, .5), 10%)};
  17. --bs-btn-active-border-color: #{rgba($white, .5)};
  18. --bs-btn-focus-border-color: #{rgba($white, .5)};
  19. --bs-btn-focus-box-shadow: 0 0 0 .25rem rgba(255, 255, 255, .2);
  20. }
  21. }

Color modes - 图2

  1. <div data-bs-theme="blue">
  2. ...
  3. </div>

JavaScript

To allow visitors or users to toggle color modes, you’ll need to create a toggle element to control the data-bs-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 look at the JavaScript that powers it. Feel free to inspect our own documentation navbar to 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. Note that if you decide to use media queries for your color modes, your JavaScript may need to be modified or removed if you prefer an implicit control.

  1. /*!
  2. * Color mode toggler for Bootstrap's docs (https://getbootstrap.com/)
  3. * Copyright 2011-2024 The Bootstrap Authors
  4. * Licensed under the Creative Commons Attribution 3.0 Unported License.
  5. */
  6. (() => {
  7. 'use strict'
  8. const getStoredTheme = () => localStorage.getItem('theme')
  9. const setStoredTheme = theme => localStorage.setItem('theme', theme)
  10. const getPreferredTheme = () => {
  11. const storedTheme = getStoredTheme()
  12. if (storedTheme) {
  13. return storedTheme
  14. }
  15. return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
  16. }
  17. const setTheme = theme => {
  18. if (theme === 'auto') {
  19. document.documentElement.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'))
  20. } else {
  21. document.documentElement.setAttribute('data-bs-theme', theme)
  22. }
  23. }
  24. setTheme(getPreferredTheme())
  25. const showActiveTheme = (theme, focus = false) => {
  26. const themeSwitcher = document.querySelector('#bd-theme')
  27. if (!themeSwitcher) {
  28. return
  29. }
  30. const themeSwitcherText = document.querySelector('#bd-theme-text')
  31. const activeThemeIcon = document.querySelector('.theme-icon-active use')
  32. const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`)
  33. const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href')
  34. document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
  35. element.classList.remove('active')
  36. element.setAttribute('aria-pressed', 'false')
  37. })
  38. btnToActive.classList.add('active')
  39. btnToActive.setAttribute('aria-pressed', 'true')
  40. activeThemeIcon.setAttribute('href', svgOfActiveBtn)
  41. const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})`
  42. themeSwitcher.setAttribute('aria-label', themeSwitcherLabel)
  43. if (focus) {
  44. themeSwitcher.focus()
  45. }
  46. }
  47. window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
  48. const storedTheme = getStoredTheme()
  49. if (storedTheme !== 'light' && storedTheme !== 'dark') {
  50. setTheme(getPreferredTheme())
  51. }
  52. })
  53. window.addEventListener('DOMContentLoaded', () => {
  54. showActiveTheme(getPreferredTheme())
  55. document.querySelectorAll('[data-bs-theme-value]')
  56. .forEach(toggle => {
  57. toggle.addEventListener('click', () => {
  58. const theme = toggle.getAttribute('data-bs-theme-value')
  59. setStoredTheme(theme)
  60. setTheme(theme)
  61. showActiveTheme(theme, true)
  62. })
  63. })
  64. })
  65. })()

Adding theme colors

Adding a new color in $theme-colors is not enough for some of our components like alerts and list groups. New colors must also be defined in $theme-colors-text, $theme-colors-bg-subtle, and $theme-colors-border-subtle for light theme; but also in $theme-colors-text-dark, $theme-colors-bg-subtle-dark, and $theme-colors-border-subtle-dark for dark theme.

This is a manual process because Sass cannot generate its own Sass variables from an existing variable or map. In future versions of Bootstrap, we’ll revisit this setup to reduce the duplication.

  1. // Required
  2. @import "functions";
  3. @import "variables";
  4. @import "variables-dark";
  5. // Add a custom color to $theme-colors
  6. $custom-colors: (
  7. "custom-color": #712cf9
  8. );
  9. $theme-colors: map-merge($theme-colors, $custom-colors);
  10. @import "maps";
  11. @import "mixins";
  12. @import "utilities";
  13. // Add a custom color to new theme maps
  14. // Light mode
  15. $custom-colors-text: ("custom-color": #712cf9);
  16. $custom-colors-bg-subtle: ("custom-color": #e1d2fe);
  17. $custom-colors-border-subtle: ("custom-color": #bfa1fc);
  18. $theme-colors-text: map-merge($theme-colors-text, $custom-colors-text);
  19. $theme-colors-bg-subtle: map-merge($theme-colors-bg-subtle, $custom-colors-bg-subtle);
  20. $theme-colors-border-subtle: map-merge($theme-colors-border-subtle, $custom-colors-border-subtle);
  21. // Dark mode
  22. $custom-colors-text-dark: ("custom-color": #e1d2f2);
  23. $custom-colors-bg-subtle-dark: ("custom-color": #8951fa);
  24. $custom-colors-border-subtle-dark: ("custom-color": #e1d2f2);
  25. $theme-colors-text-dark: map-merge($theme-colors-text-dark, $custom-colors-text-dark);
  26. $theme-colors-bg-subtle-dark: map-merge($theme-colors-bg-subtle-dark, $custom-colors-bg-subtle-dark);
  27. $theme-colors-border-subtle-dark: map-merge($theme-colors-border-subtle-dark, $custom-colors-border-subtle-dark);
  28. // Remainder of Bootstrap imports
  29. @import "root";
  30. @import "reboot";
  31. // etc

CSS

Variables

Dozens of root level CSS variables are repeated as overrides for dark mode. These are scoped to the color mode selector, which defaults to data-bs-theme but can be configured to use a prefers-color-scheme media query. Use these variables as a guideline for generating your own new color modes.

scss/_root.scss

  1. --#{$prefix}body-color: #{$body-color-dark};
  2. --#{$prefix}body-color-rgb: #{to-rgb($body-color-dark)};
  3. --#{$prefix}body-bg: #{$body-bg-dark};
  4. --#{$prefix}body-bg-rgb: #{to-rgb($body-bg-dark)};
  5. --#{$prefix}emphasis-color: #{$body-emphasis-color-dark};
  6. --#{$prefix}emphasis-color-rgb: #{to-rgb($body-emphasis-color-dark)};
  7. --#{$prefix}secondary-color: #{$body-secondary-color-dark};
  8. --#{$prefix}secondary-color-rgb: #{to-rgb($body-secondary-color-dark)};
  9. --#{$prefix}secondary-bg: #{$body-secondary-bg-dark};
  10. --#{$prefix}secondary-bg-rgb: #{to-rgb($body-secondary-bg-dark)};
  11. --#{$prefix}tertiary-color: #{$body-tertiary-color-dark};
  12. --#{$prefix}tertiary-color-rgb: #{to-rgb($body-tertiary-color-dark)};
  13. --#{$prefix}tertiary-bg: #{$body-tertiary-bg-dark};
  14. --#{$prefix}tertiary-bg-rgb: #{to-rgb($body-tertiary-bg-dark)};
  15. @each $color, $value in $theme-colors-text-dark {
  16. --#{$prefix}#{$color}-text-emphasis: #{$value};
  17. }
  18. @each $color, $value in $theme-colors-bg-subtle-dark {
  19. --#{$prefix}#{$color}-bg-subtle: #{$value};
  20. }
  21. @each $color, $value in $theme-colors-border-subtle-dark {
  22. --#{$prefix}#{$color}-border-subtle: #{$value};
  23. }
  24. --#{$prefix}heading-color: #{$headings-color-dark};
  25. --#{$prefix}link-color: #{$link-color-dark};
  26. --#{$prefix}link-hover-color: #{$link-hover-color-dark};
  27. --#{$prefix}link-color-rgb: #{to-rgb($link-color-dark)};
  28. --#{$prefix}link-hover-color-rgb: #{to-rgb($link-hover-color-dark)};
  29. --#{$prefix}code-color: #{$code-color-dark};
  30. --#{$prefix}highlight-color: #{$mark-color-dark};
  31. --#{$prefix}highlight-bg: #{$mark-bg-dark};
  32. --#{$prefix}border-color: #{$border-color-dark};
  33. --#{$prefix}border-color-translucent: #{$border-color-translucent-dark};
  34. --#{$prefix}form-valid-color: #{$form-valid-color-dark};
  35. --#{$prefix}form-valid-border-color: #{$form-valid-border-color-dark};
  36. --#{$prefix}form-invalid-color: #{$form-invalid-color-dark};
  37. --#{$prefix}form-invalid-border-color: #{$form-invalid-border-color-dark};

Sass variables

CSS variables for our dark color mode are partially generated from dark mode specific Sass variables in _variables-dark.scss. This also includes some custom overrides for changing the colors of embedded SVGs used throughout our components.

scss/_variables-dark.scss

  1. $primary-text-emphasis-dark: tint-color($primary, 40%);
  2. $secondary-text-emphasis-dark: tint-color($secondary, 40%);
  3. $success-text-emphasis-dark: tint-color($success, 40%);
  4. $info-text-emphasis-dark: tint-color($info, 40%);
  5. $warning-text-emphasis-dark: tint-color($warning, 40%);
  6. $danger-text-emphasis-dark: tint-color($danger, 40%);
  7. $light-text-emphasis-dark: $gray-100;
  8. $dark-text-emphasis-dark: $gray-300;
  9. $primary-bg-subtle-dark: shade-color($primary, 80%);
  10. $secondary-bg-subtle-dark: shade-color($secondary, 80%);
  11. $success-bg-subtle-dark: shade-color($success, 80%);
  12. $info-bg-subtle-dark: shade-color($info, 80%);
  13. $warning-bg-subtle-dark: shade-color($warning, 80%);
  14. $danger-bg-subtle-dark: shade-color($danger, 80%);
  15. $light-bg-subtle-dark: $gray-800;
  16. $dark-bg-subtle-dark: mix($gray-800, $black);
  17. $primary-border-subtle-dark: shade-color($primary, 40%);
  18. $secondary-border-subtle-dark: shade-color($secondary, 40%);
  19. $success-border-subtle-dark: shade-color($success, 40%);
  20. $info-border-subtle-dark: shade-color($info, 40%);
  21. $warning-border-subtle-dark: shade-color($warning, 40%);
  22. $danger-border-subtle-dark: shade-color($danger, 40%);
  23. $light-border-subtle-dark: $gray-700;
  24. $dark-border-subtle-dark: $gray-800;
  25. $body-color-dark: $gray-300;
  26. $body-bg-dark: $gray-900;
  27. $body-secondary-color-dark: rgba($body-color-dark, .75);
  28. $body-secondary-bg-dark: $gray-800;
  29. $body-tertiary-color-dark: rgba($body-color-dark, .5);
  30. $body-tertiary-bg-dark: mix($gray-800, $gray-900, 50%);
  31. $body-emphasis-color-dark: $white;
  32. $border-color-dark: $gray-700;
  33. $border-color-translucent-dark: rgba($white, .15);
  34. $headings-color-dark: inherit;
  35. $link-color-dark: tint-color($primary, 40%);
  36. $link-hover-color-dark: shift-color($link-color-dark, -$link-shade-percentage);
  37. $code-color-dark: tint-color($code-color, 40%);
  38. $mark-color-dark: $body-color-dark;
  39. $mark-bg-dark: $yellow-800;
  40. //
  41. // Forms
  42. //
  43. $form-select-indicator-color-dark: $body-color-dark;
  44. $form-select-indicator-dark: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path fill='none' stroke='#{$form-select-indicator-color-dark}' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='m2 5 6 6 6-6'/></svg>");
  45. $form-switch-color-dark: rgba($white, .25);
  46. $form-switch-bg-image-dark: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'><circle r='3' fill='#{$form-switch-color-dark}'/></svg>");
  47. $form-valid-color-dark: $green-300;
  48. $form-valid-border-color-dark: $green-300;
  49. $form-invalid-color-dark: $red-300;
  50. $form-invalid-border-color-dark: $red-300;
  51. //
  52. // Accordion
  53. //
  54. $accordion-icon-color-dark: $primary-text-emphasis-dark;
  55. $accordion-icon-active-color-dark: $primary-text-emphasis-dark;
  56. $accordion-button-icon-dark: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='#{$accordion-icon-color-dark}'><path fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/></svg>");
  57. $accordion-button-active-icon-dark: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='#{$accordion-icon-active-color-dark}'><path fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/></svg>");

Sass mixins

Styles for dark mode, and any custom color modes you create, can be scoped appropriately to the data-bs-theme attribute selector or media query with the customizable color-mode() mixin. See the Sass usage section for more details.

scss/mixins/_color-mode.scss

  1. @mixin color-mode($mode: light, $root: false) {
  2. @if $color-mode-type == "media-query" {
  3. @if $root == true {
  4. @media (prefers-color-scheme: $mode) {
  5. :root {
  6. @content;
  7. }
  8. }
  9. } @else {
  10. @media (prefers-color-scheme: $mode) {
  11. @content;
  12. }
  13. }
  14. } @else {
  15. [data-bs-theme="#{$mode}"] {
  16. @content;
  17. }
  18. }
  19. }