| Class | Styles |
|---|---|
| .space-x-0 | margin-inline-end: 0; |
| .space-x-1 | margin-inline-end: 0.25rem; |
| .space-x-2 | margin-inline-end: 0.375rem; |
| .space-x-3 | margin-inline-end: 0.5rem; |
| .space-x-4 | margin-inline-end: 0.75rem; |
| .space-x-5 | margin-inline-end: 1rem; |
| .space-x-6 | margin-inline-end: 1.25rem; |
| .space-x-7 | margin-inline-end: 1.5rem; |
| .space-x-8 | margin-inline-end: 1.75rem; |
| .space-x-9 | margin-inline-end: 2rem; |
| .space-x-10 | margin-inline-end: 2.25rem; |
| .space-x-11 | margin-inline-end: 2.5rem; |
| .space-x-12 | margin-inline-end: 3rem; |
| .space-y-0 | margin-block-end: 0; |
| .space-y-1 | margin-block-end: 0.25rem; |
| .space-y-2 | margin-block-end: 0.375rem; |
| .space-y-3 | margin-block-end: 0.5rem; |
| .space-y-4 | margin-block-end: 0.75rem; |
| .space-y-5 | margin-block-end: 1rem; |
| .space-y-6 | margin-block-end: 1.25rem; |
| .space-y-7 | margin-block-end: 1.5rem; |
| .space-y-8 | margin-block-end: 1.75rem; |
| .space-y-9 | margin-block-end: 2rem; |
| .space-y-10 | margin-block-end: 2.25rem; |
| .space-y-11 | margin-block-end: 2.5rem; |
| .space-y-12 | margin-block-end: 3rem; |
示例
与需要 flexbox 或 grid 的间隙工具类不同,间距工具类通过为子元素应用外边距,可作用于任何父元素。
Item 1
Item 2
Item 3
<div class="d-flex space-x-3 p-3 bd-pattern-diagonal rounded-3">
<div class="p-3 bg-subtle-accent fg-accent rounded-3">Item 1</div>
<div class="p-3 bg-subtle-accent fg-accent rounded-3">Item 2</div>
<div class="p-3 bg-subtle-accent fg-accent rounded-3">Item 3</div>
</div>为间距工具类生成的选择器被包裹在 :where() 中,以实现零特异性,从而便于用其他工具类或自定义 CSS 轻松覆盖。
表示法
适用于所有断点(从 xs 到 2xl)的间距工具类没有断点缩写。这是因为这些类从 min-width: 0 开始应用,因此不受媒体查询约束。其余断点则包含断点缩写。
对于 xs,类使用 {property}-{size} 格式命名;对于 sm、md、lg、xl 和 2xl,则使用 {breakpoint}:{property}-{size} 格式。
其中 property 为以下之一:
space-x- 表示通过margin-inline-end设置水平间距的类space-y- 表示通过margin-block-end设置垂直间距的类
其中 size 为以下之一:
0- 表示将间距设为0以消除间距的类1-(默认)表示将间距设为$spacer * .25的类2-(默认)表示将间距设为$spacer * .375的类3-(默认)表示将间距设为$spacer * .5的类4-(默认)表示将间距设为$spacer * .75的类5-(默认)表示将间距设为$spacer * 1的类6-(默认)表示将间距设为$spacer * 1.25的类7-(默认)表示将间距设为$spacer * 1.5的类8-(默认)表示将间距设为$spacer * 1.75的类9-(默认)表示将间距设为$spacer * 2的类10-(默认)表示将间距设为$spacer * 2.25的类11-(默认)表示将间距设为$spacer * 2.5的类12-(默认)表示将间距设为$spacer * 3的类
(你可以通过向 $spacers Sass map 变量添加条目来增加更多尺寸。)
水平间距
使用 space-x-* 工具类控制子元素之间的水平间距:
Item 1
Item 2
Item 3
<div class="d-flex space-x-3">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
垂直间距
使用 space-y-* 工具类控制子元素之间的垂直间距:
Item 1
Item 2
Item 3
<div class="space-y-3">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
工作原理
间距工具类通过选择器 :where(.space-x-3 > :not(:last-child)) 为除最后一个之外的每个直接子元素应用 margin-inline-end 或 margin-block-end。:where() 包裹器将特异性保持为零,因此你可以轻松地使用标准外边距工具类覆盖单个子元素。
:where(.space-x-3 > :not(:last-child)) {
margin-inline-end: .5rem;
}
CSS
Sass 工具类 API
间距工具类在我们的工具类 API 中于 scss/_utilities.scss 内声明。了解如何使用工具类 API。
"space-x": (
responsive: true,
property: margin-inline-end,
class: space-x,
child-selector: "> :not(:last-child)",
values: $spacers
),
"space-y": (
responsive: true,
property: margin-block-end,
class: space-y,
child-selector: "> :not(:last-child)",
values: $spacers
),