Tailwind CSS テーブル
Tailwind CSS は、テーブルを作成およびカスタマイズするための便利なユーティリティクラスのセットを提供します。
これらのユーティリティクラスを使用すると、枠線、間隔、配置、フォントスタイルなど、テーブルのスタイルを簡単に制御できます。
次に、Tailwind CSS を使用してテーブルを作成およびカスタマイズする方法を詳しく説明します。
基本テーブルスタイル
Tailwind CSS は、テーブルの外観とレイアウトを制御するための基本クラスを提供します。一般的に使用されるユーティリティクラスには、テーブルのレイアウトを設定するための table-auto と table-fixed があります。
table-auto:デフォルトレイアウトで、テーブルの列幅は内容に応じて自動的に決まります。table-fixed:テーブルの列幅を固定します。すべての列の幅はテーブルの合計幅に基づいて均等に分割され、統一された列幅のテーブルに適しています。
table-auto を使用すると、セルの内容に合わせてテーブルの列幅を自動調整できます。
例
<thead>
<tr>
<th>Song</th>
<th>Artist</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<tr>
<td>The Sliding Mr. Bones (Next Stop, Pottersville)</td>
<td>Malcolm Lockyer</td>
<td>1961</td>
</tr>
<tr>
<td>Witchy Woman</td>
<td>The Eagles</td>
<td>1972</td>
</tr>
<tr>
<td>Shining Star</td>
<td>Earth, Wind, and Fire</td>
<td>1975</td>
</tr>
</tbody>
</table>
試してみる »
table-fixed を使用すると、テーブルは内容を無視して列に固定幅を使用できます。最初の行の幅がテーブル全体の列幅を設定します。
例
<thead>
<tr>
<th>Song</th>
<th>Artist</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<tr>
<td>The Sliding Mr. Bones (Next Stop, Pottersville)</td>
<td>Malcolm Lockyer</td>
<td>1961</td>
</tr>
<tr>
<td>Witchy Woman</td>
<td>The Eagles</td>
<td>1972</td>
</tr>
<tr>
<td>Shining Star</td>
<td>Earth, Wind, and Fire</td>
<td>1975</td>
</tr>
</tbody>
</table>
試してみる »
枠線と間隔
Tailwind は、テーブルの枠線、内側と外側の余白、その他のレイアウトの詳細を制御するための多くのユーティリティクラスを提供します。
border、border-collapse、border-spacing などのクラスを使用して、テーブルの枠線スタイルを制御できます。
border:テーブルに枠線を追加します。border-collapse:テーブルのセルの枠線を結合するかどうかを制御します。border-spacing:セル間の間隔を設定します。
例
<thead>
<tr>
<th class="border px-4 py-2">Header 1</th>
<th class="border px-4 py-2">Header 2</th>
<th class="border px-4 py-2">Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="border px-4 py-2">Row 1, Col 1</td>
<td class="border px-4 py-2">Row 1, Col 2</td>
<td class="border px-4 py-2">Row 1, Col 3</td>
</tr>
<tr>
<td class="border px-4 py-2">Row 2, Col 1</td>
<td class="border px-4 py-2">Row 2, Col 2</td>
<td class="border px-4 py-2">Row 2, Col 3</td>
</tr>
</tbody>
</table>
試してみる »
内側と外側の余白
px-*、py-* クラスを使用してテーブルのセルの内側の余白を設定し、m-* および p-* クラスを使用してテーブルの外側の余白を設定できます。
例
<table class="table-auto w-full border-collapse">
<thead>
<tr>
<th class="border px-6 py-4">Header 1</th>
<th class="border px-6 py-4">Header 2</th>
<th class="border px-6 py-4">Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="border px-6 py-4">Row 1, Col 1</td>
<td class="border px-6 py-4">Row 1, Col 2</td>
<td class="border px-6 py-4">Row 1, Col 3</td>
</tr>
</tbody>
</table>
<!-- テーブルの外側の余白を設定 -->
<table class="table-auto w-full border-collapse mt-8">
<thead>
<tr>
<th class="border px-4 py-2">Header 1</th>
<th class="border px-4 py-2">Header 2</th>
<th class="border px-4 py-2">Header 3</th>
</tr>
</thead>
</table>
試してみる »
配置と文字スタイル
Tailwind は、テーブルのセル内容の配置とフォントスタイルを制御するためのいくつかのクラスを提供しています。例:
text-left,text-center,text-right:テキストの配置を制御します。font-semibold,font-bold:フォントの太さを制御します。uppercase,lowercase,capitalize:テキストの大文字/小文字を制御します。
例
<table class="table-auto border-collapse w-full">
<thead>
<tr>
<th class="border px-4 py-2 text-center font-bold">Header 1</th>
<th class="border px-4 py-2 text-center font-bold">Header 2</th>
<th class="border px-4 py-2 text-center font-bold">Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="border px-4 py-2 text-center">Row 1, Col 1</td>
<td class="border px-4 py-2 text-center">Row 1, Col 2</td>
<td class="border px-4 py-2 text-center">Row 1, Col 3</td>
</tr>
</tbody>
</table>
試してみる »
セルの結合
Tailwind CSS は、HTML 属性を使用してセルの結合を設定することをサポートしています。例えばcolspanとrowspan。
例
<table class="table-auto w-full border-collapse">
<thead>
<tr>
<th class="border px-4 py-2" colspan="2">Header 1 & 2</th>
<th class="border px-4 py-2">Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="border px-4 py-2" colspan="2">Row 1, Col 1 & 2</td>
<td class="border px-4 py-2">Row 1, Col 3</td>
</tr>
</tbody>
</table>
試してみる »
レスポンシブテーブル
Tailwind CSS のレスポンシブデザインクラスを使用して、さまざまな画面サイズでテーブルのレイアウトを制御することもできます。
たとえば、sm:、md:、lg: プレフィックスを使用して、異なる画面での表示方法を設定します。
例
<table class="table-auto w-full border-collapse">
<thead>
<tr>
<th class="border px-4 py-2">Header 1</th>
<th class="border px-4 py-2 hidden md:table-cell">Header 2</th>
<th class="border px-4 py-2">Header 3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="border px-4 py-2">Row 1, Col 1</td>
<td class="border px-4 py-2 hidden md:table-cell">Row 1, Col 2</td>
<td class="border px-4 py-2">Row 1, Col 3</td>
</tr>
</tbody>
</table>
試してみる »
テーブルキャプション
使用するcaption-topキャプション要素をテーブルの上部に配置します。
例
<caption class="caption-top">
Table 3.1: Professional wrestlers and their signature moves.
</caption>
<thead>
<tr>
<th>Wrestler</th>
<th>Signature Move(s)</th>
</tr>
</thead>
<tbody>
<tr>
<td>"Stone Cold" Steve Austin</td>
<td>Stone Cold Stunner, Lou Thesz Press</td>
</tr>
<tr>
<td>Bret "The Hitman" Hart</td>
<td >The Sharpshooter</td>
</tr>
<tr>
<td>Razor Ramon</td>
<td>Razor's Edge, Fallaway Slam</td>
</tr>
</tbody>
</table>
試してみる »
使用するcaption-bottomキャプション要素をテーブルの下部に配置します。
例
<caption class="caption-bottom">
Table 3.1: Professional wrestlers and their signature moves.
</caption>
<thead>
<tr>
<th>Wrestler</th>
<th>Signature Move(s)</th>
</tr>
</thead>
<tbody>
<tr>
<td>"Stone Cold" Steve Austin</td>
<td>Stone Cold Stunner, Lou Thesz Press</td>
</tr>
<tr>
<td>Bret "The Hitman" Hart</td>
<td >The Sharpshooter</td>
</tr>
<tr>
<td>Razor Ramon</td>
<td>Razor's Edge, Fallaway Slam</td>
</tr>
</tbody>
</table>
試してみる »