Utilities
Background
Convey meaning through background-color and add decoration with gradients.
View page source on GitHubOn this page
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.
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, or the .text-bg-* helpers that set both.
Unlike in Bootstrap, the .bg-* utilities of the theme colors respond to color modes: they are light-dark() pairs. .bg-light and .bg-dark keep their tone like Bootstrap's, so .bg-light .text-dark and .bg-dark .text-light stay readable in both modes.
<div class="p-3 mb-2 bg-primary text-white">.bg-primary</div>
<div class="p-3 mb-2 bg-primary-subtle text-primary-emphasis">.bg-primary-subtle</div>
<div class="p-3 mb-2 bg-secondary text-white">.bg-secondary</div>
<div class="p-3 mb-2 bg-secondary-subtle text-secondary-emphasis">.bg-secondary-subtle</div>
<div class="p-3 mb-2 bg-success text-white">.bg-success</div>
<div class="p-3 mb-2 bg-success-subtle text-success-emphasis">.bg-success-subtle</div>
<div class="p-3 mb-2 bg-danger text-white">.bg-danger</div>
<div class="p-3 mb-2 bg-danger-subtle text-danger-emphasis">.bg-danger-subtle</div>
<div class="p-3 mb-2 bg-warning text-dark">.bg-warning</div>
<div class="p-3 mb-2 bg-warning-subtle text-warning-emphasis">.bg-warning-subtle</div>
<div class="p-3 mb-2 bg-info text-white">.bg-info</div>
<div class="p-3 mb-2 bg-info-subtle text-info-emphasis">.bg-info-subtle</div>
<div class="p-3 mb-2 bg-light text-dark">.bg-light</div>
<div class="p-3 mb-2 bg-light-subtle text-light-emphasis">.bg-light-subtle</div>
<div class="p-3 mb-2 bg-dark text-white">.bg-dark</div>
<div class="p-3 mb-2 bg-dark-subtle text-dark-emphasis">.bg-dark-subtle</div>
<div class="p-3 mb-2 bg-body-secondary">.bg-body-secondary</div>
<div class="p-3 mb-2 bg-body-tertiary">.bg-body-tertiary</div>
<div class="p-3 mb-2 bg-body text-body">.bg-body</div>
<div class="p-3 mb-2 bg-black text-white">.bg-black</div>
<div class="p-3 mb-2 bg-white text-dark">.bg-white</div>
<div class="p-3 mb-2 bg-transparent text-body">.bg-transparent</div>In the felt look the solid theme fills are cut from saturated felt (.bg-light from cream felt, in both modes), and .bg-body* and .bg-*-subtle from the same cream (or charcoal) felt as the page. A fill made see-through with .bg-opacity-* is a tint instead: the felt underneath shows through it.
Inside a sewn piece (a card, a list group, a modal, an offcanvas), .bg-body-secondary and .bg-body-tertiary mark a well, e.g. the details of a selected row:
Thursday
<div class="card">
<div class="card-body">
<h5 class="card-title">Thursday</h5>
<div class="bg-body-secondary rounded-3 p-3 mb-2">14:00 · 21 °C · 10 % rain · wind 12 km/h</div>
<div class="bg-body-tertiary rounded-3 p-3">15:00 · 22 °C · 5 % rain · wind 10 km/h</div>
</div>
</div>In the felt look such a well is a pocket: the felt is pressed in along its edge, .bg-body-tertiary a little shallower than .bg-body-secondary. Its padding and size don't change.
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. In the felt look it keeps the felt texture and swells like a patch instead.
Do you need a gradient in your custom CSS? Just add background-image: var(--felt-gradient);.
Opacity#
background-color utilities use CSS variables. This allows for real-time color changes and dynamic alpha transparency changes.
How it works#
Consider the default .bg-success utility.
.bg-success {
--felt-bg-opacity: 1;
background-color: color-mix(in srgb, var(--felt-success) calc(var(--felt-bg-opacity) * 100%), transparent) !important;
}The theme colors are light-dark() values rather than RGB triplets, so instead of Bootstrap's rgba() the utility fades the color with color-mix(), using a second CSS variable, --felt-bg-opacity, for the alpha transparency (with a default value 1 thanks to a local CSS variable). 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 --felt-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="--felt-bg-opacity: .5;">This is 50% opacity success background</div>Or, choose from any of the .bg-opacity utilities (10, 25, 50, 75, 100):
<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-body bg-opacity-50">This is 50% opacity success background</div>
<div class="bg-success p-2 text-body bg-opacity-25">This is 25% opacity success background</div>
<div class="bg-success p-2 text-body bg-opacity-10">This is 10% opacity success background</div>