Skip to content

Columns 列

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

ClassProperties

基本用法

根据列数添加

使用 columns-{count} 实用程序来设置元素内应创建的列数。列宽将自动调整以适应该数量。

html
<div class="columns-3 ...">
  <img class="w-full aspect-video ..." src="..." />
  <img class="w-full aspect-square ..." src="..." />
  <!-- ... -->
</div>

根据列宽添加

使用 columns-{width} 实用程序来设置元素内内容的理想列宽,列数(计数)会自动调整以适应该数值。

这个“T 恤”比例尺与最大宽度比例尺相同,另外还加上了 2xs3xs ,因为可能希望使用较小的列。

👆 调整示例以查看预期行为

html
<div class="columns-3xs ...">
  <img class="w-full aspect-video ..." src="..." />
  <img class="w-full aspect-square ..." src="..." />
  <!-- ... -->
</div>

设置列间距

要指定列之间的宽度,您可以使用 gap-x 实用工具:

html
<div class="gap-8 columns-3 ...">
  <img class="w-full h-full aspect-video ..." src="..." />
  <img class="w-full h-full aspect-square ..." src="..." />
  <!-- ... -->
</div>

有条件地应用

悬停、焦点和其他状态

Tailwind 允许您使用变体修饰符在不同状态下有条件地应用实用程序类。例如,使用 hover:columns-3 仅在悬停时应用 columns-3 实用程序。

html
<div class="columns-2 hover:columns-3">
  <!-- ... -->
</div>

要查看所有可用状态修饰符的完整列表,请查看悬停、焦点和其他状态文档。

断点和媒体查询

您还可以使用变体修饰符来针对响应式断点、深色模式、prefers-reduced-motion 等媒体查询进行定位。例如,使用 md:columns-3 仅在中等屏幕尺寸及以上应用 columns-3 实用程序。

html
<div class="columns-2 md:columns-3">
  <!-- ... -->
</div>

要了解更多信息,请查看有关响应式设计深色模式其他媒体查询修饰符的文档。

使用自定义值

定制您的主题

默认情况下,Tailwind 提供了从 1-12 开始的列计数比例尺,以及从 3xs-7xl 开始的列 T 恤比例尺。您可以通过编辑您的 tailwind.config.js 文件中的 theme.columnstheme.extend.columns 来自定义这些值。

js
module.exports = {
  theme: {
    extend: {
      columns: {
        '4xs': '14rem',
      }
    },
  }
}

了解有关在主题自定义文档中自定义默认主题的更多信息。

任意值

如果您需要使用一个与主题无关的 columns 值,可以使用方括号来根据任意值动态生成属性。

html
<div class="columns-[10rem]">
  <!-- ... -->
</div>

任意值文档中了解更多关于任意值支持的信息。

Released under the MIT License.