The HTML Table Body element (<tbody>
) encapsulates a set of table row (<tr>
elements, indicating that they comprise the body of the table (<table>
).
The <tbody>
element, along with its cousins <thead>
and <tfoot>
, provide useful semantic information that can be used when rendering for either screen or printer as well as for accessibility purposes.
Content categories | None. |
---|---|
Permitted content | Zero or more <tr> elements. |
Tag omission | The <tbody> element is not a required child element for a parent <table> element to graphically render. However, it must not be present, if its parent <table> element has a <tr> element as a child. |
Permitted parents | Within the required parent <table> element, the <tbody> element can be added after a <caption> , <colgroup> , <thead> and a <tfoot> element. |
Permitted ARIA roles | Any |
DOM interface | HTMLTableSectionElement |
This element includes the global attributes.
align
in HTML 4.01, in HTML5
left
, aligning the content to the left of the cellcenter
, centering the content in the cellright
, aligning the content to the right of the celljustify
, inserting spaces into the textual content so that the content is justified in the cellchar
, aligning the textual content on a special character with a minimal offset, defined by the char
and charoff
attributes.If this attribute is not set, the left
value is assumed.
left
, center
, right
or justify
values, use the CSS text-align
property on it.char
value, in CSS3, you can use the value of the char
as the value of the text-align
property Unimplemented.bgcolor
black = "#000000" |
green = "#008000" | ||
silver = "#C0C0C0" |
lime = "#00FF00" | ||
gray = "#808080" |
olive = "#808000" | ||
white = "#FFFFFF" |
yellow = "#FFFF00" | ||
maroon = "#800000" |
navy = "#000080" | ||
red = "#FF0000" |
blue = "#0000FF" | ||
purple = "#800080" |
teal = "#008080" | ||
fuchsia = "#FF00FF" |
aqua = "#00FFFF" |
<tbody>
element should be styled using CSS. To give a similar effect to the bgcolor attribute, use the CSS property background-color
, on the relevant <td>
or <th>
elements.char
in HTML 4.01, in HTML5
align
is not set to char
, this attribute is ignored. char
, in CSS3, you can use the character set using the char
attribute as the value of the text-align
property Unimplemented.charoff
in HTML 4.01, in HTML5
valign
in HTML 4.01, in HTML5
baseline
, which will put the text as close to the bottom of the cell as it is possible, but align it on the baseline of the characters instead of the bottom of them. If characters are all of the size, this has the same effect as bottom
.bottom
, which will put the text as close to the bottom of the cell as it is possible;middle
, which will center the text in the cell;top
, which will put the text as close to the top of the cell as it is possible.vertical-align
property on it.<thead>
block (to semantically identify header rows), the <tbody>
block must come after it.<tbody>
, you can't also have table rows (<tr>
elements) which are direct children of the <table>
but not included inside the <tbody>
. All non-header and non-footer rows must be inside the <tbody>
if one is used.<thead>
and <tfoot>
elements specify information that may be the same—or at least very similar—on every page of a multi-page table, while the <tbody>
element's contents generally will differ from page to page.<thead>
, <tbody>
, <tfoot>
, and <caption>
blocks separately from one another for the same parent table.<tbody>
per table as long as they are all consecutive. This lets you divide the rows in large tables into sections, each of which may be separately formatted if so desired.Below are some examples showing the use of the <tbody>
element. For more examples of this tag in use, see the examples for <table>
.
In this relatively simple example, we create a table listing information about a group of students with a <thead>
and a <tbody>
, with a number of rows in the body.
The table's CSS is shown here. Note that all of the body cells including information about students are contained within a single <tbody>
element.
<table> <thead> <tr> <th>Student ID</th> <th>Name</th> <th>Major</th> </tr> </thead> <tbody> <tr> <td>3741255</td> <td>Jones, Martha</td> <td>Computer Science</td> </tr> <tr> <td>3971244</td> <td>Nim, Victor</td> <td>Russian Literature</td> </tr> <tr> <td>4100332</td> <td>Petrov, Alexandra</td> <td>Astrophysics</td> </tr> </tbody> </table>
The CSS to style our table is shown next.
table { border: 2px solid #555; border-collapse: collapse; font: 16px "Lucida Grande", "Helvetica", "Arial", sans-serif; }
First, the table's overall style attributes are set, configuring the thickness, style, and color of the table's exterior borders and using border-collapse
to ensure that the border lines are shared among adjacent cells rather than each having its own borders with space in between. font
is used to establish an initial font for the table.
th, td { border: 1px solid #bbb; padding: 2px 8px 0; text-align: left; }
Then the style is set for the majority of the cells in the table, including all data cells but also those styles shared between our <td>
and <th>
cells. The cells are given a light gray outline wh9ich is a single pixel thick, padding is adjusted, and all cells are left-aligned using text-align
thead > tr > th { background-color: #cce; font-size: 18px; border-bottom: 2px solid #999; }
Finally, header cells contained within the <thead>
block are gtiven additional styling. They use a darker background-color
, a larger font size, and a thicker, darker bottom border than the other cell borders.
The resulting table looks like this:
You can create multiple sections within a table by using multiple <tbody>
elements. Each may potentially have its own header row or rows; however, there can be only one <thead>
per table! Because of that, you need to use a <tr>
filled with <th>
elements to create headers within each <tbody>
. Let's see how that's done.
Let's take the previous example, add some more students to the list, and update the table so that instead of listing each studen't major on every row, the students are grouped by major, with heading rows for each major.
First, the resulting table, so you know what we're building:
The revised HTML looks like this:
<table> <thead> <tr> <th>Student ID</th> <th>Name</th> </tr> </thead> <tbody> <tr> <th colspan="2">Computer Science</th> </tr> <tr> <td>3741255</td> <td>Jones, Martha</td> </tr> <tr> <td>4077830</td> <td>Pierce, Benjamin</td> </tr> <tr> <td>5151701</td> <td>Kirk, James</td> </tr> </tbody> <tbody> <tr> <th colspan="2">Russian Literature</th> </tr> <tr> <td>3971244</td> <td>Nim, Victor</td> </tr> </tbody> <tbody> <tr> <th colspan="2">Astrophysics</th> </tr> <tr> <td>4100332</td> <td>Petrov, Alexandra</td> </tr> <tr> <td>8892377</td> <td>Toyota, Hiroko</td> </tr> </tbody> </table>
Notice that each major is placed in a separate <tbody>
block, the first row of which contains a single <th>
element with a colspan
attribute that spans the entire width of the table. That heading lists the name of the major contained within the <tbody>
.
Then each remaining row in each major's <tbody>
consists of two cells: the first for the student's ID and the second for their name.
Most of the CSS is unchanged. We do, however, add a slightly more subtle style for header cells contained directly within a <tbody>
(as opposed to those which reside in a <thead>
. This is used for the headers indicating each table section's corresponding major.
tbody > tr > th { background-color: #dde; border-bottom: 1.5px solid #bbb; font-weight: normal; }
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'tbody element' in that specification. | Living Standard | |
HTML5 The definition of 'tbody element' in that specification. | Recommendation |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1 | Yes | 1 | Yes | Yes | Yes |
align |
? | Yes | No1 | Yes | ? | ? |
bgcolor |
? | No | No | Yes | ? | ? |
char |
? | Yes | No2 | Yes | ? | ? |
charoff |
? | Yes | No2 | Yes | ? | ? |
valign |
? | Yes | No1 | Yes | ? | ? |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | 4 | Yes | Yes | ? |
align |
? | ? | Yes | No1 | ? | ? | ? |
bgcolor |
? | ? | No | No | ? | ? | ? |
char |
? | ? | Yes | No2 | ? | ? | ? |
charoff |
? | ? | Yes | No2 | ? | ? | ? |
valign |
? | ? | Yes | No1 | ? | ? | ? |
1. See bug 915
2. See bug 2212
<caption>
, <col>
, <colgroup>
, <table>
, <td>
, <tfoot>
, <th>
, <thead>
, <tr>
;<tbody>
element: :nth-child
pseudo-class to set the alignment on the cells of the column;text-align
property to align all cells content on the same character, like '.'.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/tbody