间距
Bootstrap 包含各种速记响应式边距、填充和间隙实用程序类,以修改元素的外观。
边距和填充
使用速记类为元素或其子集分配响应友好的margin
或padding
值。包括对单个属性、所有属性以及垂直和水平属性的支持。类是从默认 Sass 映射构建的,范围从.25rem
到3rem
。
符号
适用于所有断点的间距实用程序,从xs
到xxl
,其中没有断点缩写。这是因为这些类从min-width: 0
开始应用,因此不受媒体查询的约束。但是,其余断点确实包含断点缩写。
类使用以下格式命名{property}{sides}-{size}
,用于xs
和{property}{sides}-{breakpoint}-{size}
,用于sm
、md
、lg
、xl
和xxl
。
其中属性之一
m
- 用于设置margin
的类p
- 用于设置padding
的类
其中边之一
t
- 用于设置margin-top
或padding-top
的类b
- 用于设置margin-bottom
或padding-bottom
的类s
- (开始)用于在 LTR 中设置margin-left
或padding-left
的类,在 RTL 中设置margin-right
或padding-right
e
- (结束)用于在 LTR 中设置margin-right
或padding-right
的类,在 RTL 中设置margin-left
或padding-left
x
- 用于设置*-left
和*-right
的类y
- 用于设置*-top
和*-bottom
的类- blank - 适用于在元素的全部 4 个边上设置
margin
或padding
的类
其中 size 是以下之一
0
- 适用于通过将其设置为0
来消除margin
或padding
的类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 还包括一个 .mx-auto
类,用于水平居中固定宽度的块级内容,即设置了 display: block
和 width
的内容,方法是将水平边距设置为 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
实用程序。这可以避免在网格或 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
)。没有 .gap-auto
实用程序类,因为它实际上与 .gap-0
相同。
row-gap
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
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
),