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

colorいくつかのカラー ユーティリティ クラスを使用して意味を伝えます。ホバー状態のスタイル リンクのサポートも含まれています。

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

.text-primary

.text-secondary

.text-成功

.text-danger

.text-warning

.text-info

.text-light

.text-dark

.text-body

.text-muted

.text-白

.text-black-50

.text-white-50

<p class="text-primary">.text-primary</p>
<p class="text-secondary">.text-secondary</p>
<p class="text-success">.text-success</p>
<p class="text-danger">.text-danger</p>
<p class="text-warning bg-dark">.text-warning</p>
<p class="text-info bg-dark">.text-info</p>
<p class="text-light bg-dark">.text-light</p>
<p class="text-dark">.text-dark</p>
<p class="text-body">.text-body</p>
<p class="text-muted">.text-muted</p>
<p class="text-white bg-dark">.text-white</p>
<p class="text-black-50">.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 で削除されます。
支援技術に意味を伝える

色を使用して意味を追加しても、視覚的な指示しか提供されず、スクリーン リーダーなどの支援技術のユーザーには伝わりません。.visually-hidden色で示される情報が、コンテンツ自体 (表示されるテキストなど) から明らかであるか、クラスで非表示の追加テキストなどの代替手段によって含まれていることを確認してください。

不透明度

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(値が の) CSS 変数の RGB バージョンを使用し、アルファ透明度のために13, 110, 2532 番目の CSS 変数 を追加します (ローカル CSS 変数によるデフォルト値を使用)。つまり、今使用するときはいつでも、計算された値はです。各クラス内のローカル CSS 変数は継承の問題を回避するため、ユーティリティのネストされたインスタンスのアルファ透明度が自動的に変更されることはありません。--bs-text-opacity1.text-primarycolorrgba(13, 110, 253, 1).text-*

その不透明度を変更するには、--bs-text-opacityカスタム スタイルまたはインライン スタイルでオーバーライドします。

これはデフォルトのプライマリ テキストです
これは 50% の不透明度のプライマリ テキストです
<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% の不透明度のプライマリ テキストです
<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>、目的のクラスを使用して、要素のコンテンツを 1 つ以上のセマンティック要素でラップすることで十分な回避策があります。

サス

次の Sass 機能に加えて、色などのために含まれているCSS カスタム プロパティ(別名 CSS 変数) について読むことを検討してください。

変数

ほとんどの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;

地図

テーマの色は 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");

ユーティリティ API

カラー ユーティリティは、 のユーティリティ API で宣言されていますscss/_utilities.scssユーティリティ API の使用方法を学びます。

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