Tables

Define an HTML Table

A table is made of rows (<tr>); each row holds header cells (<th>) or data cells (<td>).

Example

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

<table border="1">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Aarav Traders</td>
    <td>Aarav Shah</td>
    <td>India</td>
  </tr>
  <tr>
    <td>Blue Harbor Foods</td>
    <td>Emma Clark</td>
    <td>UK</td>
  </tr>
</table>

</body>
</html>

Result

Styling a Table with CSS

Example

The border attribute is old-fashioned; use CSS for borders, spacing and striped rows:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
  <style>
      table { border-collapse: collapse; width: 60%; font-family: Arial, sans-serif; }
      th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
      tr:nth-child(even) { background: #f2f2f2; }
      th { background: #04AA6D; color: white; }
    </style>
</head>
<body>

<table>
  <tr><th>Name</th><th>Score</th></tr>
  <tr><td>Asha</td><td>92</td></tr>
  <tr><td>Vikram</td><td>87</td></tr>
  <tr><td>Neha</td><td>95</td></tr>
</table>

</body>
</html>

Result

Table Sections and Caption

Example

Group rows with <thead>, <tbody> and <tfoot>, and describe the table with <caption>:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

<table border="1">
  <caption>Monthly savings</caption>
  <thead>
    <tr><th>Month</th><th>Savings</th></tr>
  </thead>
  <tbody>
    <tr><td>January</td><td>₹2,000</td></tr>
    <tr><td>February</td><td>₹1,500</td></tr>
  </tbody>
  <tfoot>
    <tr><td>Total</td><td>₹3,500</td></tr>
  </tfoot>
</table>

</body>
</html>

Result

Colspan and Rowspan

Example

A cell can span several columns (colspan) or rows (rowspan):

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

<table border="1" style="border-collapse:collapse">
  <tr><th colspan="2">Name</th><th>Age</th></tr>
  <tr><td>Asha</td><td>Rao</td><td>24</td></tr>
  <tr><th rowspan="2">Phone</th><td colspan="2">98450-11111</td></tr>
  <tr><td colspan="2">98450-22222</td></tr>
</table>

</body>
</html>

Result

Note: Use tables for tabular data only, never for page layout. Layout is done with CSS Flexbox and Grid.