跳至主要内容 跳至文档导航

颜色和背景

设置具有对比前景色的背景色。

当前页

概述

颜色和背景助手将我们的 .text-* 实用程序.bg-* 实用程序 的功能组合在一个类中。 使用我们的 Sass color-contrast() 函数,对比 color,我们会自动确定特定的 background-color

Color and background helpers combine the power of our .text-* utilities and .bg-* utilities in one class. Using our Sass color-contrast() function, we automatically determine a contrasting color for a particular background-color.

**注意!**目前不支持CSS原生的color-contrast功能,所以我们通过Sass使用我们自己的函数。这意味着通过CSS变量自定义主题颜色可能会导致这些实用程序出现颜色对比度问题。
Primary with contrasting color
Secondary with contrasting color
Success with contrasting color
Danger with contrasting color
Warning with contrasting color
Info with contrasting color
Light with contrasting color
Dark with contrasting color
html
<div class="text-bg-primary p-3">Primary with contrasting color</div>
<div class="text-bg-secondary p-3">Secondary with contrasting color</div>
<div class="text-bg-success p-3">Success with contrasting color</div>
<div class="text-bg-danger p-3">Danger with contrasting color</div>
<div class="text-bg-warning p-3">Warning with contrasting color</div>
<div class="text-bg-info p-3">Info with contrasting color</div>
<div class="text-bg-light p-3">Light with contrasting color</div>
<div class="text-bg-dark p-3">Dark with contrasting color</div>
辅助功能提示: 使用颜色添加含义仅提供视觉指示,不会传达给屏幕阅读器等辅助技术的用户。请确保从内容本身(例如,可见文本)中可以明显看出含义,或者通过其他方式包含含义,例如使用.visually-hidden 类隐藏的附加文本。

使用组件

使用它们代替组合的.text-*.bg-* 类,如badges

Primary Info
html
<span class="badge text-bg-primary">Primary</span>
<span class="badge text-bg-info">Info</span>

或者在 卡片

Header

Some quick example text to build on the card title and make up the bulk of the card's content.

Header

Some quick example text to build on the card title and make up the bulk of the card's content.

html
<div class="card text-bg-primary mb-3" style="max-width: 18rem;">
  <div class="card-header">Header</div>
  <div class="card-body">
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
  </div>
</div>
<div class="card text-bg-info mb-3" style="max-width: 18rem;">
  <div class="card-header">Header</div>
  <div class="card-body">
    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
  </div>
</div>