Skip to main content

HTML Tags

HTML <tbody> Tag

Written by Updated HTML <tbody> Tag exercises

The tbody tag groups the main data rows of a table, between the header (thead) and the footer (tfoot). Even if you never write it, the browser adds one for you, which explains a classic CSS surprise.

The three parts of a table work together: thead, <tbody> and tfoot.

Syntax

html

<table>
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
    </tr>
  </tbody>
</table>

It has no attributes of its own, only the global ones such as class and id.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tbody Tag Example</title>
  <style>
    table {
      border-collapse: collapse;
      width: 70%;
      margin: 20px auto;
      text-align: center;
    }
    thead {
      background-color: #29AB87;
      color: white;
    }
    tbody tr:nth-child(even) {
      background-color: #f9f9f9;
    }
    th, td {
      border: 1px solid #555;
      padding: 10px;
    }
  </style>
</head>
<body>

  <h2>HTML Table with Tbody Example</h2>

  <table>
    <thead>
      <tr>
        <th>Country</th>
        <th>Capital</th>
        <th>Population (Millions)</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>India</td>
        <td>New Delhi</td>
        <td>1428</td>
      </tr>
      <tr>
        <td>USA</td>
        <td>Washington, D.C.</td>
        <td>332</td>
      </tr>
      <tr>
        <td>Japan</td>
        <td>Tokyo</td>
        <td>125</td>
      </tr>
    </tbody>
  </table>

</body>
</html>

The table has a green header row and striped data rows. The stripes come from CSS that targets only the rows inside <tbody>, so the header is not striped too.

The hidden tbody

If your table has rows directly inside <table>, the browser wraps them in a <tbody> automatically. So the selector table > tr matches nothing, because the rows are really inside the invisible <tbody>. Use table tr or tbody tr.

Good to know

  • A table can have several <tbody> elements, to split data into groups, each with its own heading row.
  • Style data rows with tbody tr:nth-child(even) for stripes and tbody tr:hover for a hover highlight.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

HTML <tbody> Tag exercises

Group the data rows

Make a table with a head that says Name and a body with two rows: Ada and Grace. Write the tags of both groups.

Result

Check your answer to see it here.