View on GitHub

Background

Convey meaning through background-color and add decoration with gradients.

Background color

Similar to the contextual text color classes, set the background of an element to any contextual class. Background utilities do not set color, so in some cases you’ll want to use .text-* color utilities.

Background - 图1

html

  1. <div class="p-3 mb-2 bg-primary text-white">.bg-primary</div>
  2. <div class="p-3 mb-2 bg-secondary text-white">.bg-secondary</div>
  3. <div class="p-3 mb-2 bg-success text-white">.bg-success</div>
  4. <div class="p-3 mb-2 bg-danger text-white">.bg-danger</div>
  5. <div class="p-3 mb-2 bg-warning text-dark">.bg-warning</div>
  6. <div class="p-3 mb-2 bg-info text-dark">.bg-info</div>
  7. <div class="p-3 mb-2 bg-light text-dark">.bg-light</div>
  8. <div class="p-3 mb-2 bg-dark text-white">.bg-dark</div>
  9. <div class="p-3 mb-2 bg-body text-dark">.bg-body</div>
  10. <div class="p-3 mb-2 bg-white text-dark">.bg-white</div>
  11. <div class="p-3 mb-2 bg-transparent text-dark">.bg-transparent</div>

Background gradient

By adding a .bg-gradient class, a linear gradient is added as background image to the backgrounds. This gradient starts with a semi-transparent white which fades out to the bottom.

Do you need a gradient in your custom CSS? Just add background-image: var(--bs-gradient);.

.bg-primary.bg-gradient

.bg-secondary.bg-gradient

.bg-success.bg-gradient

.bg-danger.bg-gradient

.bg-warning.bg-gradient

.bg-info.bg-gradient

.bg-light.bg-gradient

.bg-dark.bg-gradient

Opacity

Added in v5.1.0

As of v5.1.0, background-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 .bg-success utility.

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

We use an RGB version of our --bs-success (with the value of 25, 135, 84) CSS variable and attached a second CSS variable, --bs-bg-opacity, for the alpha transparency (with a default value 1 thanks to a local CSS variable). That means anytime you use .bg-success now, your computed color value is rgba(25, 135, 84, 1). The local CSS variable inside each .bg-* 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-bg-opacity via custom styles or inline styles.

Background - 图2

html

  1. <div class="bg-success p-2 text-white">This is default success background</div>
  2. <div class="bg-success p-2" style="--bs-bg-opacity: .5;">This is 50% opacity success background</div>

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

Background - 图3

html

  1. <div class="bg-success p-2 text-white">This is default success background</div>
  2. <div class="bg-success p-2 text-white bg-opacity-75">This is 75% opacity success background</div>
  3. <div class="bg-success p-2 text-dark bg-opacity-50">This is 50% opacity success background</div>
  4. <div class="bg-success p-2 text-dark bg-opacity-25">This is 25% opacity success background</div>
  5. <div class="bg-success p-2 text-dark bg-opacity-10">This is 10% opacity success background</div>

Sass

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

Variables

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

  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;
  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;
  1. $gradient: linear-gradient(180deg, rgba($white, .15), rgba($white, 0));

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;

Map

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. "dark": $dark
  10. );

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

  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:

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

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

  1. $utilities-bg: map-merge(
  2. $utilities-colors,
  3. (
  4. "black": to-rgb($black),
  5. "white": to-rgb($white),
  6. "body": to-rgb($body-bg)
  7. )
  8. );
  9. $utilities-bg-colors: map-loop($utilities-bg, rgba-css-var, "$key", "bg");

Mixins

No mixins are used to generate our background utilities, but we do have some additional mixins for other situations where you’d like to create your own gradients.

  1. @mixin gradient-bg($color: null) {
  2. background-color: $color;
  3. @if $enable-gradients {
  4. background-image: var(--#{$prefix}gradient);
  5. }
  6. }
  1. // Horizontal gradient, from left to right
  2. //
  3. // Creates two color stops, start and end, by specifying a color and position for each color stop.
  4. @mixin gradient-x($start-color: $gray-700, $end-color: $gray-800, $start-percent: 0%, $end-percent: 100%) {
  5. background-image: linear-gradient(to right, $start-color $start-percent, $end-color $end-percent);
  6. }
  7. // Vertical gradient, from top to bottom
  8. //
  9. // Creates two color stops, start and end, by specifying a color and position for each color stop.
  10. @mixin gradient-y($start-color: $gray-700, $end-color: $gray-800, $start-percent: null, $end-percent: null) {
  11. background-image: linear-gradient(to bottom, $start-color $start-percent, $end-color $end-percent);
  12. }
  13. @mixin gradient-directional($start-color: $gray-700, $end-color: $gray-800, $deg: 45deg) {
  14. background-image: linear-gradient($deg, $start-color, $end-color);
  15. }
  16. @mixin gradient-x-three-colors($start-color: $blue, $mid-color: $purple, $color-stop: 50%, $end-color: $red) {
  17. background-image: linear-gradient(to right, $start-color, $mid-color $color-stop, $end-color);
  18. }
  19. @mixin gradient-y-three-colors($start-color: $blue, $mid-color: $purple, $color-stop: 50%, $end-color: $red) {
  20. background-image: linear-gradient($start-color, $mid-color $color-stop, $end-color);
  21. }
  22. @mixin gradient-radial($inner-color: $gray-700, $outer-color: $gray-800) {
  23. background-image: radial-gradient(circle, $inner-color, $outer-color);
  24. }
  25. @mixin gradient-striped($color: rgba($white, .15), $angle: 45deg) {
  26. background-image: linear-gradient($angle, $color 25%, transparent 25%, transparent 50%, $color 50%, $color 75%, transparent 75%, transparent);
  27. }

Utilities API

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

  1. "background-color": (
  2. property: background-color,
  3. class: bg,
  4. local-vars: (
  5. "bg-opacity": 1
  6. ),
  7. values: map-merge(
  8. $utilities-bg-colors,
  9. (
  10. "transparent": transparent
  11. )
  12. )
  13. ),
  14. "bg-opacity": (
  15. css-var: true,
  16. class: bg-opacity,
  17. values: (
  18. 10: .1,
  19. 25: .25,
  20. 50: .5,
  21. 75: .75,
  22. 100: 1
  23. )
  24. ),