1. 弹性盒 & 网格
  2. 网格列开始/结束

Quick reference

Class
Properties
col-autogrid-column: auto;
col-span-1grid-column: span 1 / span 1;
col-span-2grid-column: span 2 / span 2;
col-span-3grid-column: span 3 / span 3;
col-span-4grid-column: span 4 / span 4;
col-span-5grid-column: span 5 / span 5;
col-span-6grid-column: span 6 / span 6;
col-span-7grid-column: span 7 / span 7;
col-span-8grid-column: span 8 / span 8;
col-span-9grid-column: span 9 / span 9;
col-span-10grid-column: span 10 / span 10;
col-span-11grid-column: span 11 / span 11;
col-span-12grid-column: span 12 / span 12;
col-span-fullgrid-column: 1 / -1;
col-start-1grid-column-start: 1;
col-start-2grid-column-start: 2;
col-start-3grid-column-start: 3;
col-start-4grid-column-start: 4;
col-start-5grid-column-start: 5;
col-start-6grid-column-start: 6;
col-start-7grid-column-start: 7;
col-start-8grid-column-start: 8;
col-start-9grid-column-start: 9;
col-start-10grid-column-start: 10;
col-start-11grid-column-start: 11;
col-start-12grid-column-start: 12;
col-start-13grid-column-start: 13;
col-start-autogrid-column-start: auto;
col-end-1grid-column-end: 1;
col-end-2grid-column-end: 2;
col-end-3grid-column-end: 3;
col-end-4grid-column-end: 4;
col-end-5grid-column-end: 5;
col-end-6grid-column-end: 6;
col-end-7grid-column-end: 7;
col-end-8grid-column-end: 8;
col-end-9grid-column-end: 9;
col-end-10grid-column-end: 10;
col-end-11grid-column-end: 11;
col-end-12grid-column-end: 12;
col-end-13grid-column-end: 13;
col-end-autogrid-column-end: auto;

基本用法

¥Basic usage

跨列

¥Spanning columns

使用 col-span-* 工具使元素跨越 n 列。

¥Use the col-span-* utilities to make an element span n columns.

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

起始行和结束行

¥Starting and ending lines

使用 col-start-*col-end-* 工具使元素在第 n 条网格线开始或结束。这些也可以与 col-span-* 工具结合使用以跨越特定数量的列。

¥Use the col-start-* and col-end-* utilities to make an element start or end at the nth grid line. These can also be combined with the col-span-* utilities to span a specific number of columns.

请注意,CSS 网格线从 1 开始,而不是 0,因此 6 列网格中的全角元素将从第 1 行开始到第 7 行结束。

¥Note that CSS grid lines start at 1, not 0, so a full-width element in a 6-column grid would start at line 1 and end at line 7.

01
02
03
04
<div class="grid grid-cols-6 gap-4">
  <div class="col-start-2 col-span-4 ...">01</div>
  <div class="col-start-1 col-end-3 ...">02</div>
  <div class="col-end-7 col-span-2 ...">03</div>
  <div class="col-start-1 col-end-7 ...">04</div>
</div>

有条件地应用

悬停、聚焦和其他状态

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:col-span-6 to only apply the col-span-6 utility on hover.

<div class="col-span-2 hover:col-span-6">
  <!-- ... -->
</div>

For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

断点和媒体查询

You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:col-span-6 to apply the col-span-6 utility at only medium screen sizes and above.

<div class="col-span-2 md:col-span-6">
  <!-- ... -->
</div>

To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.


使用自定义值

¥Using custom values

自定义主题

¥Customizing your theme

默认情况下,Tailwind 包括用于处理最多 12 列的网格的网格列工具。你可以通过自定义 Tailwind 主题配置的 gridColumngridColumnStartgridColumnEnd 部分来更改、添加或删除它们。

¥By default, Tailwind includes grid-column utilities for working with grids with up to 12 columns. You change, add, or remove these by customizing the gridColumn, gridColumnStart, and gridColumnEnd sections of your Tailwind theme config.

要添加新的 col-* 工具,请自定义 Tailwind 主题配置的 gridColumn 部分:

¥To add new col-* utilities, customize the gridColumn section of your Tailwind theme config:

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      gridColumn: {
        'span-16': 'span 16 / span 16',
      }
    }
  }
}

我们在内部将其用于我们的 col-span-* 工具。请注意,由于这直接配置了 grid-column 简写属性,我们直接在值名称中包含单词 span,它不会自动融入类名称中。这意味着你可以自由添加条目来执行你想要的任何操作 - 它们不必只是 span 工具。

¥We use this internally for our col-span-* utilities. Note that since this configures the grid-column shorthand property directly, we include the word span directly in the value name, it’s not baked into the class name automatically. That means you are free to add entries that do whatever you want here — they don’t just have to be span utilities.

要添加新的 col-start-* 工具,请自定义 Tailwind 主题配置的 gridColumnStart 部分:

¥To add new col-start-* utilities, customize the gridColumnStart section of your Tailwind theme config:

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      gridColumnStart: {
        '13': '13',
        '14': '14',
        '15': '15',
        '16': '16',
        '17': '17',
      }
    }
  }
}

要添加新的 col-end-* 工具,请自定义 Tailwind 主题配置的 gridColumnEnd 部分:

¥To add new col-end-* utilities, customize the gridColumnEnd section of your Tailwind theme config:

tailwind.config.js
module.exports = {
  theme: {
    extend: {
      gridColumnEnd: {
        '13': '13',
        '14': '14',
        '15': '15',
        '16': '16',
        '17': '17',
      }
    }
  }
}

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

¥Learn more about customizing the default theme in the theme customization documentation.

任意值

¥Arbitrary values

If you need to use a one-off grid column value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

<div class="col-[16_/_span_16]">
  <!-- ... -->
</div>

Learn more about arbitrary value support in the arbitrary values documentation.