背景色(Background Color)v5.0.0-alpha1スタイル変更、クラス追加
文字色のクラスと同様、要素の背景色を任意の背景色クラスで簡単に設定可能。背景色ユーティリティでは color
を設定していないので、場合によっては .text-{color}
ユーティリティを使用する必要がある。
背景色の種類
●段落全体に背景色を設定する 色が見やすいように空白や枠を設けています
.bg-primary
.bg-secondary
.bg-success
.bg-info
.bg-warning
.bg-danger
.bg-light
.bg-dark
.bg-white
.bg-body
v5.0.0-alpha1追加(※<body>
の背景色と同じ配色).bg-transparent
●文章の一部分に背景色を設定する
Primary:.bg-primary
標準の文字色と白色の文字の場合。さらにリンクを貼った(←リンクを貼った)場合。
Secondary:.bg-secondary
標準の文字色と白色の文字の場合。さらにリンクを貼った(←リンクを貼った)場合。
Success:.bg-success
標準の文字色と白色の文字の場合。さらにリンクを貼った(←リンクを貼った)場合。
Info:.bg-info
標準の文字色と白色の文字の場合。さらにリンクを貼った(←リンクを貼った)場合。
Warning:.bg-warning
標準の文字色と白色の文字の場合。さらにリンクを貼った(←リンクを貼った)場合。
Danger:.bg-danger
標準の文字色と白色の文字の場合。さらにリンクを貼った(←リンクを貼った)場合。
Light:.bg-light
標準の文字色と白色の文字(←白色の文字)の場合。さらにリンクを貼った場合。
Dark:.bg-dark
標準の文字色(←標準の文字色)と白色の文字の場合。さらにリンクを貼った場合。
White:.bg-white
標準の文字色と白色の文字(←白色の文字)の場合。さらにリンクを貼った場合。
設定例
段落全体に背景色を設定する<div class="p-3 mb-2 bg-primary text-white">...</div>
文章の一部分に背景色を設定する<p><span class="bg-primary">標準の文字色</span>と<span class="bg-primary text-white">白色の文字</span>の場合。さらに<a href="#" class="bg-primary">リンクを貼った</a>場合。
【設定】
<div>
や<span>
などの要素に.bg-{color}
(上記の「背景色の種類」から選択)を入れる{color}
は、primary
,secondary
,success
,info
,warning
,danger
,light
,dark
,white
,body
,transparent
のいずれかを選択- 背景色によっては、
.text-white
など見やすい色の文字を追加する必要あり .bg-body
の色を変更する場合は、scss/_variables.scss
内の$body-bg
の値を変更して再コンパイルする
【変更履歴】
- 【v5.0.0-alpha1】
.bg-body
が追加<a>
タグに.bg-{color}
を入れてもホバー時に濃いめに強調されなくなった
定義済みの文字色と背景色の配色
.bg-primary |
.bg-secondary |
.bg-success |
.bg-info |
.bg-warning |
.bg-danger |
.bg-light |
.bg-dark |
.bg-white |
.bg-body |
|
.text-primary |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-secondary |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-success |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-info |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-warning |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-danger |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-light |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-dark |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-body |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-muted |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-white |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-black-50 |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
.text-white-50 |
文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 | 文字 |
背景色のグラデーション化(Background gradient)v5.0.0-alpha1設定変更
.bg-gradient
クラスを追加することにより、線形グラデーションが背景画像として背景に追加される。このグラデーションは、下にフェードアウトする半透明の白で始まる。
カスタムCSSにグラデーションが必要なら、background-image: var(--bs-gradient);
を追加する。
背景色の種類
●段落全体に背景色を設定する 色が見やすいように空白を設けています
.bg-primary.bg-gradient
.bg-secondary.bg-gradient
.bg-success.bg-gradient
.bg-danger.bg-gradient
.bg-warning.bg-gradient
.bg-info.bg-gradient
.bg-light.bg-gradient
.bg-dark.bg-gradient
.bg-transparent.bg-gradient
設定例
<div class="p-3 mb-2 bg-primary bg-gradient text-white">...</div>
【設定】
.bg-{themecolor}
に.bg-gradient
を追加- 背景色によっては、
.text-white
など見やすい色の文字を追加する必要あり
【変更履歴】
- 【v5.0.0-alpha1】
.bg-gradient-{themecolor}
⇒.bg-{themecolor}.bg-gradient
(.bg-gradient-{themecolor}
は廃止、scss/_variables.scss
内の$enable-gradients
の設定変更も不要に)
Sass v5.0.0-beta2追加
以下のSass機能に加えて、カラーなどに含まれているCSSカスタムプロパティ(別名CSS変数)についても読んでみること。
変数(Variables)
ほとんどの background-color
ユーティリティは、一般的なカラーパレット変数から再割り当てされたテーマカラーによって生成される。
color-variablesの設定$blue: #0d6efd;
$indigo: #6610f2;
$purple: #6f42c1;
$pink: #d63384;
$red: #dc3545;
$orange: #fd7e14;
$yellow: #ffc107;
$green: #198754;
$teal: #20c997;
$cyan: #0dcaf0;
theme-color-variablesの設定$primary: $blue;
$secondary: $gray-600;
$success: $green;
$info: $cyan;
$warning: $yellow;
$danger: $red;
$light: $gray-100;
$dark: $gray-900;
variable-gradientの設定$gradient: linear-gradient(180deg, rgba($white, .15), rgba($white, 0));
グレースケールカラーも利用可能だが、ユーティリティの生成にはサブセットのみを使用。
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;
マップ(Map)
次に、テーマカラーがSassマップに配置されるので、それをループして、ユーティリティやコンポーネント修飾子などが生成可能。
theme-colors-mapの設定$theme-colors: (
"primary": $primary,
"secondary": $secondary,
"success": $success,
"info": $info,
"warning": $warning,
"danger": $danger,
"light": $light,
"dark": $dark
);
グレースケールカラーは、Sassマップとしても利用可能。このマップは、ユーティリティの生成には使用されない。
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
);
mixin(Mixins)
背景ユーティリティの生成にmixinは使用されないが、独自のグラデーションを作成したい他の状況のために、いくつかの追加のmixinがある。
gradient-bg-mixinの設定@mixin gradient-bg($color: null) {
background-color: $color;
@if $enable-gradients {
background-image: var(--#{$variable-prefix}gradient);
}
}
gradient-mixinsの設定// 左から右への水平方向のグラデーション
//
// 各カラーストップの色と位置を指定して、開始と終了の2つのカラーストップを作成。
@mixin gradient-x($start-color: $gray-700, $end-color: $gray-800, $start-percent: 0%, $end-percent: 100%) {
background-image: linear-gradient(to right, $start-color $start-percent, $end-color $end-percent);
}
// 上から下への垂直方向のグラデーション
//
// 各カラーストップの色と位置を指定して、開始と終了の2つのカラーストップを作成。
@mixin gradient-y($start-color: $gray-700, $end-color: $gray-800, $start-percent: null, $end-percent: null) {
background-image: linear-gradient(to bottom, $start-color $start-percent, $end-color $end-percent);
}
@mixin gradient-directional($start-color: $gray-700, $end-color: $gray-800, $deg: 45deg) {
background-image: linear-gradient($deg, $start-color, $end-color);
}
@mixin gradient-x-three-colors($start-color: $blue, $mid-color: $purple, $color-stop: 50%, $end-color: $red) {
background-image: linear-gradient(to right, $start-color, $mid-color $color-stop, $end-color);
}
@mixin gradient-y-three-colors($start-color: $blue, $mid-color: $purple, $color-stop: 50%, $end-color: $red) {
background-image: linear-gradient($start-color, $mid-color $color-stop, $end-color);
}
@mixin gradient-radial($inner-color: $gray-700, $outer-color: $gray-800) {
background-image: radial-gradient(circle, $inner-color, $outer-color);
}
@mixin gradient-striped($color: rgba($white, .15), $angle: 45deg) {
background-image: linear-gradient($angle, $color 25%, transparent 25%, transparent 50%, $color 50%, $color 75%, transparent 75%, transparent);
}
ユーティリティAPI(Utilities API)
背景ユーティリティは、scss/_utilities.scss
のユーティリティAPIで宣言されている。ユーティリティAPIの使用方法はここで学習。
utils-bg-colorの設定"background-color": (
property: background-color,
class: bg,
values: map-merge(
$theme-colors,
(
"body": $body-bg,
"white": $white,
"transparent": transparent
)
)
),