HTML Tables
In this page:
<table>
<tr>
<th>Header</th>
<th>Header</th>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
</tr>
</table>
Basic Table Elements (table, tr, td)
To build an HTML table, you define the grid system using three nested elements: the table container, tr for table rows, and td for table data cells.
- Think of tr as a horizontal row container, and td as individual cells side-by-side within that row.
- Accepted attributes:
- td colspan — number of columns the cell spans
- td rowspan — number of rows the cell spans
- td headers — space-separated list of th ids
- border, cellpadding, cellspacing, align, width on table are obsolete; use CSS
Example: Basic Table Elements (table, tr, td)
<table>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
</table>
Declaring Table Headings (th)
Table headings should stand out from regular data. By using the th tag instead of td for your header row, browsers will automatically render the text in bold and center-aligned format.
- Use the th tag on your first row to make reading table columns much easier.
- Accepted attributes:
- scope — row | col | rowgroup | colgroup | auto
- colspan / rowspan — positive integers
- headers — space-separated list of th ids
- abbr — short label for the header cell
Example: Declaring Table Headings (th)
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>30</td>
</tr>
</table>
Merging Cells (colspan and rowspan)
Sometimes you need a header or summary row to span across multiple columns or merge cells vertically. The colspan attribute merges cells horizontally across columns, and the rowspan attribute merges them vertically down rows.
- Double-check your cell counts when using span attributes to prevent table alignment issues.
- Accepted attributes:
- colspan — positive integer (default 1)
- rowspan — non-negative integer (default 1; 0 spans to end of row group)
- Valid on td and th
Example: Merging Cells (colspan and rowspan)
<table>
<tr>
<td colspan="2">Merged across columns</td>
</tr>
<tr>
<td rowspan="2">Merged down rows</td>
<td>Cell</td>
</tr>
<tr>
<td>Cell</td>
</tr>
</table>
Semantic Table Structure (thead, tbody, tfoot)
For complex tables, you can group rows into semantic containers: thead (table header), tbody (table body), and tfoot (table footer). This makes it easier to apply specific visual styles and print long tables across multiple pages.
Example: Semantic Table Structure (thead, tbody, tfoot)
<table>
<thead>
<tr><th>Item</th></tr>
</thead>
<tbody>
<tr><td>Apple</td></tr>
</tbody>
<tfoot>
<tr><td>Total</td></tr>
</tfoot>
</table>
Accessible Tables with captions and scopes
Screen readers can find tables difficult to interpret unless you add accessibility helpers. The caption tag defines a visible title description for the table, and the scope attribute tells assistive devices if a th is a header for a column or row.
- Always specify scope="col" or scope="row" inside your th tags for maximum accessibility.
- Accepted attributes:
- caption — element with no special attributes; first child of table
- scope — row | col | rowgroup | colgroup | auto (on th)
- headers — space-separated list of th ids (on td/th)
Example: Accessible Tables with captions and scopes
<table>
<caption>Monthly Sales</caption>
<tr>
<th scope="col">Month</th>
<th scope="col">Sales</th>
</tr>
</table>
Column and Row Spanning
The colspan attribute makes a cell stretch across multiple columns. The rowspan attribute makes a cell stretch across multiple rows. Both are numbers representing how many cells to span.
- Always count your columns carefully when using colspan to avoid layout breaking.
- Accepted attributes:
- colspan — positive integer (default 1)
- rowspan — non-negative integer (default 1)
- headers — space-separated list of th ids
Example: Column and Row Spanning
<table>
<tr>
<td colspan="3">Spans 3 columns</td>
</tr>
<tr>
<td rowspan="2">Spans 2 rows</td>
<td>Cell</td>
<td>Cell</td>
</tr>
</table>
Table Structure Elements
Use thead for header rows, tbody for main data rows, and tfoot for summary rows. This gives your table semantic meaning and helps screen readers navigate it correctly.
Example: Table Structure Elements
<table>
<thead>
<tr><th>Product</th></tr>
</thead>
<tbody>
<tr><td>Book</td></tr>
</tbody>
<tfoot>
<tr><td>Summary</td></tr>
</tfoot>
</table>
Colgroup and Col Elements
The colgroup and col elements let you apply styling to an entire column of a table without touching every single cell. Instead of adding a class to every td in a column, you describe the column once at the top of the table.
- Use colgroup when you want a whole column highlighted, like a totals column, without repeating CSS classes on every row.
- Accepted attributes:
- span — positive integer number of columns covered (on col and colgroup)
- width, align, bgcolor — obsolete; use CSS
- Only global attributes otherwise (class, id, style)
Example: Colgroup and Col Elements
<table>
<colgroup>
<col style="background-color: yellow;">
<col>
</colgroup>
<tr>
<td>Total</td>
<td>100</td>
</tr>
</table>
- Placing plain text or non-row elements directly inside the table tag.
- Miscounting columns when coding colspan and rowspan attributes, leading to distorted table layouts.
- Using empty layout table structures for visual page design rather than for structuring actual tabular data.
- Tables are constructed using rows (tr), which contain header cells (th) or standard data cells (td).
- Merge multiple cells horizontally using the colspan attribute, and vertically with the rowspan attribute.
- Organize complex data semantically using the thead, tbody, and tfoot containers for maximum accessibility.
HTML tables and standard structure tags are universally supported across all browsers.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- HTML Favicon
- HTML Page Title
- HTML Tables
- HTML Lists
- HTML Block & Inline
- HTML Comments
- HTML Colors
- HTML CSS
- HTML Links
- HTML Images
- HTML RGB Colors
- HTML HEX Colors
- HTML HSL Colors
- HTML Link Colors
- HTML Link Bookmarks
- HTML Background Images
- HTML Table Borders
- HTML Table Sizes
- HTML Table Headers
- HTML Table Padding & Spacing
- HTML Colspan & Rowspan
- HTML Table Styling
- HTML Table Colgroup
- HTML Unordered Lists
- HTML Ordered Lists
- HTML Other Lists / Description Lists