Example 1 — Basic Table
<table>
<tr><td>Apple</td><td>Red</td></tr>
<tr><td>Banana</td><td>Yellow</td></tr>
</table> How It Works
Each tr is a row; each td is a cell. No headers yet—just raw data.

HTML tables display tabular data in a structured grid of rows and columns. They are essential for datasets, comparisons, schedules, and any information that fits naturally into a spreadsheet-like layout.
This tutorial covers table structure, semantic sections, styling with CSS, colspan and rowspan, accessibility, common pitfalls, and a complete employee list example.
Container.
Rows.
Cells.
Sections.
Style it.
Accessible.
An HTML table is a container that holds data in rows and columns. You build it with the <table> element; each row uses <tr>; header cells use <th>; data cells use <td>.
Tables are for data, not page layout. Modern sites use CSS Grid and Flexbox for columns and sidebars.
See the table tag reference for every attribute. This tutorial focuses on building readable, accessible tables from scratch.
A well-formed table uses these elements:
<table> — defines the table container.<thead> — groups header row(s).<tbody> — groups the main data rows.<tfoot> — groups footer rows (totals, optional).<tr> — defines one table row.<th> — header cell (column or row title).<td> — data cell.<table>
<thead>
<tr><th>Name</th><th>Age</th></tr>
</thead>
<tbody>
<tr><td>Alice</td><td>28</td></tr>
<tr><td>Bob</td><td>34</td></tr>
</tbody>
</table> <table> — main container for all table content.<caption> — optional title shown above the table (great for accessibility).<thead> — column titles; typically one row of th cells.<tbody> — body rows with your data.<tfoot> — summary row(s) such as totals or averages.<tr> — a horizontal row of cells.<th scope="col"> — column header; use scope="row" for row headers.<td> — ordinary data cell.HTML4 table attributes still appear in old tutorials but are deprecated in HTML5. Use CSS instead:
border — use border: 1px solid #ddd on cells via CSS.cellpadding — use padding on th, td.cellspacing — use border-collapse: collapse and margins/borders.Valid modern attributes include colspan and rowspan on th and td to merge cells.
CSS improves readability with borders, spacing, alignment, and alternating row colors:
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #f4f4f4;
}
tr:nth-child(even) {
background-color: #f9f9f9;
} border-collapse: collapse merges adjacent borders into single lines. Zebra striping with nth-child(even) helps users track rows across wide tables.
Merge cells when headers or summaries span multiple columns or rows:
<tr>
<th colspan="2">Schedule</th>
</tr>
<tr>
<td rowspan="2">Monday</td>
<td>Math</td>
</tr> th with scope, add caption, and avoid tables for non-tabular layout.border, cellpadding, and cellspacing with CSS.| Element | Purpose |
|---|---|
<table> | Table container |
<thead> | Header section |
<tbody> | Body rows |
<tfoot> | Footer / totals |
<tr> | Table row |
<th> | Header cell |
<td> | Data cell |
colspan="2" | Span 2 columns |
rowspan="2" | Span 2 rows |
Six examples from a minimal grid to a styled employee list. Each includes View Output and Try It Yourself.
<table>
<tr><td>Apple</td><td>Red</td></tr>
<tr><td>Banana</td><td>Yellow</td></tr>
</table> Each tr is a row; each td is a cell. No headers yet—just raw data.
<table>
<thead>
<tr><th>Product</th><th>Price</th></tr>
</thead>
<tbody>
<tr><td>Notebook</td><td>$4.99</td></tr>
<tr><td>Pen</td><td>$1.25</td></tr>
</tbody>
</table> thead wraps the header row; th cells label each column.
<table>
<thead>
<tr><th>Item</th><th>Qty</th><th>Cost</th></tr>
</thead>
<tbody>
<tr><td>Coffee</td><td>2</td><td>$6.00</td></tr>
<tr><td>Bagel</td><td>1</td><td>$3.50</td></tr>
</tbody>
<tfoot>
<tr><td colspan="2">Total</td><td>$9.50</td></tr>
</tfoot>
</table> tfoot holds the total row. colspan="2" merges the first two footer cells.
<table>
<tr><th colspan="2">Schedule</th></tr>
<tr>
<td rowspan="2">Mon</td>
<td>Math</td>
</tr>
<tr><td>Science</td></tr>
</table> The header spans two columns; “Mon” spans two rows beside Math and Science.
<style>
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ddd; padding: 8px; }
th { background: #f4f4f4; }
tr:nth-child(even) { background: #f9f9f9; }
</style>
<table>
<thead><tr><th>Name</th><th>Score</th></tr></thead>
<tbody>
<tr><td>Alice</td><td>92</td></tr>
<tr><td>Bob</td><td>85</td></tr>
</tbody>
</table> Alternating row backgrounds (zebra stripes) from the CSS in the Styling section above.
Full example from this tutorial—styled table with four columns and three employees:
<table>
<thead>
<tr><th>ID</th><th>Name</th><th>Position</th><th>Department</th></tr>
</thead>
<tbody>
<tr><td>1</td><td>John Doe</td><td>Software Engineer</td><td>Development</td></tr>
<tr><td>2</td><td>Jane Smith</td><td>Project Manager</td><td>Operations</td></tr>
</tbody>
</table> Combines semantic structure, CSS styling, and real-world tabular data.
thead, tbody, and th for structurecaption to describe the tableborder-collapse, padding)border / cellpaddingHTML table elements are supported in every browser. CSS styling (border-collapse, nth-child) works in all modern browsers.
HTML table elements are supported in every browser. CSS styling (border-collapse, nth-child) works in all modern browsers.
Bottom line: Tables are one of the oldest, most reliable HTML features.
HTML tables are fundamental for presenting structured data on the web. Use semantic sections, header cells, and CSS styling to build tables that are clear, accessible, and visually polished.
Next, format text inside cells with HTML Lists to organize content with bullets and numbers, or explore individual tags like th and td.
Not layout.
RuleHeaders.
SemanticSections.
StructureNot border=.
Stylea11y.
AccessTables were once the primary tool for web page layout in the 1990s and early 2000s. CSS layout replaced that pattern. Today, using a table for non-data layout fails accessibility audits and makes responsive design much harder.
Add rows, headers, and CSS stripes, then preview your employee list live.
6 people found this page helpful