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.
<div class="p-3 mb-2 bg-primary text-white">.bg-primary</div>
<div class="p-3 mb-2 bg-secondary text-white">.bg-secondary</div>
<div class="p-3 mb-2 bg-success text-white">.bg-success</div>
<div class="p-3 mb-2 bg-danger text-white">.bg-danger</div>
<div class="p-3 mb-2 bg-warning text-dark">.bg-warning</div>
<div class="p-3 mb-2 bg-info text-white">.bg-info</div>
<div class="p-3 mb-2 bg-light text-dark">.bg-light</div>
<div class="p-3 mb-2 bg-dark text-white">.bg-dark</div>
<div class="p-3 mb-2 bg-maricopa-blue text-white">.bg-maricopa-blue</div>
<div class="p-3 mb-2 bg-cgc-primary text-white">.bg-cgc-primary</div>
<div class="p-3 mb-2 bg-cgc-secondary text-white">.bg-cgc-secondary</div>
<div class="p-3 mb-2 bg-emc-primary text-white">.bg-emc-primary</div>
<div class="p-3 mb-2 bg-emc-secondary text-dark">.bg-emc-secondary</div>
<div class="p-3 mb-2 bg-gcc-primary text-white">.bg-gcc-primary</div>
<div class="p-3 mb-2 bg-gcc-secondary text-white">.bg-gcc-secondary</div>
<div class="p-3 mb-2 bg-gwc-primary text-white">.bg-gwc-primary</div>
<div class="p-3 mb-2 bg-gwc-secondary text-white">.bg-gwc-secondary</div>
<div class="p-3 mb-2 bg-mcc-primary text-white">.bg-mcc-primary</div>
<div class="p-3 mb-2 bg-mcc-secondary text-white">.bg-mcc-secondary</div>
<div class="p-3 mb-2 bg-pcc-primary text-white">.bg-pcc-primary</div>
<div class="p-3 mb-2 bg-pcc-secondary text-dark">.bg-pcc-secondary</div>
<div class="p-3 mb-2 bg-pvc-primary text-white">.bg-pvc-primary</div>
<div class="p-3 mb-2 bg-pvc-secondary text-white">.bg-pvc-secondary</div>
<div class="p-3 mb-2 bg-rsc-primary text-white">.bg-rsc-primary</div>
<div class="p-3 mb-2 bg-rsc-secondary text-dark">.bg-rsc-secondary</div>
<div class="p-3 mb-2 bg-scc-primary text-white">.bg-scc-primary</div>
<div class="p-3 mb-2 bg-scc-secondary text-dark">.bg-scc-secondary</div>
<div class="p-3 mb-2 bg-smc-primary text-white">.bg-smc-primary</div>
<div class="p-3 mb-2 bg-smc-secondary text-white">.bg-smc-secondary</div>
<div class="p-3 mb-2 bg-body text-dark">.bg-body</div>
<div class="p-3 mb-2 bg-white text-dark">.bg-white</div>
<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);
.
Opacity
Added in v5.1.0As 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.
.bg-success {
--bs-bg-opacity: 1;
background-color: rgba(var(--bs-success-rgb), var(--bs-bg-opacity)) !important;
}
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.
<div class="bg-success p-2 text-white">This is default success background</div>
<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:
<div class="bg-success p-2 text-white">This is default success background</div>
<div class="bg-success p-2 text-white bg-opacity-75">This is 75% opacity success background</div>
<div class="bg-success p-2 text-dark bg-opacity-50">This is 50% opacity success background</div>
<div class="bg-success p-2 text-dark bg-opacity-25">This is 25% opacity success background</div>
<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.
$blue: #002b5c;
$indigo: #0171bc;
$purple: #6f42c1;
$pink: #d63384;
$red: #c10520;
$orange: #fd7e14;
$yellow: #f7d068;
$green: #228736;
$teal: #20c997;
$cyan: #0057b8;
$skyblue: #d9edf7;
$primary: $blue;
$secondary: $indigo;
$success: $green;
$info: $cyan;
$warning: $yellow;
$danger: $red;
$light: $gray-100;
$dark: $gray-700;
$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.
$white: #fff;
$gray-100: #f2f2f2;
$gray-200: #e0e0e0;
$gray-300: #cccccc;
$gray-400: #999999;
$gray-500: #666666;
$gray-600: #545454;
$gray-700: #404040;
$gray-800: #262626;
$gray-900: #171717;
$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.
$theme-colors: (
"primary": $primary,
"secondary": $secondary,
"success": $success,
"info": $info,
"warning": $warning,
"danger": $danger,
"light": $light,
"dark": $dark
);
Grayscale colors are also available as a Sass map. This map is not used to generate any utilities.
$grays: (
"100": $gray-100,
"200": $gray-200,
"300": $gray-300,
"400": $gray-400,
"500": $gray-500,
"600": $gray-600,
"700": $gray-700,
"800": $gray-800,
"900": $gray-900
);
RGB colors are generated from a separate Sass map:
$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:
$utilities-bg: map-merge(
$utilities-colors,
(
"black": to-rgb($black),
"white": to-rgb($white),
"body": to-rgb($body-bg)
)
);
$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.
@mixin gradient-bg($color: null) {
background-color: $color;
@if $enable-gradients {
background-image: var(--#{$prefix}gradient);
}
}
// Horizontal gradient, from left to right
//
// Creates two color stops, start and end, by specifying a color and position for each color stop.
@mixin gradient-x($start-color: $gray-700, $end-color: $gray-800, $start-percent: 0%, $end-percent: 100%) {
background-image: linear-gradient(to right, $start-color $start-percent, $end-color $end-percent);
}
// Vertical gradient, from top to bottom
//
// Creates two color stops, start and end, by specifying a color and position for each color stop.
@mixin gradient-y($start-color: $gray-700, $end-color: $gray-800, $start-percent: null, $end-percent: null) {
background-image: linear-gradient(to bottom, $start-color $start-percent, $end-color $end-percent);
}
@mixin gradient-directional($start-color: $gray-700, $end-color: $gray-800, $deg: 45deg) {
background-image: linear-gradient($deg, $start-color, $end-color);
}
@mixin gradient-x-three-colors($start-color: $blue, $mid-color: $purple, $color-stop: 50%, $end-color: $red) {
background-image: linear-gradient(to right, $start-color, $mid-color $color-stop, $end-color);
}
@mixin gradient-y-three-colors($start-color: $blue, $mid-color: $purple, $color-stop: 50%, $end-color: $red) {
background-image: linear-gradient($start-color, $mid-color $color-stop, $end-color);
}
@mixin gradient-radial($inner-color: $gray-700, $outer-color: $gray-800) {
background-image: radial-gradient(circle, $inner-color, $outer-color);
}
@mixin gradient-striped($color: rgba($white, .15), $angle: 45deg) {
background-image: linear-gradient($angle, $color 25%, transparent 25%, transparent 50%, $color 50%, $color 75%, transparent 75%, transparent);
}
Utilities API
Background utilities are declared in our utilities API in scss/_utilities.scss
. Learn how to use the utilities API.
"background-color": (
property: background-color,
class: bg,
local-vars: (
"bg-opacity": 1
),
values: map-merge(
$utilities-bg-colors,
(
"transparent": transparent
)
)
),
"bg-opacity": (
css-var: true,
class: bg-opacity,
values: (
10: .1,
25: .25,
50: .5,
75: .75,
100: 1
)
),