定义网格列
使用 grid-cols-1 到 grid-cols-12 创建多列布局,并使用 grid-cols-none 配置自定义网格。
定义网格列 是 CoddyKit 上的免费 Tailwind CSS Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Tailwind CSS Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Tailwind CSS Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
CSS Grid in Tailwind
CSS Grid is a two-dimensional layout system that lets you place items in rows and columns simultaneously. Tailwind CSS provides utility classes that map directly to CSS Grid properties, so you never have to write display: grid or grid-template-columns by hand. You simply add the right class names to your HTML elements.
<div class="grid">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>Activating Grid Display
To turn an element into a grid container, apply the grid class. Without any column definition, all children stack into a single column by default. This is the starting point for any Tailwind Grid layout — every grid utility you add builds on top of the grid class.
<!-- Single-column grid by default -->
<div class="grid gap-4">
<div class="bg-blue-100 p-4">Card 1</div>
<div class="bg-blue-100 p-4">Card 2</div>
<div class="bg-blue-100 p-4">Card 3</div>
</div>The grid-cols-N Utilities
Tailwind provides grid-cols-1 through grid-cols-12 to divide the container into equal-width columns. Under the hood, grid-cols-3 compiles to grid-template-columns: repeat(3, minmax(0, 1fr)). This means all columns share the available width equally, making it perfect for card grids and feature sections.
<!-- 3-column equal grid -->
<div class="grid grid-cols-3 gap-4">
<div class="bg-green-100 p-4">Col 1</div>
<div class="bg-green-100 p-4">Col 2</div>
<div class="bg-green-100 p-4">Col 3</div>
<div class="bg-green-100 p-4">Col 4</div>
<div class="bg-green-100 p-4">Col 5</div>
<div class="bg-green-100 p-4">Col 6</div>
</div>Common Column Counts
Different column counts suit different use cases. A 2-column grid works well for split-content sections, 3 columns for feature rows, and 4 or 6 columns for product or image galleries. Tailwind supports every integer from 1 to 12, giving you the same range as Bootstrap's grid system but without the row wrappers.
<!-- 2-col layout -->
<div class="grid grid-cols-2 gap-6">
<div class="bg-purple-100 p-6">Left panel</div>
<div class="bg-purple-100 p-6">Right panel</div>
</div>
<!-- 4-col product grid -->
<div class="grid grid-cols-4 gap-4">
<div class="bg-yellow-100 p-4">Product</div>
<div class="bg-yellow-100 p-4">Product</div>
<div class="bg-yellow-100 p-4">Product</div>
<div class="bg-yellow-100 p-4">Product</div>
</div>The 12-Column Grid System
The 12-column grid is a classic design convention because 12 is divisible by 1, 2, 3, 4, and 6, giving you lots of layout flexibility. With Tailwind's grid-cols-12, each child occupies one column by default. You then use col-span-* on individual children to make them span multiple columns — which we will explore in the next lesson.
<!-- 12-column base -->
<div class="grid grid-cols-12 gap-2">
<!-- Each child spans 1 col unless told otherwise -->
<div class="bg-red-100 p-2">1</div>
<div class="bg-red-100 p-2">2</div>
<div class="bg-red-100 p-2">3</div>
<!-- ...up to 12 -->
</div>grid-cols-none for Custom Grids
When the predefined 1–12 presets are not enough, use grid-cols-none to remove any column template, then define your own with grid-cols-[...] arbitrary values. The bracket notation lets you write any valid CSS grid-template-columns value directly inside a class name, giving you infinite flexibility without leaving HTML.
<!-- Custom column widths with arbitrary values -->
<div class="grid grid-cols-[200px_1fr_2fr] gap-4">
<div class="bg-indigo-100 p-4">Fixed 200px</div>
<div class="bg-indigo-100 p-4">1 fraction</div>
<div class="bg-indigo-100 p-4">2 fractions</div>
</div>Subgrid With Tailwind
Modern browsers support the subgrid keyword, which lets a nested grid inherit its parent's column tracks. Tailwind v3.4+ includes grid-cols-subgrid for this purpose. Subgrid is powerful for aligning form labels inside card components that sit in a parent grid — each card's internal elements align with the overall grid lines.
<!-- Parent grid with 3 columns -->
<div class="grid grid-cols-3 gap-4">
<!-- Child that spans 2 cols and uses subgrid internally -->
<div class="col-span-2 grid grid-cols-subgrid gap-2">
<div class="bg-teal-100 p-3">A</div>
<div class="bg-teal-100 p-3">B</div>
</div>
<div class="bg-teal-200 p-3">C</div>
</div>Combining Grid With Gap
The gap-* utilities add consistent spacing between grid cells. Use gap-4 for uniform spacing in both directions, gap-x-4 for horizontal-only gaps, and gap-y-6 for vertical-only gaps. Unlike margins, gap does not add extra space on the outside edges of the grid, keeping your layout clean and predictable.
<!-- Different horizontal and vertical gaps -->
<div class="grid grid-cols-3 gap-x-8 gap-y-4">
<div class="bg-orange-100 p-4">A</div>
<div class="bg-orange-100 p-4">B</div>
<div class="bg-orange-100 p-4">C</div>
<div class="bg-orange-100 p-4">D</div>
<div class="bg-orange-100 p-4">E</div>
<div class="bg-orange-100 p-4">F</div>
</div>Responsive Column Definitions
In most real-world layouts you want fewer columns on small screens and more on larger ones. Tailwind's responsive prefixes make this trivial: grid-cols-1 md:grid-cols-2 lg:grid-cols-4 stacks items on mobile, shows two columns on tablets, and four on desktops. This mobile-first approach means the unprefixed class is the baseline, and prefixed classes override it at larger breakpoints.
<!-- Mobile: 1 col, tablet: 2 cols, desktop: 4 cols -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
<div class="bg-sky-100 p-6 rounded-lg">Card A</div>
<div class="bg-sky-100 p-6 rounded-lg">Card B</div>
<div class="bg-sky-100 p-6 rounded-lg">Card C</div>
<div class="bg-sky-100 p-6 rounded-lg">Card D</div>
</div>Auto-Fill vs Auto-Fit Columns
CSS Grid's auto-fill and auto-fit keywords create dynamic column counts based on available space. With Tailwind's arbitrary value syntax you can write grid-cols-[repeat(auto-fill,minmax(200px,1fr))] to automatically add columns as space allows. auto-fill keeps empty tracks, while auto-fit collapses them — both create truly fluid grids without media queries.
<!-- Auto-filling card grid: min 250px per card -->
<div class="grid gap-4" style="grid-template-columns: repeat(auto-fill, minmax(250px, 1fr))">
<div class="bg-pink-100 p-4 rounded">Card 1</div>
<div class="bg-pink-100 p-4 rounded">Card 2</div>
<div class="bg-pink-100 p-4 rounded">Card 3</div>
<div class="bg-pink-100 p-4 rounded">Card 4</div>
<div class="bg-pink-100 p-4 rounded">Card 5</div>
</div>Practical Three-Column Feature Grid
Putting it all together, here is a real feature section pattern used on marketing pages. Each feature card has an icon, heading, and description. The grid is grid-cols-1 sm:grid-cols-3 so it stacks on mobile and spreads into three columns on larger screens. Consistent padding and a subtle background create visual separation without borders.
<section class="grid grid-cols-1 sm:grid-cols-3 gap-8 px-8 py-12">
<div class="text-center">
<div class="text-4xl mb-4">🚀</div>
<h3 class="font-bold text-lg mb-2">Fast</h3>
<p class="text-gray-600">Ships in milliseconds.</p>
</div>
<div class="text-center">
<div class="text-4xl mb-4">🔒</div>
<h3 class="font-bold text-lg mb-2">Secure</h3>
<p class="text-gray-600">Built-in protection.</p>
</div>
<div class="text-center">
<div class="text-4xl mb-4">📱</div>
<h3 class="font-bold text-lg mb-2">Responsive</h3>
<p class="text-gray-600">Works on all devices.</p>
</div>
</section>Quick Check
Test your understanding of Tailwind CSS Mastery concepts from this lesson.
Lesson Recap
In this lesson you learned: the grid class activates a CSS Grid container, grid-cols-1 through grid-cols-12 define equal-width column tracks, and responsive prefixes like md:grid-cols-3 switch column counts at breakpoints. Next up we explore how to make grid items span multiple columns and rows for complex layouts.
常见问题解答
「定义网格列」课时是免费的吗?
是的 — 「定义网格列」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Tailwind CSS Academy 课程的其余内容,请升级到 CoddyKit PRO。 Tailwind CSS Academy 课程共包含 4 节课。
「定义网格列」这节课中我会学到什么?
使用 grid-cols-1 到 grid-cols-12 创建多列布局,并使用 grid-cols-none 配置自定义网格。 你通过在浏览器中直接运行的动手代码来练习 Tailwind CSS Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Tailwind CSS Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Tailwind CSS Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「定义网格列」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Tailwind CSS Academy 课中编写并运行代码吗?
能。每节 Tailwind CSS Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。