跳到主要内容

通过少量颜色工具类传达意义。还包括支持为链接添加悬停状态的样式。

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

颜色

使用颜色工具为文本着色。如果你想为链接着色,可以使用.link-* 辅助类,它们有 :hover 和 :focus 状态。

像.text-*这样的颜色工具是从我们最初的$theme-colors Sass 映射生成的,目前还不支持颜色模式,但任何.text-*-emphasis 工具都支持。这将在 v6 中解决。

.text-primary

.text-primary-emphasis

.text-secondary

.text-secondary-emphasis

.text-success

.text-success-emphasis

.text-danger

.text-danger-emphasis

.text-warning

.text-warning-emphasis

.text-info

.text-info-emphasis

.text-light

.text-light-emphasis

.text-dark

.text-dark-emphasis

.text-body

.text-body-emphasis

.text-body-secondary

.text-body-tertiary

.text-black

.text-white

.text-black-50

.text-white-50

HTML
<p class="text-primary">.text-primary</p>
<p class="text-primary-emphasis">.text-primary-emphasis</p>
<p class="text-secondary">.text-secondary</p>
<p class="text-secondary-emphasis">.text-secondary-emphasis</p>
<p class="text-success">.text-success</p>
<p class="text-success-emphasis">.text-success-emphasis</p>
<p class="text-danger">.text-danger</p>
<p class="text-danger-emphasis">.text-danger-emphasis</p>
<p class="text-warning bg-dark">.text-warning</p>
<p class="text-warning-emphasis">.text-warning-emphasis</p>
<p class="text-info bg-dark">.text-info</p>
<p class="text-info-emphasis">.text-info-emphasis</p>
<p class="text-light bg-dark">.text-light</p>
<p class="text-light-emphasis">.text-light-emphasis</p>
<p class="text-dark bg-white">.text-dark</p>
<p class="text-dark-emphasis">.text-dark-emphasis</p>

<p class="text-body">.text-body</p> <p class="text-body-emphasis">.text-body-emphasis</p> <p class="text-body-secondary">.text-body-secondary</p> <p class="text-body-tertiary">.text-body-tertiary</p>

<p class="text-black bg-white">.text-black</p> <p class="text-white bg-dark">.text-white</p> <p class="text-black-50 bg-white">.text-black-50</p> <p class="text-white-50 bg-dark">.text-white-50</p>

弃用: 随着 .text-opacity-* 工具和文本工具的 CSS 变量的添加,.text-black-50 和 .text-white-50 从 v5.1.0 起已被弃用。它们将在 v6.0.0 中被移除。

弃用:随着扩展主题颜色和变量的添加,自 v5.3.0 起,.text-muted 工具已被弃用。其默认值也已重新分配给新的 --bs-secondary-color CSS 变量,以更好地支持颜色模式。它将在 v6.0.0 中被移除。

不透明度

在 v5.1.0 中添加

从 v5.1.0 开始,文本颜色工具类使用 Sass 和 CSS 变量生成。这允许在不进行编译的情况下实时更改颜色,并动态调整透明度。

它是如何工作的

考虑我们默认的 .text-primary 效用。

.text-primary {
  --bs-text-opacity: 1;
  color: rgba(var(--bs-primary-rgb), var(--bs-text-opacity)) !important;
}

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

示例

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

这是默认主要文本
这是50%不透明度的主要文本
HTML
<div class="text-primary">This is default primary text</div>
<div class="text-primary" style="--bs-text-opacity: .5;">This is 50% opacity primary text</div>

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

这是默认主要文本
这是75%不透明度的主要文本
这是50%不透明度的主要文本
这是25%不透明度的主要文本
HTML
<div class="text-primary">This is default primary text</div>
<div class="text-primary text-opacity-75">This is 75% opacity primary text</div>
<div class="text-primary text-opacity-50">This is 50% opacity primary text</div>
<div class="text-primary text-opacity-25">This is 25% opacity primary text</div>

特异性

有时由于另一个选择器的特异性,无法应用上下文类。在某些情况下,一个足够的解决方法是将元素的内容包裹在一个 <div> 或更多具有所需类的语义元素。

CSS

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

Sass 变量

大多数 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;

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

$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;
$theme-colors-text: (
  "primary": $primary-text-emphasis,
  "secondary": $secondary-text-emphasis,
  "success": $success-text-emphasis,
  "info": $info-text-emphasis,
  "warning": $warning-text-emphasis,
  "danger": $danger-text-emphasis,
  "light": $light-text-emphasis,
  "dark": $dark-text-emphasis,
);

在浅色和深色模式下,用于设置.text-*-emphasis工具的颜色的变量:

$primary-text-emphasis:   shade-color($primary, 60%);
$secondary-text-emphasis: shade-color($secondary, 60%);
$success-text-emphasis:   shade-color($success, 60%);
$info-text-emphasis:      shade-color($info, 60%);
$warning-text-emphasis:   shade-color($warning, 60%);
$danger-text-emphasis:    shade-color($danger, 60%);
$light-text-emphasis:     $gray-700;
$dark-text-emphasis:      $gray-700;
$primary-text-emphasis-dark:        tint-color($primary, 40%);
$secondary-text-emphasis-dark:      tint-color($secondary, 40%);
$success-text-emphasis-dark:        tint-color($success, 40%);
$info-text-emphasis-dark:           tint-color($info, 40%);
$warning-text-emphasis-dark:        tint-color($warning, 40%);
$danger-text-emphasis-dark:         tint-color($danger, 40%);
$light-text-emphasis-dark:          $gray-100;
$dark-text-emphasis-dark:           $gray-300;

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-text: map-merge(
  $utilities-colors,
  (
    "black": to-rgb($black),
    "white": to-rgb($white),
    "body": to-rgb($body-color)
  )
);
$utilities-text-colors: map-loop($utilities-text, rgba-css-var, "$key", "text");

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

颜色模式自适应文本颜色也可以作为 Sass 映射提供:

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

Sass 工具 API

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

"color": (
  property: color,
  class: text,
  local-vars: (
    "text-opacity": 1
  ),
  values: map-merge(
    $utilities-text-colors,
    (
      "muted": var(--#{$prefix}secondary-color), // deprecated
      "black-50": rgba($black, .5), // deprecated
      "white-50": rgba($white, .5), // deprecated
      "body-secondary": var(--#{$prefix}secondary-color),
      "body-tertiary": var(--#{$prefix}tertiary-color),
      "body-emphasis": var(--#{$prefix}emphasis-color),
      "reset": inherit,
    )
  )
),
"text-opacity": (
  css-var: true,
  class: text-opacity,
  values: (
    25: .25,
    50: .5,
    75: .75,
    100: 1
  )
),
"text-color": (
  property: color,
  class: text,
  values: $utilities-text-emphasis-colors
),