1. 布局
  2. 多列

布局

用于控制元素内列数的实用工具。

ClassStyles
columns-<number>
columns: <number>;
columns-3xs
columns: var(--container-3xs); /* 16rem (256px) */
columns-2xs
columns: var(--container-2xs); /* 18rem (288px) */
columns-xs
columns: var(--container-xs); /* 20rem (320px) */
columns-sm
columns: var(--container-sm); /* 24rem (384px) */
columns-md
columns: var(--container-md); /* 28rem (448px) */
columns-lg
columns: var(--container-lg); /* 32rem (512px) */
columns-xl
columns: var(--container-xl); /* 36rem (576px) */
columns-2xl
columns: var(--container-2xl); /* 42rem (672px) */
columns-3xl
columns: var(--container-3xl); /* 48rem (768px) */

示例

按数量设置

使用 columns-<number> 实用工具,如 columns-3 来设置元素内应创建的列数:

<div class="columns-3 ...">
<img class="aspect-3/2 ..." src="/img/mountains-1.jpg" />
<img class="aspect-square ..." src="/img/mountains-2.jpg" />
<img class="aspect-square ..." src="/img/mountains-3.jpg" />
<!-- ... -->
</div>

列的宽度将自动调整以满足指定数量的列。

按宽度设置

使用 columns-xscolumns-sm 等实用工具来为元素内的内容设置理想的列宽:

调整示例大小以查看预期行为

<div class="columns-3xs ...">
<img class="aspect-3/2 ..." src="/img/mountains-1.jpg" />
<img class="aspect-square ..." src="/img/mountains-2.jpg" />
<img class="aspect-square ..." src="/img/mountains-3.jpg" />
<!-- ... -->
</div>

设置列宽时,列数会自动调整以确保它们不会变得太窄。

设置列间距

使用 gap-<width> 实用工具来指定列之间的宽度:

<div class="columns-3 gap-8 ...">
<img class="aspect-3/2 ..." src="/img/mountains-1.jpg" />
<img class="aspect-square ..." src="/img/mountains-2.jpg" />
<img class="aspect-square ..." src="/img/mountains-3.jpg" />
<!-- ... -->
</div>

了解有关间距实用工具的更多信息,请参阅 间距文档

使用自定义值

Use the columns-[<value>] syntax to set the columns based on a completely custom value:

<div class="columns-[30vw] ...">
<!-- ... -->
</div>

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

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

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

响应式设计

Prefix a columns utility with a breakpoint variant like sm: to only apply the utility at small screen sizes and above:

调整示例大小以查看预期行为

<div class="columns-2 gap-4 sm:columns-3 sm:gap-8 ...">
<img class="aspect-3/2 ..." src="/img/mountains-1.jpg" />
<img class="aspect-square ..." src="/img/mountains-2.jpg" />
<img class="aspect-square ..." src="/img/mountains-3.jpg" />
<!-- ... -->
</div>

Learn more about using variants in the variants documentation.

自定义主题

Use the --container-* theme variables to customize the 固定宽度列 utilities in your project:

@theme {
--container-4xs: 14rem;
}

Now the columns-4xs utility can be used in your markup:

<div class="columns-4xs">
<!-- ... -->
</div>

Learn more about customizing your theme in the theme documentation.