跳至主要內容 跳至文件導覽

透過一些色彩工具程式類別,使用 color 傳達意義。也包含支援連結樣式和滑鼠移入狀態。

無障礙提示:使用色彩新增意義僅提供視覺指示,不會傳達給螢幕閱讀器等輔助技術的使用者。請確保意義從內容本身很明顯(例如,具有 足夠 色彩對比 的可見文字),或透過其他方式包含,例如使用 .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 中移除。
已棄用:隨著新增擴充的主題色彩和變數,.text-muted 工具已自 v5.3.0 起棄用。其預設值也已重新指定給新的 --bs-secondary-color CSS 變數,以更好地支援色彩模式。它將在 v6.0.0 中移除。

不透明度

新增於 v5.1.0

自 v5.1.0 起,文字色彩工具會使用 Sass 和 CSS 變數產生。這允許在不編譯的情況下進行即時色彩變更,並動態調整 alpha 透明度。

運作方式

考慮我們的預設 .text-primary 實用程式。

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

我們使用 --bs-primary (值為 13, 110, 253) CSS 變數的 RGB 版本,並附加一個第二個 CSS 變數 --bs-text-opacity,以設定 alpha 透明度 (預設值為 1,這要歸功於一個本機 CSS 變數)。這表示現在您每次使用 .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

顏色實用程式在 scss/_utilities.scss 中的實用程式 API 中宣告。 瞭解如何使用實用程式 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
),