ตารางใน Tailwind ใช้ HTML <table> ปกติ + utility classes สำหรับ:
- จัดการ border collapse/separate
- กำหนด column widths
- ใส่ zebra striping
- ทำ sticky headers
Tip: สำหรับตาราง data-heavy ให้พิจารณาใช้ library เช่น TanStack Table ร่วมกับ Tailwind
| Class | Effect |
|---|
border-collapse | Borders รวมกัน |
border-separate | Borders แยกกัน |
<!-- Collapsed (default สำหรับเส้นเดียว) -->
<table class="border-collapse">
<td class="border border-gray-300 p-2">Cell 1</td>
<td class="border border-gray-300 p-2">Cell 2</td>
<!-- Separate (มีช่องว่างระหว่าง cells) -->
<table class="border-separate border-spacing-2">
<td class="border border-gray-300 rounded p-2">Cell 1</td>
<td class="border border-gray-300 rounded p-2">Cell 2</td>
ใช้กับ border-separate เท่านั้น:
| Class | Spacing |
|---|
border-spacing-0 | 0 |
border-spacing-1 | 0.25rem |
border-spacing-2 | 0.5rem |
border-spacing-4 | 1rem |
border-spacing-x-4 | 1rem แนวนอน |
border-spacing-y-2 | 0.5rem แนวตั้ง |
| Class | CSS | Use Case |
|---|
table-auto | table-layout: auto | ปรับตาม content (default) |
table-fixed | table-layout: fixed | ความกว้างคงที่ |
<!-- Fixed: ทุก column กว้างเท่ากัน -->
<table class="table-fixed w-full">
<th class="w-1/4">Name</th>
<th class="w-1/2">Description</th>
<th class="w-1/4">Price</th>
<caption class="caption-top text-gray-500 mb-2">
<caption class="caption-bottom text-gray-500 mt-2">
<div class="overflow-x-auto">
<table class="w-full border-collapse">
<thead class="bg-gray-50 dark:bg-gray-800">
class="border-b p-3 text-left text-sm font-semibold text-gray-900 dark:text-white"
class="border-b p-3 text-left text-sm font-semibold text-gray-900 dark:text-white"
class="border-b p-3 text-right text-sm font-semibold text-gray-900 dark:text-white"
<tbody class="divide-y divide-gray-200 dark:divide-gray-700">
<tr class="hover:bg-gray-50 dark:hover:bg-gray-800/50">
<td class="p-3 text-gray-900 dark:text-white">John Doe</td>
<td class="p-3 text-gray-500">john@example.com</td>
<td class="p-3 text-right">
<button class="text-blue-500 hover:underline">Edit</button>
<tr class="odd:bg-white even:bg-gray-50">
<tr class="odd:bg-white even:bg-gray-50">
<tr class="odd:bg-white even:bg-gray-50">
<div class="max-h-64 overflow-y-auto">
<thead class="sticky top-0 bg-white shadow-sm">
<th class="p-3">Name</th>
| Do ✅ | Don’t ❌ |
|---|
ใช้ overflow-x-auto สำหรับ responsive | ปล่อยให้ตารางกว้างเกิน viewport |
ใช้ divide-y แทน border ทุก row | ใส่ border ทุก cell |
ใช้ sticky สำหรับ header ตารางยาว | ปล่อยให้ header scroll หายไป |
ใช้ text-left/right จัด alignment | ปล่อย default alignment |