HTML <tbody> Tag

The <tbody> tag defines the body content (a set of rows) of an HTML table creating a separate semantic block in it. The <tbody> tag is used along with the <thead> and the <tfoot> tags, which specify header and footer of the table respectively.

The <tbody> tag must be used as a child of the <table> element, after the <caption>, <colgroup> (if any) and the <thead> elements. In HTML5, the <tfoot> element comes either before or after the <tbody> element.

The <thead>, <tbody>, and <tfoot> elements do not affect the layout of the table by default. Use CSS properties to customize the look of the table.

When printing a document, the <thead> and <tfoot> elements will define the information that can be the same or very similar on each page of a multi-page table, while the content of the <tbody> tag will vary from page to page.

In case of using <tbody>, you can't have <tr> elements (table rows) which are children of the <table> element, but are not included within the <tbody>. If you use non-header and non-footer rows they must be inside of the <tbody> element.

More than one <tbody> elements can be used for each table as long as they are all successive. This will separate the rows in large tables into sections and you will be able to format each of them separately.

Syntax

The <tbody> tag comes in pairs. The content is written between the opening (<tbody>) and closing (</tbody>) tags.

<table>
  <thead> ... </thead>
  <tfoot> ... </tfoot>
  <tbody>
    <tr>
      <td> ... </td>
    </tr>
  </tbody>
</table>

Example of the HTML <tbody> tag:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      th, td {
        padding: 10px;
        border: 1px solid #666;
      }
    </style>
  </head>
  <body>
    <table style="width:80%; margin:30px auto; border-collapse:collapse;">
      <thead style="background-color:#1c87c9; color:#fff;">
        <tr>
          <th>Month</th>
          <th>Savings</th>
        </tr>
      </thead>
      <tfoot style="background-color:grey;">
        <!-- <tfoot> is placed after <thead>, but is shown on the bottom of the table. -->
        <tr>
          <td>Total</td>
          <td>1500</td>
        </tr>
      </tfoot>
      <tbody style="background-color:lightgrey;">
        <tr>
          <td>January</td>
          <td>500</td>
        </tr>
        <tr>
          <td>February</td>
          <td>1000</td>
        </tr>
      </tbody>
    </table>
  </body>
</html>

Result

Example of the HTML <tbody> tag with the <thead> and <tfoot> tags:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      table{
      width:60%; 
      margin:30px auto; 
      border-collapse:collapse;
      }
      thead{
      background-color:#8ebf42; 
      color:#fff;
      }
      tbody{
      background-color: #f3ebeb;
      }
      tfoot{
      background-color: #ccc7c7;
      }
      th, td {
      padding: 10px;
      border: 1px solid #666666;
      }
    </style>
  </head>
  <body>
    <table >
      <thead>
        <tr>
          <th>Month</th>
          <th>Savings</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>January</td>
          <td>1500</td>
        </tr>
        <tr>
          <td>February</td>
          <td>1000</td>
        </tr>
      </tbody>
      <tfoot >
        <tr>
          <td>Total</td>
          <td>2500</td>
        </tr>
      </tfoot>
    </table>
  </body>
</html>

Attributes

Attribute Values Description
align right
left
center
justify
char
Specifies the alignment of the content inside <tbody> element.
Not supported in HTML5
bgcolor bgcolor Sets the background color of the rows inside <tbody> element.
Not supported in HTML5.
char character Specifies the alignment of the content inside the <tbody> element to a character. Is used only when the attribute align = "char".
Not supported in HTML5.
charoff number Specifies the number of characters the content inside the <tbody> element will be aligned from the character specified by the char attribute. Is used only when the attribute align = "char".
Not supported in HTML5.
valign top
bottom
middle
baseline
Specifies a vertical alignment of the content inside the <tbody>element.
Not supported in HTML5.

The <tbody> tag also supports the Global Attributes and the Event Attributes.

How to style <tbody> tag?

Common properties to alter the visual weight/emphasis/size of text in <tbody> tag:

  • CSS font-style property sets the style of the font. normal | italic | oblique | initial | inherit.
  • CSS font-family property specifies a prioritized list of one or more font family names and/or generic family names for the selected element.
  • CSS font-size property sets the size of the font.
  • CSS font-weight property defines whether the font should be bold or thick.
  • CSS text-transform property controls text case and capitalization.
  • CSS text-decoration property specifies the decoration added to text, and is a shorthand property for text-decoration-line, text-decoration-color, text-decoration-style.

Coloring text in <tbody> tag:

  • CSS color property describes the color of the text content and text decorations.
  • CSS background-color property sets the background color of an element.

Text layout styles for <tbody> tag:

  • CSS text-indent property specifies the indentation of the first line in a text block.
  • CSS text-overflow property specifies how overflowed content that is not displayed should be signalled to the user.
  • CSS white-space property specifies how white-space inside an element is handled.
  • CSS word-break property specifies where the lines should be broken.

Other properties worth looking at for <tbody> tag:


Browser support



Do you find this helpful?

Related articles