HTML - <tbody> Tag



HTML <tbody> tag is used to create a separate semantic block in an HTML table, which defines the table's body content.

The <tbody> tag is used in conjunction with the <thead> and <tfoot> tags, which designate the table's header and footer, respectively. The <tbody> tag must be used as a child of the <table> element, after the <thead>, <caption>, and <colgroup> element. The HTML5 <tfoot> element can be found either before or after the <tbody> element.

These elements can be used by browsers to enable table body scrolling separate from header and footer scrolling. These features can also make it possible for the table header and footer to be printed at the top and bottom of each page when publishing a large table that spans multiple pages.

Syntax

<tbody>
  ...
</tbody>

Attribute

HTML tbody tag supports Global and Event attributes of HTML. Bellow mentioned attributes are deprecated so use CSS properties rather using these attributes.

Attribute Value Description
align left
right
center
justify
Specifies the alignment of text content(Deprecated).
bgcolor color Specifies the background color of each column cell(Deprecated).
char character Specifies the alignment of the content to a character of each column cell(Deprecated).
charoff number Specifies the number of characters to offset the column cell content from the alignment character specified by the char attribute(Deprecated).
valign baseline
bottom
middle
top
Specifies the vertical alignment of each column cell(Deprecated).

Examples of HTML table Tag

Bellow examples will illustrate the usage of tbody tag. Where, when and how to use tbody tag and how to style the body part of a table.

Creating Semantic block using tbody Tag

Following is an example where we are going to use the <tbody> tag in the table.

<!DOCTYPE html>
<html>
<body>
   <table border="1">
      <thead>
         <tr>
            <th>ID</th>
            <th>NAME</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td>1567</td>
            <td>Maya</td>
         </tr>
         <tr>
            <td>1566</td>
            <td>Ram </td>
         </tr>
         <tr>
            <td>1564</td>
            <td>Rahul</td>
         </tr>
      </tbody>
   </table>
</body>
</html>

Styling Table's Body Section

Consider another scenario where we are going to style the <tbody> tag using CSS. CSS applied to the content that comes under the <tbody> tag and is displayed on the webpage.

<!DOCTYPE html>
<html>
   <style>
      th,
      td {
         padding: 9px;
         border: 1.5px solid #DE3163;
      }
   </style>
<body>
   <table style="width:50%;  border-collapse:collapse;">
      <thead>
         <tr>
            <th>Items</th>
            <th>Price</th>
         </tr>
      </thead>
      <tfoot>
         <tr>
            <td>Total</td>
            <td>1100</td>
         </tr>
      </tfoot>
      <tbody style="background-color:#ABEBC6;">
         <tr>
            <td>Chocolates</td>
            <td>200</td>
         </tr>
         <tr>
            <td>Grocessary</td>
            <td>900</td>
         </tr>
      </tbody>
   </table>
</body>
</html>

Align Body Content

Let’s look at another example where we are going to align the content inside the <tbody> tag to centre using CSS.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #A569BD;
      }
   </style>
<body>
   <table style="width:50%">
      <thead>
         <tr>
            <th>Students</th>
            <th>Marks</th>
         </tr>
      </thead>
      <tbody style="text-align:center">
         <tr>
            <td>Maya</td>
            <td>75</td>
         </tr>
         <tr>
            <td>Raj</td>
            <td>76</td>
         </tr>
         <tr>
            <td>Suresh</td>
            <td>77</td>
         </tr>
      </tbody>
   </table>
</body>
</html>

Vertical alignment of Body Element

In the following example, we are going to use the CSS vertical align property to make the content inside the <tbody> tag align to the centre.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #2ECC71;
      }
   </style>
<body>
   <table style="width:50%;">
      <tr>
         <th>Employee</th>
         <th>Department</th>
      </tr>
      <tbody style="vertical-align:middle">
         <tr style="height:100px">
            <td>Maya</td>
            <td>InformationTechnology</td>
         </tr>
         <tr style="height:100px">
            <td>Ram</td>
            <td>Analyst</td>
         </tr>
         <tr style="height:100px">
            <td>Suresh</td>
            <td>Associate</td>
         </tr>
      </tbody>
      </table>
   </body>
</html>

Supported Browsers

Tag Chrome Edge Firefox Safari Opera
tbody Yes Yes Yes Yes Yes
html_tags_reference.htm
Advertisements