Gridの列の定義
grid-cols-1からgrid-cols-12までを使って複数列のレイアウトを作成し、grid-cols-noneでカスタム構成を指定します。
「Gridの列の定義」はCoddyKit上の無料Tailwind CSS Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「Gridの列の定義」レッスンは無料ですか?
はい。「Gridの列の定義」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Tailwind CSS Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Tailwind CSS Academyコースには全4レッスンが含まれています。
「Gridの列の定義」で何を学びますか?
grid-cols-1からgrid-cols-12までを使って複数列のレイアウトを作成し、grid-cols-noneでカスタム構成を指定します。 ブラウザで直接実行するハンズオンコードでTailwind CSS Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Tailwind CSS Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのTailwind CSS Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「Gridの列の定義」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このTailwind CSS Academyレッスンでコードを書いて実行できますか?
はい。すべてのTailwind CSS Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Gridの列の定義
- 列と行のまたぎ
- Gridの行と自動配置
- Gridのギャップ、正当化、配置