1. 弹性盒子与网格
  2. 间隙

弹性盒子与网格

gap

用于控制网格和弹性盒子项目之间间隙的工具类。

ClassStyles
gap-<number>
gap: calc(var(--spacing) * <value>);
gap-(<custom-property>)
gap: var(<custom-property>);
gap-[<value>]
gap: <value>;
gap-x-<number>
column-gap: calc(var(--spacing) * <value>);
gap-x-(<custom-property>)
column-gap: var(<custom-property>);
gap-x-[<value>]
column-gap: <value>;
gap-y-<number>
row-gap: calc(var(--spacing) * <value>);
gap-y-(<custom-property>)
row-gap: var(<custom-property>);
gap-y-[<value>]
row-gap: <value>;

示例

基本示例

使用 gap-<number> 工具类,如 gap-2gap-4 来改变网格和弹性盒子布局中行与列之间的间隙:

01
02
03
04
<div class="grid grid-cols-2 gap-4">
<div>01</div>
<div>02</div>
<div>03</div>
<div>04</div>
</div>

独立更改行和列间隙

使用 gap-x-<number>gap-y-<number> 工具类,如 gap-x-8gap-y-4 来独立更改列和行之间的间隙:

01
02
03
04
05
06
<div class="grid grid-cols-3 gap-x-8 gap-y-4">
<div>01</div>
<div>02</div>
<div>03</div>
<div>04</div>
<div>05</div>
<div>06</div>
</div>

使用自定义值

Use utilities like gap-[<value>],gap-x-[<value>], and gap-y-[<value>] to set the gap based on a completely custom value:

<div class="gap-[10vw] ...">
<!-- ... -->
</div>

For CSS variables, you can also use the gap-(<custom-property>) syntax:

<div class="gap-(--my-gap) ...">
<!-- ... -->
</div>

This is just a shorthand for gap-[var(<custom-property>)] that adds the var() function for you automatically.

响应式设计

Prefix gap,column-gap, and row-gap utilities with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

<div class="grid gap-4 md:gap-6 ...">
<!-- ... -->
</div>

Learn more about using variants in the variants documentation.