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.

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.

Conveying meaning to assistive technologies

Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies – such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (e.g. the visible text), or is included through alternative means, such as additional text hidden with the .visually-hidden class.

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.

  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.

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

Colors - 图2

  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:

  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>

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 or more semantic element with the desired class.

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

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

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

  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;

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

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

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

RGB colors are generated from a separate Sass map:

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

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

  1. $utilities-text-colors: map-loop($utilities-colors, rgba-css-var, "$key", "text");
  1. property: color,
  2. class: text,
  3. local-vars: (
  4. "text-opacity": 1
  5. ),
  6. values: map-merge(
  7. $utilities-text-colors,
  8. (
  9. "muted": $text-muted,
  10. "black-50": rgba($black, .5), // deprecated
  11. "white-50": rgba($white, .5), // deprecated
  12. "reset": inherit,
  13. )
  14. )
  15. ),
  16. "text-opacity": (
  17. css-var: true,
  18. class: text-opacity,
  19. values: (
  20. 25: .25,
  21. 50: .5,
  22. 75: .75,
  23. 100: 1
  24. ),