カラーユーティリティ(Colors)
いくつかのカラーユーティリティクラスを使用して、color を通じて意味を伝える。ホバー状態とのリンクのスタイルのサポートも含む。
※"v5.0.0"での変更はv5.0.0-alpha版、v5.0.0-beta版での変更も含みます。
※"v5.3.0"での変更はv5.3.0-alpha版での変更も含みます。
.visually-hidden クラスで非表示にした追加テキストなどの代替手段を入れるように心がけてください。
文字色(Colors)v5.1.0スタイル変更、v5.3.0クラス追加
文字色ユーティリティを使用してテキストを色付ける。リンクを色付けしたい場合は、:hover と :focus 状態を持つ .link-{themecolor} ヘルパークラスを使用する。
$theme-colors Sassマップから生成された .text-* などの文字色ユーティリティは、まだカラーモードに対応していないが、.text-*-emphasis ユーティリティはすべて対応している。これはv6で解決される予定。
文字色の種類※モードによって色が見やすいように背景色をつけています
この文章はダミーです。Primary:.text-primary
この文章はダミーです。強調Primary:.text-primary-emphasisv5.3.0追加
この文章はダミーです。Secondary:.text-secondary
この文章はダミーです。強調Secondary:.text-secondary-emphasisv5.3.0追加
この文章はダミーです。Success:.text-success
この文章はダミーです。強調Success:.text-success-emphasisv5.3.0追加
この文章はダミーです。Danger:.text-danger
この文章はダミーです。強調Danger:.text-danger-emphasisv5.3.0追加
この文章はダミーです。Warning:.text-warning
この文章はダミーです。強調Warning:.text-warning-emphasisv5.3.0追加
この文章はダミーです。Info:.text-info
この文章はダミーです。強調Info:.text-info-emphasisv5.3.0追加
この文章はダミーです。Light:.text-light
この文章はダミーです。強調Light:.text-light-emphasisv5.3.0追加
この文章はダミーです。Dark:.text-dark
この文章はダミーです。強調Dark:.text-dark-emphasisv5.3.0追加
この文章はダミーです。Body(<body> の文字色と同じ配色):.text-body
この文章はダミーです。強調文字カラー:.text-body-emphasisv5.3.0追加
この文章はダミーです。二次Body:.text-body-secondaryv5.3.0追加
この文章はダミーです。三次Body:.text-body-tertiaryv5.3.0追加
この文章はダミーです。Muted(弱め):.text-mutedv5.3.0非推奨
この文章はダミーです。Black:.text-blackv5.1.0追加
この文章はダミーです。White:.text-white
この文章はダミーです。Black-50(50%不透明な黒色):.text-black-50v5.1.0非推奨
この文章はダミーです。White-50(50%不透明な白色):.text-white-50v5.1.0非推奨
この文章はダミーです。※デフォルトの場合(参考)
設定例
<span class="text-primary">この文章はダミーです。</span>
【設定】
<p>や<span>などの要素に.text-{themecolor}(-{emphasis})か.text-{color}(上記の「文字色の種類」から選択)を入れる{themecolor}は、primary,secondary,success,danger,warning,info,light,darkのいずれかを選択{color}は、black,white,body,body-emphasis,body-secondary,body-tertiary,muted,black-50,white-50のいずれかを選択
【注意】
.text-{themecolor}(-{emphasis}),.text-body,.text-body-emphasis,.text-body-secondary,.text-body-tertiary,.text-mutedはライトモードとダークモードで色が変わる- 色の設定を変更する場合は
- ライトモード:
scss/_variables.scss内の${themecolor}-text,$body(-{emphasis|secondary|tertiary})-colorの値 - ダークモード:
scss/_variables-dark.scss内の${themecolor}-text-dark,$body(-{emphasissecondary|tertiary})-color-darkの値
- ライトモード:
- 親要素の色を継承したい場合は、色の継承に記載
- リンクの色を強調したい場合は、色付きリンクに記載
【変更履歴】
- 【v5.0.0-alpha1】
<a>タグに.text-{color}を入れてもホバーやフォーカス時に濃いめに強調されなくなった(.link-{themecolor}(リンクヘルパークラス)を使用)
- 【v5.1.0】
.text-{color}の{color}設定がHEX値からRGBテーマカラーCSS変数と透明度アルファ値CSS変数に変更.text-blackが追加.text-black-50(.text-black.text-opacity-50を使用)と.text-white-50(.text-white.text-opacity-50を使用)は非推奨に(v5.1.0~でも使用できるがv6で廃止予定)
- 【v5.2.0】
- ※
scss/_variables.scss内の$text-mutedの値(.text-mutedの色)がv5.2.0-beta1で$gray-600からrgba(var(--#{$prefix}body-color-rgb), .75)に変更されたが、.bg-dark上では文字が全く見えないので元に戻した
- ※
- 【v5.3.0】
.text-{themecolor}-emphasis,.text-body-emphasis,.text-body-secondary,.text-body-tertiaryが追加.text-muted(.text-body-secondaryを使用)は非推奨に(v5.3.0~でも使用できるがv6で廃止予定)- ※ライトモードでの
.text-{themecolor}-emphasisのcolorがv5.3.0-alpha2で若干暗めになる
.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)v5.1.0新設
v5.1.0以降、文字色ユーティリティはCSS変数を使用してSassで生成される。これにより、コンパイルや動的な透明度のアルファ値の変更なしで、リアルタイムの色の変更が可能になる。
使い方(How it works)
デフォルトの .text-primary ユーティリティについて考察:
Bootstrap5.1.0~の設定 緑背景が5.1.0での変更箇所
.text-primary {
--bs-text-opacity: 1;
color: rgba(var(--bs-primary-rgb), var(--bs-text-opacity)) !important;
}
※Bootstrap5.0.2の設定 赤背景が5.1.0での変更箇所
.text-primary {
color: #0d6efd !important;
}
--bs-primary のRGBバージョン(値は 13, 110, 253)のCSS変数を使用し、透明度のアルファ値のために2番目のCSS変数 --bs-text-opacity を添付(ローカルCSS変数のおかげでデフォルト値は 1)。つまり今は .text-primary を使用すると、計算された color 値は rgba(13, 110, 253, 1) になる。各 .text-* クラス内のローカルCSS変数は継承の問題を回避するため、ユーティリティの入れ子になったインスタンスは自動的に変更された透明度のアルファ値を持たない。
実例(Examples)
その不透明度を変更するには、カスタムスタイルかインラインスタイルを経由して --bs-text-opacity を再定義。
1.インラインスタイルで設定
見本
設定例
50%の不透明度のPrimaryの文字<div class="text-primary" style="--bs-text-opacity: .5;">...</div>
2.定義済クラスで設定
または、.text-opacity ユーティリティのいずれかから選択。
不透明度の種類
設定なし(参考)(--bs-text-opacity: 1)
.text-opacity-75(--bs-text-opacity: 0.75)
.text-opacity-50(--bs-text-opacity: 0.5)
.text-opacity-25(--bs-text-opacity: 0.25)
設定例
75%の不透明度のPrimaryの文字<div class="text-primary text-opacity-75">...</div>
定義済クラスの配色
.text-primary.text-primary.text-opacity-75.text-primary.text-opacity-50.text-primary.text-opacity-25.text-secondary.text-secondary.text-opacity-75.text-secondary.text-opacity-50.text-secondary.text-opacity-25.text-success.text-success.text-opacity-75.text-success.text-opacity-50.text-success.text-opacity-25.text-info.text-info.text-opacity-75.text-info.text-opacity-50.text-info.text-opacity-25.text-warning.text-warning.text-opacity-75.text-warning.text-opacity-50.text-warning.text-opacity-25.text-danger.text-danger.text-opacity-75.text-danger.text-opacity-50.text-danger.text-opacity-25.text-light.text-light.text-opacity-75.text-light.text-opacity-50.text-light.text-opacity-25.text-dark.text-dark.text-opacity-75.text-dark.text-opacity-50.text-dark.text-opacity-25.text-body.text-body.text-opacity-75.text-body.text-opacity-50.text-body.text-opacity-25.text-black.text-black.text-opacity-75.text-black.text-opacity-50.text-black.text-opacity-25.text-white.text-white.text-opacity-75.text-white.text-opacity-50.text-white.text-opacity-25【設定】
.text-{color}に インラインでstyle="--bs-text-opacity: {value};"を入れるか、.text-{color}に.text-opacity-{value}(上記の「不透明度の種類」から選択)を追加{value}は、25,50,75のいずれかを選択
【注意】
- デフォルトでは、
.text-opacity-10は存在しない(たぶん薄すぎるから) .text-{themecolor}-emphasis(.text-body-emphasisも含む)は、colorの設定がrgba方式ではないため設定不可.text-body-secondary,.text-body-tertiaryは、colorの設定に opacity値が固定されているため設定不可
特異性(Specificity)
各色のクラスは、別のセレクタの特異性のために適用できないことがある。場合によっては、要素のコンテンツを更に <div> で囲むようにすれば十分。
CSS v5.0.0-beta2追加、v5.3.0Sassから名称変更
以下のSass機能に加えて、カラーなどに含まれているCSSカスタムプロパティ(別名CSS変数)についても読んでください。
Sass変数(Sass variables)v5.3.0変数から名称変更
ほとんどの color ユーティリティは、一般的なカラーパレット変数から再割り当てされたテーマカラーによって生成される。
デフォルトの設定
scss/_variables.scss 内 color-variables の設定$blue: #0d6efd;
$indigo: #6610f2;
$purple: #6f42c1;
$pink: #d63384;
$red: #dc3545;
$orange: #fd7e14;
$yellow: #ffc107;
$green: #198754;
$teal: #20c997;
$cyan: #0dcaf0;
scss/_variables.scss 内 theme-color-variables の設定$primary: $blue;
$secondary: $gray-600;
$success: $green;
$info: $cyan;
$warning: $yellow;
$danger: $red;
$light: $gray-100;
$dark: $gray-900;
グレースケールカラーも利用できるが、ユーティリティの生成にはサブセットのみを使用。
scss/_variables.scss 内 gray-color-variables の設定$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;
.text-*-emphasis ユーティリティのライトモードとダークモードの色を設定するための変数:
scss/_variables.scss 内 theme-text-variables の設定$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;
scss/_variables-dark.scss 内 theme-text-dark-variables の設定$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 maps)v5.2.0設定変更、v5.3.0マップから名称変更
次に、テーマカラーがSassマップに配置されるので、それをループして、ユーティリティやコンポーネント修飾子などが生成できる。
デフォルトの設定
scss/_variables.scss 内 theme-colors-map の設定$theme-colors: (
"primary": $primary,
"secondary": $secondary,
"success": $success,
"info": $info,
"warning": $warning,
"danger": $danger,
"light": $light,
"dark": $dark
);
グレースケールカラーは、Sassマップとしても利用できる。このマップは、ユーティリティの生成には使用されない。
scss/_variables.scss 内 gray-colors-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マップから生成される:
scss/_maps.scss 内 theme-colors-rgb の設定$theme-colors-rgb: map-loop($theme-colors, to-rgb, "$value");
カラーの不透明度は、ユーティリティAPIによって消費される独自のマップを使用して構築される:
scss/_maps.scss 内 utilities-text-colors の設定$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マップとしても利用可能:
scss/_maps.scss 内 theme-text-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,
);
scss/_maps.scss 内 theme-text-dark-map の設定$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,
);
【変更履歴】
- 【v5.2.0】
- RGBカラーと不透明度のマップの設定が
scss/_variables.scssから新設のscss/_maps.scssに変更
- RGBカラーと不透明度のマップの設定が
SassユーティリティAPI(Sass utilities API)v5.3.0ユーティリティAPIより名称変更
カラーユーティリティは、scss/_utilities.scss のユーティリティAPIで宣言されている。ユーティリティAPIの使用方法はこちらに掲載。
デフォルトの設定
scss/_utilities.scss 内 utils-color の設定"color": (
property: color,
class: text,
local-vars: (
"text-opacity": 1
),
values: map-merge(
$utilities-text-colors,
(
"muted": var(--#{$prefix}secondary-color), // 非推奨
"black-50": rgba($black, .5), // 非推奨
"white-50": rgba($white, .5), // 非推奨
"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
),