HTML tables are used to display structured data in rows and columns. A table is created using the <table> element along with <tr> (table row), <th> (table header), and <td> (table data).
Tables are ideal for showing reports, schedules, and comparison data. Modern HTML uses tables only for tabular data, not for general page layout.
A basic HTML table needs a table element and at least one row of header cells followed by rows of data cells.
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
</table>
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>Alice</td>
<td>24</td>
</tr>
<tr>
<td>Bob</td>
<td>30</td>
</tr>
</table>
<table border="1">
<tr>
<th colspan="2">Employee</th>
</tr>
<tr>
<td rowspan="2">John</td>
<td>Manager</td>
</tr>
<tr>
<td>IT Department</td>
</tr>
</table>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
}
th {
background-color: #4CAF50;
color: white;
}
</style>
<table>
<tr>
<th>Country</th>
<th>Capital</th>
</tr>
<tr>
<td>India</td>
<td>New Delhi</td>
</tr>
<tr>
<td>USA</td>
<td>Washington, D.C.</td>
</tr>
</table>
This table has two columns and two data rows. The first row contains headers, and the next rows contain values.
| Name | Age |
|---|---|
| Alice | 24 |
| Bob | 30 |
The header spans two columns using colspan="2", and the cell with "John" spans two rows using rowspan="2".
| Employee | |
|---|---|
| John | Manager |
| IT Department | |
The table below uses CSS for cleaner borders, padding, and a colored header row.
| Country | Capital |
|---|---|
| India | New Delhi |
| USA | Washington, D.C. |
border-collapse: collapse; for clean, merged borders.<th> for better accessibility and semantics.border attributes.colspan and rowspan sparingly to avoid overly complex layouts.colspan to merge cells.<table>, <tr>, <th>, and <td>.colspan and rowspan let you merge cells horizontally and vertically.