跳到主要内容

通过背景颜色传达含义,并通过渐变添加装饰。

无障碍提示:仅使用颜色来增加意义只提供视觉提示,这对于使用辅助技术(如屏幕阅读器)的用户是无法传达的。请确保意义本身在内容中就显而易见(例如,可见文本通过 足够的颜色对比)或者通过其他方式包含,例如使用 .visually-hidden 类隐藏的额外文本。

背景颜色

类似于上下文文本颜色类,可以将元素的背景设置为任何上下文类。背景工具类 不会设置 color,因此在某些情况下,你可能需要使用 .text-* 颜色工具类。

后台工具类如 .bg-* 源自我们的原始 $theme-colors Sass 映射尚未响应颜色模式,然而,任何 .bg-*-subtle 工具类意愿。这将在 v6 中解决。

.bg-primary
.bg-primary-subtle
.bg-secondary
.bg-secondary-subtle
.bg-success
.bg-success-subtle
.bg-danger
.bg-danger-subtle
.bg-warning
.bg-warning-subtle
.bg-info
.bg-info-subtle
.bg-light
.bg-light-subtle
.bg-dark
.bg-dark-subtle
.bg-body-secondary
.bg-body-tertiary
.bg-body
.bg-black
.bg-white
.bg-透明
HTML
<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-dark">.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>

背景渐变

通过添加一个 .bg-gradient 类,在背景上添加了线性渐变作为背景图像。这个渐变从半透明的白色开始,并在底部逐渐消失。

你在自定义 CSS 中需要渐变吗?只需添加 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
.bg-black.bg-gradient

不透明度

在 v5.1.0 中添加

从 v5.1.0 起,background-color 工具使用 Sass 结合 CSS 变量生成。这允许在不进行编译的情况下实时更改颜色以及动态更改 alpha 透明度。

它是如何工作的

考虑我们默认的 .bg-success 效用。

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

我们使用了--bs-success的 RGB 版本(CSS 变量值为 25, 135, 84),并附加了第二个 CSS 变量 --bs-bg-opacity 用于 alpha 透明度(借助本地 CSS 变量,默认值为 1)。这意味着每当你现在使用 .bg-success 时,你的计算color 值就是 rgba(25, 135, 84, 1)。每个 .bg-* 类中的本地 CSS 变量避免了继承问题,因此工具类的嵌套实例不会自动具有修改的 alpha 透明度。

示例

要更改该不透明度,请通过自定义样式或内联样式覆盖 --bs-bg-opacity。

这是默认的成功背景
这是50%不透明度的成功背景
HTML
<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>

或者,从任何 .bg-opacity 个工具类中选择:

这是默认的成功背景
这是75%不透明度的成功背景
这是50%不透明度的成功背景
这是25%不透明度的成功背景
这是10%不透明度的成功背景
HTML
<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>

CSS

除了以下的 Sass 功能之外,还可以考虑阅读我们包含的 CSS 自定义属性(即 CSS 变量)以了解颜色和更多内容。

Sass 变量

大多数 background-color 工具是由我们的主题颜色生成的,从我们的通用调色板变量中重新分配。

$blue:    #0d6efd;
$indigo:  #6610f2;
$purple:  #6f42c1;
$pink:    #d63384;
$red:     #dc3545;
$orange:  #fd7e14;
$yellow:  #ffc107;
$green:   #198754;
$teal:    #20c997;
$cyan:    #0dcaf0;
$primary:       $blue;
$secondary:     $gray-600;
$success:       $green;
$info:          $cyan;
$warning:       $yellow;
$danger:        $red;
$light:         $gray-100;
$dark:          $gray-900;
$gradient: linear-gradient(180deg, rgba($white, .15), rgba($white, 0));

灰度颜色也可用,但只有一部分会被用于生成任何工具。

$white:    #fff;
$gray-100: #f8f9fa;
$gray-200: #e9ecef;
$gray-300: #dee2e6;
$gray-400: #ced4da;
$gray-500: #adb5bd;
$gray-600: #6c757d;
$gray-700: #495057;
$gray-800: #343a40;
$gray-900: #212529;
$black:    #000;

用于在浅色和深色模式下设置background-color在.bg-*-subtle工具类中的变量:

$primary-bg-subtle:       tint-color($primary, 80%);
$secondary-bg-subtle:     tint-color($secondary, 80%);
$success-bg-subtle:       tint-color($success, 80%);
$info-bg-subtle:          tint-color($info, 80%);
$warning-bg-subtle:       tint-color($warning, 80%);
$danger-bg-subtle:        tint-color($danger, 80%);
$light-bg-subtle:         mix($gray-100, $white);
$dark-bg-subtle:          $gray-400;
$primary-bg-subtle-dark:            shade-color($primary, 80%);
$secondary-bg-subtle-dark:          shade-color($secondary, 80%);
$success-bg-subtle-dark:            shade-color($success, 80%);
$info-bg-subtle-dark:               shade-color($info, 80%);
$warning-bg-subtle-dark:            shade-color($warning, 80%);
$danger-bg-subtle-dark:             shade-color($danger, 80%);
$light-bg-subtle-dark:              $gray-800;
$dark-bg-subtle-dark:               mix($gray-800, $black);

Sass 映射

主题颜色随后被放入一个 Sass 映射中,这样我们就可以遍历它们来生成我们的工具类、组件修饰符等。

$theme-colors: (
  "primary":    $primary,
  "secondary":  $secondary,
  "success":    $success,
  "info":       $info,
  "warning":    $warning,
  "danger":     $danger,
  "light":      $light,
  "dark":       $dark
);

灰度颜色也可以作为 Sass 映射使用。此映射不会用来生成任何工具类。

$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 颜色是从一个独立的 Sass 映射生成的:

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

背景颜色不透明度在此基础上构建了自己的映射,该映射由工具类 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");

$utilities-bg-subtle: ( "primary-subtle": var(--#{$prefix}primary-bg-subtle), "secondary-subtle": var(--#{$prefix}secondary-bg-subtle), "success-subtle": var(--#{$prefix}success-bg-subtle), "info-subtle": var(--#{$prefix}info-bg-subtle), "warning-subtle": var(--#{$prefix}warning-bg-subtle), "danger-subtle": var(--#{$prefix}danger-bg-subtle), "light-subtle": var(--#{$prefix}light-bg-subtle), "dark-subtle": var(--#{$prefix}dark-bg-subtle) );

颜色模式背景色也可以作为 Sass 映射使用:

$theme-colors-bg-subtle: (
  "primary": $primary-bg-subtle,
  "secondary": $secondary-bg-subtle,
  "success": $success-bg-subtle,
  "info": $info-bg-subtle,
  "warning": $warning-bg-subtle,
  "danger": $danger-bg-subtle,
  "light": $light-bg-subtle,
  "dark": $dark-bg-subtle,
);
$theme-colors-bg-subtle-dark: (
  "primary": $primary-bg-subtle-dark,
  "secondary": $secondary-bg-subtle-dark,
  "success": $success-bg-subtle-dark,
  "info": $info-bg-subtle-dark,
  "warning": $warning-bg-subtle-dark,
  "danger": $danger-bg-subtle-dark,
  "light": $light-bg-subtle-dark,
  "dark": $dark-bg-subtle-dark,
);

Sass mixin

生成背景工具时未使用任何mixin,但我们确实有一些用于其他情况的额外mixin,当你想创建自己的渐变时可以使用。

@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); }

Sass 工具 API

后台工具类在我们的工具类 API scss/_utilities.scss 中声明。了解如何使用工具类 API。

"background-color": (
  property: background-color,
  class: bg,
  local-vars: (
    "bg-opacity": 1
  ),
  values: map-merge(
    $utilities-bg-colors,
    (
      "transparent": transparent,
      "body-secondary": rgba(var(--#{$prefix}secondary-bg-rgb), var(--#{$prefix}bg-opacity)),
      "body-tertiary": rgba(var(--#{$prefix}tertiary-bg-rgb), var(--#{$prefix}bg-opacity)),
    )
  )
),
"bg-opacity": (
  css-var: true,
  class: bg-opacity,
  values: (
    10: .1,
    25: .25,
    50: .5,
    75: .75,
    100: 1
  )
),
"subtle-background-color": (
  property: background-color,
  class: bg,
  values: $utilities-bg-subtle
),