顏色(Colors)
使用少量的色彩通用類別透過 color 傳達意義。也支援使用滑鼠懸停狀態來設定連結樣式。
無障礙提示: 僅使用顏色來傳達意義只提供了視覺指示,這不會傳達給使用輔助技術(如螢幕閱讀器)的使用者。請確保意義從內容本身就很明顯(例如,具有_足夠_色彩對比度的可見文字),或透過替代方式包含,例如使用 .visually-hidden 類別隱藏的額外文字。
色彩(Colors)
使用色彩工具程式為文字上色。如果您想為連結上色,可以使用 .link-* 輔助類別,它們具有 :hover 和 :focus 狀態。
從我們原始的 $theme-colors Sass map 產生的色彩工具程式(如 .text-*)尚未支援色彩模式回應,但任何 .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
<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 中移除。
透明度(Opacity)
Added in v5.1.0從 v5.1.0 開始,文字色彩工具程式使用 Sass 搭配 CSS 變數產生。這允許即時變更色彩而無需重新編譯,並可動態變更 alpha 透明度。
運作原理(How it works)
以我們預設的 .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 透明度(由於區域 CSS 變數,預設值為 1)。這意味著現在每當您使用 .text-primary 時,您計算出的 color 值為 rgba(13, 110, 253, 1)。每個 .text-* 類別內的區域 CSS 變數避免了繼承問題,因此巢狀的工具程式實例不會自動具有修改後的 alpha 透明度。
範例(Example)
要變更該透明度,請透過自訂樣式或行內樣式覆寫 --bs-text-opacity。
<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 工具程式中選擇:
<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> 權重(Specificity)
有時由於另一個選擇器的權重問題,情境類別無法被套用。在某些情況下,一個足夠的解決方法是將元素的內容包裝在 <div> 或更具語義的元素中,並使用所需的類別。
CSS
除了以下 Sass 功能外,請考慮閱讀我們包含的 CSS 自訂屬性(又稱 CSS 變數),了解更多關於色彩等方面的資訊。
Sass 變數(Sass variables)
大多數 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 maps
主題色彩接著會被放入 Sass map 中,以便我們可以迴圈遍歷它們來產生我們的工具程式、元件修飾符等。
$theme-colors: (
"primary": $primary,
"secondary": $secondary,
"success": $success,
"info": $info,
"warning": $warning,
"danger": $danger,
"light": $light,
"dark": $dark
);
灰階色彩也可作為 Sass map 使用。此 map 不用於產生任何工具程式。
$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 map 產生:
$theme-colors-rgb: map-loop($theme-colors, to-rgb, "$value");
色彩透明度使用自己的 map 建構,該 map 由 utilities 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 map 使用:
$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 utilities API
色彩通用類別在 scss/_utilities.scss 中的 utilities API 中宣告。了解如何使用 utilities 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
),