1. 弹性盒子与网格
  2. 网格模板列

弹性盒子与网格

grid-template-columns

用于指定网格布局中的列的工具类。

ClassStyles
grid-cols-<number>
grid-template-columns: repeat(<number>, minmax(0, 1fr));
grid-cols-none
grid-template-columns: none;
grid-cols-subgrid
grid-template-columns: subgrid;
grid-cols-[<value>]
grid-template-columns: <value>;
grid-cols-(<custom-property>)
grid-template-columns: var(<custom-property>);

示例

指定网格列

使用 grid-cols-<number> 工具类,如 grid-cols-2grid-cols-4 来创建具有 n 个等大小列的网格:

01
02
03
04
05
06
07
08
09
<div class="grid grid-cols-4 gap-4">
<div>01</div>
<!-- ... -->
<div>09</div>
</div>

实现子网格

使用 grid-cols-subgrid 工具类来采用项目父元素定义的列轨道:

01
02
03
04
05
06
<div class="grid grid-cols-4 gap-4">
<div>01</div>
<!-- ... -->
<div>05</div>
<div class="col-span-3 grid grid-cols-subgrid gap-4">
<div class="col-start-2">06</div>
</div>
</div>

使用自定义值

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

<div class="grid-cols-[200px_minmax(900px,_1fr)_100px] ...">
<!-- ... -->
</div>

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

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

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

响应式设计

Prefix a grid-template-columns utility with a breakpoint variant like md: to only apply the utility at medium screen sizes and above:

<div class="grid grid-cols-1 md:grid-cols-6 ...">
<!-- ... -->
</div>

Learn more about using variants in the variants documentation.