Colors

Convey meaning through color with a handful of color utility classes. Includes support for styling links with hover states, too.

Accessibility tip: Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies like screen readers. Please ensure the meaning is obvious from the content itself (e.g., the visible text with a sufficient color contrast) or is included through alternative means, such as additional text hidden with the .visually-hidden class.

Colors

Colorize text with color utilities. If you want to colorize links, you can use the .link-* helper classes which have :hover and :focus states.

Color utilities like .text-* that generated from our original $theme-colors Sass map don’t yet respond to color modes, however, any .text-*-emphasis utility will. This will be resolved in v6.

Colors - 图1

html

  1. <p class="text-primary">.text-primary</p>
  2. <p class="text-primary-emphasis">.text-primary-emphasis</p>
  3. <p class="text-secondary">.text-secondary</p>
  4. <p class="text-secondary-emphasis">.text-secondary-emphasis</p>
  5. <p class="text-success">.text-success</p>
  6. <p class="text-success-emphasis">.text-success-emphasis</p>
  7. <p class="text-danger">.text-danger</p>
  8. <p class="text-danger-emphasis">.text-danger-emphasis</p>
  9. <p class="text-warning bg-dark">.text-warning</p>
  10. <p class="text-warning-emphasis">.text-warning-emphasis</p>
  11. <p class="text-info bg-dark">.text-info</p>
  12. <p class="text-info-emphasis">.text-info-emphasis</p>
  13. <p class="text-light bg-dark">.text-light</p>
  14. <p class="text-light-emphasis">.text-light-emphasis</p>
  15. <p class="text-dark bg-white">.text-dark</p>
  16. <p class="text-dark-emphasis">.text-dark-emphasis</p>
  17. <p class="text-body">.text-body</p>
  18. <p class="text-body-emphasis">.text-body-emphasis</p>
  19. <p class="text-body-secondary">.text-body-secondary</p>
  20. <p class="text-body-tertiary">.text-body-tertiary</p>
  21. <p class="text-black bg-white">.text-black</p>
  22. <p class="text-white bg-dark">.text-white</p>
  23. <p class="text-black-50 bg-white">.text-black-50</p>
  24. <p class="text-white-50 bg-dark">.text-white-50</p>

Deprecation: With the addition of .text-opacity-* utilities and CSS variables for text utilities, .text-black-50 and .text-white-50 are deprecated as of v5.1.0. They’ll be removed in v6.0.0.

Deprecation: With the addition of the expanded theme colors and variables, the .text-muted utility has been deprecated as of v5.3.0. Its default value has also been reassigned to the new --bs-secondary-color CSS variable to better support color modes. It will be removed in v6.0.0.

Opacity

Added in v5.1.0

As of v5.1.0, text color utilities are generated with Sass using CSS variables. This allows for real-time color changes without compilation and dynamic alpha transparency changes.

How it works

Consider our default .text-primary utility.

  1. .text-primary {
  2. --bs-text-opacity: 1;
  3. color: rgba(var(--bs-primary-rgb), var(--bs-text-opacity)) !important;
  4. }

We use an RGB version of our --bs-primary (with the value of 13, 110, 253) CSS variable and attached a second CSS variable, --bs-text-opacity, for the alpha transparency (with a default value 1 thanks to a local CSS variable). That means anytime you use .text-primary now, your computed color value is rgba(13, 110, 253, 1). The local CSS variable inside each .text-* class avoids inheritance issues so nested instances of the utilities don’t automatically have a modified alpha transparency.

Example

To change that opacity, override --bs-text-opacity via custom styles or inline styles.

Colors - 图2

html

  1. <div class="text-primary">This is default primary text</div>
  2. <div class="text-primary" style="--bs-text-opacity: .5;">This is 50% opacity primary text</div>

Or, choose from any of the .text-opacity utilities:

Colors - 图3

html

  1. <div class="text-primary">This is default primary text</div>
  2. <div class="text-primary text-opacity-75">This is 75% opacity primary text</div>
  3. <div class="text-primary text-opacity-50">This is 50% opacity primary text</div>
  4. <div class="text-primary text-opacity-25">This is 25% opacity primary text</div>

Specificity

Sometimes contextual classes cannot be applied due to the specificity of another selector. In some cases, a sufficient workaround is to wrap your element’s content in a <div> or more semantic element with the desired class.

CSS

In addition to the following Sass functionality, consider reading about our included CSS custom properties (aka CSS variables) for colors and more.

Sass variables

Most color utilities are generated by our theme colors, reassigned from our generic color palette variables.

scss/_variables.scss

  1. $blue: #0d6efd;
  2. $indigo: #6610f2;
  3. $purple: #6f42c1;
  4. $pink: #d63384;
  5. $red: #dc3545;
  6. $orange: #fd7e14;
  7. $yellow: #ffc107;
  8. $green: #198754;
  9. $teal: #20c997;
  10. $cyan: #0dcaf0;

scss/_variables.scss

  1. $primary: $blue;
  2. $secondary: $gray-600;
  3. $success: $green;
  4. $info: $cyan;
  5. $warning: $yellow;
  6. $danger: $red;
  7. $light: $gray-100;
  8. $dark: $gray-900;

Grayscale colors are also available, but only a subset are used to generate any utilities.

scss/_variables.scss

  1. $white: #fff;
  2. $gray-100: #f8f9fa;
  3. $gray-200: #e9ecef;
  4. $gray-300: #dee2e6;
  5. $gray-400: #ced4da;
  6. $gray-500: #adb5bd;
  7. $gray-600: #6c757d;
  8. $gray-700: #495057;
  9. $gray-800: #343a40;
  10. $gray-900: #212529;
  11. $black: #000;

scss/_maps.scss

  1. $theme-colors-text: (
  2. "primary": $primary-text-emphasis,
  3. "secondary": $secondary-text-emphasis,
  4. "success": $success-text-emphasis,
  5. "info": $info-text-emphasis,
  6. "warning": $warning-text-emphasis,
  7. "danger": $danger-text-emphasis,
  8. "light": $light-text-emphasis,
  9. "dark": $dark-text-emphasis,
  10. );

Variables for setting colors in .text-*-emphasis utilities in light and dark mode:

scss/_variables.scss

  1. $primary-text-emphasis: shade-color($primary, 60%);
  2. $secondary-text-emphasis: shade-color($secondary, 60%);
  3. $success-text-emphasis: shade-color($success, 60%);
  4. $info-text-emphasis: shade-color($info, 60%);
  5. $warning-text-emphasis: shade-color($warning, 60%);
  6. $danger-text-emphasis: shade-color($danger, 60%);
  7. $light-text-emphasis: $gray-700;
  8. $dark-text-emphasis: $gray-700;

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;

Sass maps

Theme colors are then put into a Sass map so we can loop over them to generate our utilities, component modifiers, and more.

scss/_variables.scss

  1. $theme-colors: (
  2. "primary": $primary,
  3. "secondary": $secondary,
  4. "success": $success,
  5. "info": $info,
  6. "warning": $warning,
  7. "danger": $danger,
  8. "light": $light,
  9. "dark": $dark
  10. );

Grayscale colors are also available as a Sass map. This map is not used to generate any utilities.

scss/_variables.scss

  1. $grays: (
  2. "100": $gray-100,
  3. "200": $gray-200,
  4. "300": $gray-300,
  5. "400": $gray-400,
  6. "500": $gray-500,
  7. "600": $gray-600,
  8. "700": $gray-700,
  9. "800": $gray-800,
  10. "900": $gray-900
  11. );

RGB colors are generated from a separate Sass map:

scss/_maps.scss

  1. $theme-colors-rgb: map-loop($theme-colors, to-rgb, "$value");

Color opacities build on that with their own map that’s consumed by the utilities API:

scss/_maps.scss

  1. $utilities-text: map-merge(
  2. $utilities-colors,
  3. (
  4. "black": to-rgb($black),
  5. "white": to-rgb($white),
  6. "body": to-rgb($body-color)
  7. )
  8. );
  9. $utilities-text-colors: map-loop($utilities-text, rgba-css-var, "$key", "text");
  10. $utilities-text-emphasis-colors: (
  11. "primary-emphasis": var(--#{$prefix}primary-text-emphasis),
  12. "secondary-emphasis": var(--#{$prefix}secondary-text-emphasis),
  13. "success-emphasis": var(--#{$prefix}success-text-emphasis),
  14. "info-emphasis": var(--#{$prefix}info-text-emphasis),
  15. "warning-emphasis": var(--#{$prefix}warning-text-emphasis),
  16. "danger-emphasis": var(--#{$prefix}danger-text-emphasis),
  17. "light-emphasis": var(--#{$prefix}light-text-emphasis),
  18. "dark-emphasis": var(--#{$prefix}dark-text-emphasis)
  19. );

Color mode adaptive text colors are also available as a Sass map:

scss/_maps.scss

  1. $theme-colors-text: (
  2. "primary": $primary-text-emphasis,
  3. "secondary": $secondary-text-emphasis,
  4. "success": $success-text-emphasis,
  5. "info": $info-text-emphasis,
  6. "warning": $warning-text-emphasis,
  7. "danger": $danger-text-emphasis,
  8. "light": $light-text-emphasis,
  9. "dark": $dark-text-emphasis,
  10. );

scss/_maps.scss

  1. $theme-colors-text-dark: (
  2. "primary": $primary-text-emphasis-dark,
  3. "secondary": $secondary-text-emphasis-dark,
  4. "success": $success-text-emphasis-dark,
  5. "info": $info-text-emphasis-dark,
  6. "warning": $warning-text-emphasis-dark,
  7. "danger": $danger-text-emphasis-dark,
  8. "light": $light-text-emphasis-dark,
  9. "dark": $dark-text-emphasis-dark,
  10. );

Sass utilities API

Color utilities are declared in our utilities API in scss/_utilities.scss. Learn how to use the utilities API.

scss/_utilities.scss

  1. "color": (
  2. property: color,
  3. class: text,
  4. local-vars: (
  5. "text-opacity": 1
  6. ),
  7. values: map-merge(
  8. $utilities-text-colors,
  9. (
  10. "muted": var(--#{$prefix}secondary-color), // deprecated
  11. "black-50": rgba($black, .5), // deprecated
  12. "white-50": rgba($white, .5), // deprecated
  13. "body-secondary": var(--#{$prefix}secondary-color),
  14. "body-tertiary": var(--#{$prefix}tertiary-color),
  15. "body-emphasis": var(--#{$prefix}emphasis-color),
  16. "reset": inherit,
  17. )
  18. )
  19. ),
  20. "text-opacity": (
  21. css-var: true,
  22. class: text-opacity,
  23. values: (
  24. 25: .25,
  25. 50: .5,
  26. 75: .75,
  27. 100: 1
  28. )
  29. ),
  30. "text-color": (
  31. property: color,
  32. class: text,
  33. values: $utilities-text-emphasis-colors
  34. ),