| Class | Styles |
|---|---|
| .gap-0 | gap: 0; |
| .gap-1 | gap: 0.25rem; |
| .gap-2 | gap: 0.375rem; |
| .gap-3 | gap: 0.5rem; |
| .gap-4 | gap: 0.75rem; |
| .gap-5 | gap: 1rem; |
| .gap-6 | gap: 1.25rem; |
| .gap-7 | gap: 1.5rem; |
| .gap-8 | gap: 1.75rem; |
| .gap-9 | gap: 2rem; |
| .gap-10 | gap: 2.25rem; |
| .gap-11 | gap: 2.5rem; |
| .gap-12 | gap: 3rem; |
| .row-gap-0 | row-gap: 0; |
| .row-gap-1 | row-gap: 0.25rem; |
| .row-gap-2 | row-gap: 0.375rem; |
| .row-gap-3 | row-gap: 0.5rem; |
| .row-gap-4 | row-gap: 0.75rem; |
| .row-gap-5 | row-gap: 1rem; |
| .row-gap-6 | row-gap: 1.25rem; |
| .row-gap-7 | row-gap: 1.5rem; |
| .row-gap-8 | row-gap: 1.75rem; |
| .row-gap-9 | row-gap: 2rem; |
| .row-gap-10 | row-gap: 2.25rem; |
| .row-gap-11 | row-gap: 2.5rem; |
| .row-gap-12 | row-gap: 3rem; |
| .column-gap-0 | column-gap: 0; |
| .column-gap-1 | column-gap: 0.25rem; |
| .column-gap-2 | column-gap: 0.375rem; |
| .column-gap-3 | column-gap: 0.5rem; |
| .column-gap-4 | column-gap: 0.75rem; |
| .column-gap-5 | column-gap: 1rem; |
| .column-gap-6 | column-gap: 1.25rem; |
| .column-gap-7 | column-gap: 1.5rem; |
| .column-gap-8 | column-gap: 1.75rem; |
| .column-gap-9 | column-gap: 2rem; |
| .column-gap-10 | column-gap: 2.25rem; |
| .column-gap-11 | column-gap: 2.5rem; |
| .column-gap-12 | column-gap: 3rem; |
概述
使用间隙工具类控制 flexbox 和 grid 布局中子元素之间的空间。间隙工具类基于默认的 Sass map 构建,范围从 0 到 3rem(.gap-0 到 .gap-12)。使用 .gap-3 和 .gap-5 等工具类控制所有子元素之间的间隙:
<div class="d-flex gap-3">...</div>
<div class="d-flex gap-5">...</div>
对于 flexbox 栅格的 .row,请改用 .g-*、.gx-* 和 .gy-*。这些类还会更新列宽所用的 gutter 变量。参见 Gutters。
支持说明: 间隙工具类适用于 CSS Grid 和 Flexbox 布局。它们是用于控制容器内子元素间距的、替代外边距工具类的现代方案。
表示法
Gap utilities that apply to all breakpoints, from xs to 2xl, have no breakpoint prefix in them. This is because those classes are applied from min-width: 0 and up, and thus are not bound by a media query. The remaining breakpoints, however, do include a breakpoint prefix.
The classes are named using the format {property}-{size} for xs and {breakpoint}:{property}-{size} for sm, md, lg, xl, and 2xl.
Where property is one of:
gap- for classes that setgaprow-gap- for classes that setrow-gapcolumn-gap- for classes that setcolumn-gap
Where size is one of:
0- for classes that eliminate thegapby setting it to01- (by default) for classes that set thegapto$spacer * .252- (by default) for classes that set thegapto$spacer * .3753- (by default) for classes that set thegapto$spacer * .54- (by default) for classes that set thegapto$spacer * .755- (by default) for classes that set thegapto$spacer * 16- (by default) for classes that set thegapto$spacer * 1.257- (by default) for classes that set thegapto$spacer * 1.58- (by default) for classes that set thegapto$spacer * 1.759- (by default) for classes that set thegapto$spacer * 210- (by default) for classes that set thegapto$spacer * 2.2511- (by default) for classes that set thegapto$spacer * 2.512- (by default) for classes that set thegapto$spacer * 3
(You can add more sizes by adding entries to the $spacers Sass map variable.)
示例
Flexbox 间隙
使用 gap 工具类控制 flexbox 容器中子元素之间的空间:
<div class="d-flex gap-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Grid 间隙
间隙工具类同样适用于 CSS Grid 布局:
<div class="d-grid gap-3" style="grid-template-columns: 1fr 1fr;">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
</div>
行间隙和列间隙
使用 row-gap 和 column-gap 工具类分别控制行与行、列与列之间的空间:
<div class="d-grid row-gap-4 column-gap-2" style="grid-template-columns: 1fr 1fr 1fr;">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
<div>Item 6</div>
</div>
带换行的 Flexbox
你还可以在可换行的 flexbox 布局中使用 row-gap 和 column-gap 工具类:
<div class="d-flex flex-wrap row-gap-3 column-gap-2">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
</div>
响应式
所有间隙工具类都是响应式的,并包含所有断点。
.gap-0through.gap-12.sm:gap-0through.sm:gap-12.md:gap-0through.md:gap-12.lg:gap-0through.lg:gap-12.xl:gap-0through.xl:gap-12.2xl:gap-0through.2xl:gap-12
.row-gap-0through.row-gap-12.sm:row-gap-0through.sm:row-gap-12.md:row-gap-0through.md:row-gap-12.lg:row-gap-0through.lg:row-gap-12.xl:row-gap-0through.xl:row-gap-12.2xl:row-gap-0through.2xl:row-gap-12
.column-gap-0through.column-gap-12.sm:column-gap-0through.sm:column-gap-12.md:column-gap-0through.md:column-gap-12.lg:column-gap-0through.lg:column-gap-12.xl:column-gap-0through.xl:column-gap-12.2xl:column-gap-0through.2xl:column-gap-12
CSS
Sass 工具类 API
间隙工具类在我们的工具类 API 中于 scss/_utilities.scss 内声明。了解如何使用工具类 API。
"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
),