An HTML table is used to present data in rows and columns, similar to spreadsheets. It organizes information clearly for users. Tables are commonly used for:
✔ Displaying structured data (e.g., pricing tables, reports).
✔ Creating layouts (although CSS is preferred for modern layouts).
✔ Presenting comparisons and statistics.
An HTML table is created using the <table>
tag, and it contains rows <tr>
and cells <td>
. If headers are needed, <th>
is used instead of <td>
.
<table border="1">
<tr>
<th>Product</th>
<th>Price</th>
</tr>
<tr>
<td>Laptop</td>
<td>$1000</td>
</tr>
<tr>
<td>Smartphone</td>
<td>$500</td>
</tr>
</table>
✔ <table>
→ Defines the table.
✔ <tr>
→ Creates a new row.
✔ <th>
→ Defines header cells (bold & centered).
✔ <td>
→ Defines data cells.
✔ border="1"
→ Adds borders (for visibility).
✔ colspan
→ Merges multiple columns into one.
✔ rowspan
→ Merges multiple rows into one.
<table border="1">
<tr>
<th colspan="2">Employee Details</th>
</tr>
<tr>
<td>Name</td>
<td>John Doe</td>
</tr>
<tr>
<td rowspan="2">Contact</td>
<td>john@example.com</td>
</tr>
<tr>
<td>+1234567890</td>
</tr>
</table>
✔ The header spans two columns using colspan="2"
.
✔ The "Contact" row spans two rows using rowspan="2"
.
A <caption>
describes the purpose of the table.