スペーシング
Bootstrapには、要素の外観を変更するための、幅広いレスポンシブなマージン、パディング、ギャップのショートハンドユーティリティクラスが含まれています。
マージンとパディング
レスポンシブ対応のmargin
またはpadding
値を、要素またはその側面のサブセットにショートハンドクラスで割り当てます。個々のプロパティ、すべてのプロパティ、垂直方向と水平方向のプロパティのサポートが含まれています。クラスは、.25rem
から3rem
までのデフォルトのSassマップから構築されます。
表記法
xs
からxxl
までのすべてのブレークポイントに適用されるスペーシングユーティリティには、ブレークポイントの省略形がありません。これは、これらのクラスがmin-width: 0
以上から適用され、メディアクエリにバインドされないためです。ただし、残りのブレークポイントにはブレークポイントの省略形が含まれています。
クラスの名前は、xs
の場合は{property}{sides}-{size}
、sm
、md
、lg
、xl
、xxl
の場合は{property}{sides}-{breakpoint}-{size}
の形式を使用して付けられます。
ここで、*property*は以下のいずれかです。
m
-margin
を設定するクラスの場合p
-padding
を設定するクラスの場合
ここで、*sides*は以下のいずれかです。
t
-margin-top
またはpadding-top
を設定するクラスの場合b
-margin-bottom
またはpadding-bottom
を設定するクラスの場合s
- (start) LTRではmargin-left
またはpadding-left
、RTLではmargin-right
またはpadding-right
を設定するクラスの場合e
- (end) LTRではmargin-right
またはpadding-right
、RTLではmargin-left
またはpadding-left
を設定するクラスの場合x
-*-left
と*-right
の両方を設定するクラスの場合y
-*-top
と*-bottom
の両方を設定するクラスの場合- 空白 - 要素の4つの側面すべてに
margin
またはpadding
を設定するクラスの場合
ここで、*size*は以下のいずれかです。
0
-margin
またはpadding
を0
に設定して削除するクラスの場合1
- (デフォルト)margin
またはpadding
を$spacer * .25
に設定するクラスの場合2
- (デフォルト)margin
またはpadding
を$spacer * .5
に設定するクラスの場合3
- (デフォルト)margin
またはpadding
を$spacer
に設定するクラスの場合4
- (デフォルト)margin
またはpadding
を$spacer * 1.5
に設定するクラスの場合5
- (デフォルト)margin
または`padding`を`$spacer * 3`に設定するクラスの場合auto
-margin
を`auto`に設定するクラスの場合
($spacers
Sassマップ変数にエントリを追加することで、さらにサイズを追加できます。)
例
これらのクラスの代表的な例を次に示します。
.mt-0 {
margin-top: 0 !important;
}
.ms-1 {
margin-left: ($spacer * .25) !important;
}
.px-2 {
padding-left: ($spacer * .5) !important;
padding-right: ($spacer * .5) !important;
}
.p-3 {
padding: $spacer !important;
}
水平方向の中央揃え
さらに、Bootstrapには、水平方向のマージンをauto
に設定することにより、固定幅のブロックレベルのコンテンツ(つまり、display: block
とwidth
が設定されているコンテンツ)を水平方向に中央揃えするための.mx-auto
クラスも含まれています。
<div class="mx-auto p-2" style="width: 200px;">
Centered element
</div>
負のマージン
CSSでは、`margin`プロパティは負の値を使用できます(`padding`は使用できません)。これらの負のマージンは**デフォルトで無効**になっていますが、Sassで`$enable-negative-margins: true`を設定することで有効にできます。
構文はデフォルトの正のマージンユーティリティとほぼ同じですが、要求されたサイズの前に`n`が追加されています。`mt-1`の反対のクラスの例を次に示します。
.mt-n1 {
margin-top: -0.25rem !important;
}
ギャップ
display: grid
またはdisplay: flex
を使用する場合、親要素で`gap`ユーティリティを使用できます。これにより、グリッドまたはフレックスコンテナの個々の子にマージンユーティリティを追加する手間を省くことができます。ギャップユーティリティはデフォルトでレスポンシブであり、`$spacers` Sassマップに基づいて、ユーティリティAPIを介して生成されます。
<div class="grid gap-3">
<div class="p-2 g-col-6">Grid item 1</div>
<div class="p-2 g-col-6">Grid item 2</div>
<div class="p-2 g-col-6">Grid item 3</div>
<div class="p-2 g-col-6">Grid item 4</div>
</div>
サポートには、Bootstrapのすべてのグリッドブレークポイントのレスポンシブオプションと、`$spacers`マップ(0〜5)の6つのサイズが含まれています。`gap-auto`ユーティリティクラスはありません。これは、事実上`gap-0`と同じであるためです。
行ギャップ
`row-gap`は、指定されたコンテナの子アイテム間の垂直方向のスペースを設定します。
<div class="grid gap-0 row-gap-3">
<div class="p-2 g-col-6">Grid item 1</div>
<div class="p-2 g-col-6">Grid item 2</div>
<div class="p-2 g-col-6">Grid item 3</div>
<div class="p-2 g-col-6">Grid item 4</div>
</div>
列ギャップ
`column-gap`は、指定されたコンテナの子アイテム間の水平方向のスペースを設定します。
<div class="grid gap-0 column-gap-3">
<div class="p-2 g-col-6">Grid item 1</div>
<div class="p-2 g-col-6">Grid item 2</div>
<div class="p-2 g-col-6">Grid item 3</div>
<div class="p-2 g-col-6">Grid item 4</div>
</div>
CSS
Sassマップ
スペーシングユーティリティはSassマップを介して宣言され、ユーティリティAPIで生成されます。
$spacer: 1rem;
$spacers: (
0: 0,
1: $spacer * .25,
2: $spacer * .5,
3: $spacer,
4: $spacer * 1.5,
5: $spacer * 3,
);
SassユーティリティAPI
スペーシングユーティリティは、`scss/_utilities.scss`のユーティリティAPIで宣言されています。ユーティリティAPIの使用方法についてはこちらをご覧ください。
"margin": (
responsive: true,
property: margin,
class: m,
values: map-merge($spacers, (auto: auto))
),
"margin-x": (
responsive: true,
property: margin-right margin-left,
class: mx,
values: map-merge($spacers, (auto: auto))
),
"margin-y": (
responsive: true,
property: margin-top margin-bottom,
class: my,
values: map-merge($spacers, (auto: auto))
),
"margin-top": (
responsive: true,
property: margin-top,
class: mt,
values: map-merge($spacers, (auto: auto))
),
"margin-end": (
responsive: true,
property: margin-right,
class: me,
values: map-merge($spacers, (auto: auto))
),
"margin-bottom": (
responsive: true,
property: margin-bottom,
class: mb,
values: map-merge($spacers, (auto: auto))
),
"margin-start": (
responsive: true,
property: margin-left,
class: ms,
values: map-merge($spacers, (auto: auto))
),
// Negative margin utilities
"negative-margin": (
responsive: true,
property: margin,
class: m,
values: $negative-spacers
),
"negative-margin-x": (
responsive: true,
property: margin-right margin-left,
class: mx,
values: $negative-spacers
),
"negative-margin-y": (
responsive: true,
property: margin-top margin-bottom,
class: my,
values: $negative-spacers
),
"negative-margin-top": (
responsive: true,
property: margin-top,
class: mt,
values: $negative-spacers
),
"negative-margin-end": (
responsive: true,
property: margin-right,
class: me,
values: $negative-spacers
),
"negative-margin-bottom": (
responsive: true,
property: margin-bottom,
class: mb,
values: $negative-spacers
),
"negative-margin-start": (
responsive: true,
property: margin-left,
class: ms,
values: $negative-spacers
),
// Padding utilities
"padding": (
responsive: true,
property: padding,
class: p,
values: $spacers
),
"padding-x": (
responsive: true,
property: padding-right padding-left,
class: px,
values: $spacers
),
"padding-y": (
responsive: true,
property: padding-top padding-bottom,
class: py,
values: $spacers
),
"padding-top": (
responsive: true,
property: padding-top,
class: pt,
values: $spacers
),
"padding-end": (
responsive: true,
property: padding-right,
class: pe,
values: $spacers
),
"padding-bottom": (
responsive: true,
property: padding-bottom,
class: pb,
values: $spacers
),
"padding-start": (
responsive: true,
property: padding-left,
class: ps,
values: $spacers
),
// Gap utility
"gap": (
responsive: true,
property: gap,
class: gap,
values: $spacers
),
"row-gap": (
responsive: true,
property: row-gap,
class: row-gap,
values: $spacers
),
"column-gap": (
responsive: true,
property: column-gap,
class: column-gap,
values: $spacers
),