Tables help organize information in rows and columns so it's easy to read and compare. Data grids add features like sorting and scrolling for better use.
Table and data grid patterns in Tailwind
<table class="min-w-full border-collapse border border-gray-300"> <thead> <tr class="bg-gray-100"> <th class="border border-gray-300 px-4 py-2 text-left">Header 1</th> <th class="border border-gray-300 px-4 py-2 text-left">Header 2</th> </tr> </thead> <tbody> <tr> <td class="border border-gray-300 px-4 py-2">Data 1</td> <td class="border border-gray-300 px-4 py-2">Data 2</td> </tr> </tbody> </table>
Use <thead> for headers and <tbody> for data rows to keep structure clear.
Tailwind classes like border, px-4, and py-2 add spacing and borders for readability.
<table class="min-w-full border-collapse border border-gray-300"> <thead> <tr class="bg-gray-200"> <th class="border border-gray-300 px-3 py-1">Name</th> <th class="border border-gray-300 px-3 py-1">Age</th> </tr> </thead> <tbody> <tr> <td class="border border-gray-300 px-3 py-1">Alice</td> <td class="border border-gray-300 px-3 py-1">30</td> </tr> </tbody> </table>
overflow-x-auto. Rows alternate background colors for easier reading.<div class="overflow-x-auto"> <table class="min-w-full border-collapse border border-gray-300"> <thead> <tr class="bg-gray-100"> <th class="border border-gray-300 px-4 py-2">Product</th> <th class="border border-gray-300 px-4 py-2">Price</th> <th class="border border-gray-300 px-4 py-2">Stock</th> </tr> </thead> <tbody> <tr> <td class="border border-gray-300 px-4 py-2">Book</td> <td class="border border-gray-300 px-4 py-2">$10</td> <td class="border border-gray-300 px-4 py-2">15</td> </tr> <tr class="bg-gray-50"> <td class="border border-gray-300 px-4 py-2">Pen</td> <td class="border border-gray-300 px-4 py-2">$2</td> <td class="border border-gray-300 px-4 py-2">100</td> </tr> </tbody> </table> </div>
This is a complete webpage showing a user list in a table. It uses Tailwind CSS for styling. The table has borders, alternating row colors, and a hover effect to highlight rows. The container allows horizontal scrolling on small screens.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Simple Data Grid</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6 bg-gray-50"> <main class="max-w-4xl mx-auto"> <h1 class="text-2xl font-semibold mb-4">User List</h1> <div class="overflow-x-auto border border-gray-300 rounded"> <table class="min-w-full border-collapse"> <thead class="bg-gray-100"> <tr> <th class="border border-gray-300 px-4 py-2 text-left">Name</th> <th class="border border-gray-300 px-4 py-2 text-left">Email</th> <th class="border border-gray-300 px-4 py-2 text-left">Role</th> </tr> </thead> <tbody> <tr class="bg-white hover:bg-gray-50"> <td class="border border-gray-300 px-4 py-2">John Doe</td> <td class="border border-gray-300 px-4 py-2">john@example.com</td> <td class="border border-gray-300 px-4 py-2">Admin</td> </tr> <tr class="bg-gray-50 hover:bg-gray-100"> <td class="border border-gray-300 px-4 py-2">Jane Smith</td> <td class="border border-gray-300 px-4 py-2">jane@example.com</td> <td class="border border-gray-300 px-4 py-2">Editor</td> </tr> <tr class="bg-white hover:bg-gray-50"> <td class="border border-gray-300 px-4 py-2">Bob Lee</td> <td class="border border-gray-300 px-4 py-2">bob@example.com</td> <td class="border border-gray-300 px-4 py-2">Viewer</td> </tr> </tbody> </table> </div> </main> </body> </html>
Use overflow-x-auto on a container around the table to make it scroll horizontally on small screens.
Alternating row colors (bg-white and bg-gray-50) help eyes follow rows easily.
Adding hover:bg-gray-50 or similar classes improves user experience by highlighting rows on mouse hover.
Tables organize data in rows and columns for easy reading.
Tailwind CSS classes add borders, spacing, and colors to make tables clear and nice looking.
Use containers with horizontal scrolling for tables on small screens to keep content accessible.
Practice
Solution
Step 1: Understand the role of tables
Tables are designed to display data in a structured way using rows and columns.Step 2: Recognize Tailwind CSS usage
Tailwind CSS helps style these tables but does not change their purpose.Final Answer:
To organize data in rows and columns for easy reading -> Option CQuick Check:
Tables = organize data [OK]
- Confusing tables with animations
- Thinking tables handle user input
- Assuming tables add images
Solution
Step 1: Identify border classes
The classborderadds a border to elements, including table cells.Step 2: Differentiate from other classes
border-collapsecontrols border merging,table-autosets table layout, andtext-centeraligns text.Final Answer:
border -> Option DQuick Check:
Border around cells = border [OK]
- Using border-collapse instead of border
- Confusing layout classes with border
- Using text alignment classes for borders
<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>Solution
Step 1: Analyze container class
Theoverflow-x-autoon the div allows horizontal scrolling if the table is wider than the screen.Step 2: Analyze table and cell classes
The table hasmin-w-fullmaking it at least full width, andborderclasses add visible borders. Paddingp-2adds space inside cells.Final Answer:
A full-width table with borders and horizontal scroll on small screens -> Option AQuick Check:
overflow-x-auto + border = scroll + borders [OK]
- Ignoring overflow-x-auto effect
- Confusing vertical and horizontal scroll
- Missing that borders are added by border class
<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>Solution
Step 1: Understand border-collapse effect
Theborder-collapseclass merges adjacent borders into one, but addingborderon the table itself can cause unexpected double borders.Step 2: Check other options
Overflow-x-auto is optional, padding p-4 is reasonable, and the table tag is properly closed.Final Answer:
Using both border-collapse and border on the table causes double borders -> Option BQuick Check:
border-collapse + border on table = double borders [OK]
- Thinking overflow-x-auto is required always
- Assuming p-4 is too small
- Missing that table tag is closed
Solution
Step 1: Enable horizontal scrolling and fixed header
Theoverflow-x-autoon the container allows horizontal scroll. Thesticky top-0ontheadfixes the header row on scroll.Step 2: Add alternating row colors
Thedivide-y divide-gray-200ontbodycreates visible row separators for readability.Step 3: Confirm other classes
min-w-fullensures full width, andborder-collapsemerges borders nicely.Final Answer:
Option A code snippet with overflow-x-auto, sticky header, and divide-y -> Option AQuick Check:
Sticky header + scroll + row divide =<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]
- Missing sticky class for fixed header
- Using overflow-y-auto instead of overflow-x-auto
- Not adding row dividers for readability
Ready to prove it?
Pass the test and this pattern moves unknown
