メインコンテンツにスキップ ドキュメントナビゲーションにスキップ

少数のカラーユーティリティクラスを使用して、color を通じて意味を伝えましょう。ホバーステートでリンクをスタイルすることもできます。

このページの内容
アクセシビリティのヒント:色を使用して意味を追加すると、視覚的な指示のみが提供され、スクリーンリーダーなどの支援技術のユーザーには伝わりません。意味がコンテンツ自体から明らかであること(例:十分な色コントラストを持つ可視テキスト)、または.visually-hiddenクラスで非表示にされた追加テキストなど、別の方法で含まれていることを確認してください。

カラーユーティリティでテキストに色を付けます。リンクに色を付けたい場合は、:hover:focusの状態を持つ.link-*ヘルパークラスを使用できます。

元の $theme-colors Sass マップから生成された .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

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 以降、テキストカラーユーティリティは CSS 変数を使用して Sass で生成されます。これにより、コンパイルなしでリアルタイムの色変更と動的なアルファ透過性の変更が可能になります。

仕組み

デフォルトの .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 バージョンを使用し、アルファ透過性のために 2 番目の CSS 変数 --bs-text-opacity を添付します (ローカル CSS 変数のおかげでデフォルト値は 1 です)。つまり、.text-primary を使用すると、計算された color 値は rgba(13, 110, 253, 1) になります。各 .text-* クラス内のローカル CSS 変数は継承の問題を回避するため、ユーティリティのネストされたインスタンスはアルファ透過性が自動的に変更されません。

その不透明度を変更するには、カスタムスタイルまたはインラインスタイルで --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
),