Performance: Table and data grid patterns
This affects page load speed and rendering performance by how many DOM nodes are created and how CSS styles are applied to table elements.
<table class="w-full border-collapse border border-gray-300"> <thead class="bg-gray-100"> <tr> <th class="p-2 text-left">Name</th> <th class="p-2 text-left">Age</th> <th class="p-2 text-left">City</th> </tr> </thead> <tbody class="divide-y divide-gray-200"> <!-- 100+ rows with minimal cell styling, using tbody divide for borders --> <tr> <td class="p-2">Alice</td> <td class="p-2">30</td> <td class="p-2">New York</td> </tr> <!-- ...more rows --> </tbody> </table>
<table class="w-full border-collapse border border-gray-300"> <thead> <tr> <th class="border border-gray-300 p-2">Name</th> <th class="border border-gray-300 p-2">Age</th> <th class="border border-gray-300 p-2">City</th> </tr> </thead> <tbody> <!-- 100+ rows with same heavy border and padding classes on every cell --> <tr> <td class="border border-gray-300 p-2">Alice</td> <td class="border border-gray-300 p-2">30</td> <td class="border border-gray-300 p-2">New York</td> </tr> <!-- ...more rows --> </tbody> </table>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy cell borders and padding on every cell | High (many styled nodes) | 100+ reflows for 100 rows | High paint cost due to many borders | [X] Bad |
| Border-collapse with row dividers and minimal cell padding | Moderate (fewer styled nodes) | Single reflow for borders | Low paint cost with simple styles | [OK] Good |
border adds a border to elements, including table cells.border-collapse controls border merging, table-auto sets table layout, and text-center aligns text.<div class="overflow-x-auto">
<table class="min-w-full border border-gray-300">
<thead>
<tr><th class="border p-2">Name</th><th class="border p-2">Age</th></tr>
</thead>
<tbody>
<tr><td class="border p-2">Alice</td><td class="border p-2">30</td></tr>
</tbody>
</table>
</div>overflow-x-auto on the div allows horizontal scrolling if the table is wider than the screen.min-w-full making it at least full width, and border classes add visible borders. Padding p-2 adds space inside cells.<table class="table-auto border-collapse border border-gray-400">
<thead>
<tr>
<th class="border p-4">ID</th>
<th class="border p-4">Score</th>
</tr>
</thead>
<tbody>
<tr>
<td class="border p-4">1</td>
<td class="border p-4">85</td>
</tr>
</tbody>
</table>border-collapse class merges adjacent borders into one, but adding border on the table itself can cause unexpected double borders.overflow-x-auto on the container allows horizontal scroll. The sticky top-0 on thead fixes the header row on scroll.divide-y divide-gray-200 on tbody creates visible row separators for readability.min-w-full ensures full width, and border-collapse merges borders nicely.<div class="overflow-x-auto max-h-64"><table class="min-w-full border-collapse border border-gray-300"><thead class="sticky top-0 bg-white">...</thead><tbody class="divide-y divide-gray-200">...</tbody></table></div> [OK]Pass the test and this pattern moves unknown