间距

在非 flex 和非 grid 布局中控制元素直接子元素之间的外边距。

ClassStyles
.space-x-0margin-inline-end: 0;
.space-x-1margin-inline-end: 0.25rem;
.space-x-2margin-inline-end: 0.375rem;
.space-x-3margin-inline-end: 0.5rem;
.space-x-4margin-inline-end: 0.75rem;
.space-x-5margin-inline-end: 1rem;
.space-x-6margin-inline-end: 1.25rem;
.space-x-7margin-inline-end: 1.5rem;
.space-x-8margin-inline-end: 1.75rem;
.space-x-9margin-inline-end: 2rem;
.space-x-10margin-inline-end: 2.25rem;
.space-x-11margin-inline-end: 2.5rem;
.space-x-12margin-inline-end: 3rem;
.space-y-0margin-block-end: 0;
.space-y-1margin-block-end: 0.25rem;
.space-y-2margin-block-end: 0.375rem;
.space-y-3margin-block-end: 0.5rem;
.space-y-4margin-block-end: 0.75rem;
.space-y-5margin-block-end: 1rem;
.space-y-6margin-block-end: 1.25rem;
.space-y-7margin-block-end: 1.5rem;
.space-y-8margin-block-end: 1.75rem;
.space-y-9margin-block-end: 2rem;
.space-y-10margin-block-end: 2.25rem;
.space-y-11margin-block-end: 2.5rem;
.space-y-12margin-block-end: 3rem;

示例

与需要 flexbox 或 grid 的间隙工具类不同,间距工具类通过为子元素应用外边距,可作用于任何父元素。

Item 1
Item 2
Item 3
HTML

<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
HTML
          <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
HTML
          <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() 包裹器将特异性保持为零,因此你可以轻松地使用标准外边距工具类覆盖单个子元素。

CSS
          :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
),