HTML Tables
Master the art of organizing data with structured, accessible tables!
What are HTML Tables?
HTML tables are structured elements used to display tabular data in rows and columns, making complex information easy to read and understand.
šÆ When to Use Tables:
- šData Presentation - Financial reports, statistics
- šComparison Data - Product features, pricing
- šļøSchedules & Calendars - Timetables, events
- šGrid Data - Spreadsheet-like information
Table Elements
<table>Table container<tr>Table row<th>Table header<td>Table data cell<thead>Header section<tbody>Body section<tfoot>Footer sectionš Basic Table Syntax
The <table> Structure
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>Row-Based Structure
Tables are built row by row, not column by column
Nested Elements
Rows contain cells, which hold the actual content
Live Example
| Name | Age | City |
|---|---|---|
| John Doe | 28 | New York |
| Jane Smith | 32 | London |
<table> with 2 rows and 3 columnsšļø Advanced Table Structure
šļø Table Structures
HTML Code
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
</table>Description:
Simple table with headers and data
Key Features:
- Basic structure
- Header row
- Data rows
Live Preview
š” Best Practices:
- ⢠Always use <th> for header cells
- ⢠Consider adding basic styling for better readability
- ⢠Keep table structure simple and clear
š§ Table Elements Explained
šÆ Core Elements
<table>
The container element for the entire table
<table>...</table><tr> (Table Row)
Defines a row within the table
<tr>...</tr><th> vs <td>
Header cells vs Data cells
<th>Header</th> vs <td>Data</td>ā” Semantic Elements
<thead>, <tbody>, <tfoot>
Group table content semantically
<thead>...</thead>
<tbody>...</tbody>
<tfoot>...</tfoot><caption>
Provides a title or description for the table
<caption>Table Title</caption>colspan & rowspan
Makes cells span multiple columns or rows
colspan="2" rowspan="3"š How Browsers Process Tables
Browser Table Processing Journey
Follow how the browser processes tables from HTML to rendered grid
HTML Parsing
Browser reads HTML and encounters table structure
DOM Construction
Browser builds Document Object Model tree
CSS Styling
Browser applies default table styles
Layout Calculation
Browser calculates table dimensions
Accessibility Tree
Table added to accessibility API
Rendering
Table is painted on screen
Step 1: HTML Parsing
HTML parser identifies table elements and their hierarchy
<table><tr><th>Header</th></tr></table>What happens:
Browser reads HTML and encounters table structure
Live Table Processing Example
HTML Structure:
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Laptop</td>
<td>$999</td>
</tr>
</tbody>
</table>Rendered Table:
| Product | Price |
|---|---|
| Laptop | $999 |
| Mouse | $25 |
⢠Browser calculated column widths automatically
⢠Applied default border styles
⢠Structured for accessibility
š” Real-World Table Examples
Basic Data Table
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John</td>
<td>25</td>
</tr>
</table>| Name | Age |
|---|---|
| John | 25 |
| Jane | 30 |
Simple two-column table with header row
Structured Table
<table>
<thead>...</thead>
<tbody>...</tbody>
<tfoot>...</tfoot>
</table>| Product | Price |
|---|---|
| Laptop | $999 |
| Total | $999 |
Table with semantic sections
Merged Cells
<td colspan="2">Spans 2 columns</td>
<td rowspan="2">Spans 2 rows</td>| Full Name | Department | |
|---|---|---|
| John | Doe | |
Table with column and row spanning
Financial Table
<table>
<caption>Quarterly Report</caption>
...
</table>| Quarter | Sales | Growth |
|---|---|---|
| Q1 | $50,000 | +5% |
Table with caption and styled cells
Schedule Table
<table>
<th scope="col">Time</th>
<th scope="row">9:00</th>
</table>| Time | Monday | Tuesday |
|---|---|---|
| 9:00 | Meeting | Workshop |
Table with row and column headers
Responsive Table
<div class="overflow-x-auto">
<table>...</table>
</div>| Name | Phone | Role | |
|---|---|---|---|
| John Doe | john@example.com | (555) 123-4567 | Developer |
ā Scroll horizontally on mobile ā
Table wrapped for horizontal scrolling
ā Table Best Practices
šÆ Accessibility & Semantics
ā Do's
- āUse <th> for header cells
- āInclude <caption> for table description
- āUse scope attribute for complex headers
- āProvide alternative text for complex tables
ā ļø Common Mistakes
ā Don'ts
- āDon't use tables for page layout
- āAvoid nested tables when possible
- āDon't skip semantic table sections
- āAvoid overly complex cell spanning
š” Pro Tips:
- ⢠Use scope="col" for column headers and scope="row" for row headers
- ⢠Always include <thead>, <tbody>, and <tfoot> for better structure
- ⢠Make tables responsive with horizontal scrolling on mobile
- ⢠Use zebra striping (<tr> background colors) for better readability
- ⢠Consider using <figure> and <figcaption> for table context
š® Table Playground
Configure Your Table
š” Quick Presets:
Live Preview & Code
| Header 1 | Header 2 | Header 3 |
|---|---|---|
| Row 1 Col 1 | Row 1 Col 2 | Row 1 Col 3 |
| Row 2 Col 1 | Row 2 Col 2 | Row 2 Col 3 |
| Row 3 Col 1 | Row 3 Col 2 | Row 3 Col 3 |
Table: 3 rows Ć 3 columns
Features: Header Caption bordered
Generated HTML Code:
<table class="min-w-full text-sm border-collapse border border-gray-300">
<caption class="text-lg font-semibold mb-2">Sample Data Table</caption>
<thead class="bg-gray-100">
<tr>
<th class="px-4 py-2 text-left border border-gray-300 font-semibold">Header 1</th>
<th class="px-4 py-2 text-left border border-gray-300 font-semibold">Header 2</th>
<th class="px-4 py-2 text-left border border-gray-300 font-semibold">Header 3</th>
</tr>
</thead>
<tbody>
<tr class="">
<td class="px-4 py-2 text-left border border-gray-300">Row 1 Col 1</td>
<td class="px-4 py-2 text-left border border-gray-300">Row 1 Col 2</td>
<td class="px-4 py-2 text-left border border-gray-300">Row 1 Col 3</td>
</tr>
<tr class="bg-gray-50">
<td class="px-4 py-2 text-left border border-gray-300">Row 2 Col 1</td>
<td class="px-4 py-2 text-left border border-gray-300">Row 2 Col 2</td>
<td class="px-4 py-2 text-left border border-gray-300">Row 2 Col 3</td>
</tr>
<tr class="">
<td class="px-4 py-2 text-left border border-gray-300">Row 3 Col 1</td>
<td class="px-4 py-2 text-left border border-gray-300">Row 3 Col 2</td>
<td class="px-4 py-2 text-left border border-gray-300">Row 3 Col 3</td>
</tr>
</tbody>
</table>Accessibility:
ā Has header row
Mobile Ready:
ā Good for mobile
š± Responsive Table Design
Horizontal Scroll
Allow tables to scroll horizontally on mobile devices to maintain readability.
Stacked Layout
Convert table rows into card-like layouts for better mobile viewing.
Priority Columns
Show only essential columns on mobile and provide expandable details.
š Mobile Best Practices:
- ⢠Use container div with overflow-x-auto for horizontal scrolling
- ⢠Consider hiding less important columns on small screens
- ⢠Increase touch target sizes for interactive table elements
- ⢠Use media queries to adjust font sizes and padding
- ⢠Test table readability on various mobile devices