/

HTML / CSS / JavaScript Tutorial

HTML Element: <tbody>

[this page | pdf | back links]

The HTML <tbody> element indicates the body of a table. It appears inside a <table> element and is used in conjunction with <tfoot> and <thead> elements to differentiate between different parts of the table. This can allow browsers to scroll the table body independently of the header and footer, or to allow printing of the header and footer at the top and bottom of each page. A <tbody> element needs to come after any <caption>, <colgroup> and <thead> elements. It needs to contain one or more <tr> elements.

 

The attributes it can take are HTML global attributes and HTML event attributes.

 

It used to support the align, char, charoff and valign attributes, but these are no longer supported by HTML 5.

 

To create or access such an element in JavaScript see here. The corresponding HTML DOM object supports standard DOM properties and methods. The default style applicable to this element is shown here.

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,td,th,tr,caption {border: thin solid black; border-collapse: collapse;}
th {background-color: yellow;}
thead {color: blue}
tbody {color: black}
tfoot {color: green}
</style>
</head>
<body>
Created using HTML:<br>
<table>
  <caption>A table</caption>
  <thead>
    <tr><th>col1</th><th>col2</th></tr>
    <tr><td>1</td><td>2</td></tr>
  </thead>
  <tbody>
    <tr><td>A</td><td>B</td></tr>
  </tbody>
  <tfoot>
    <tr><td>a</td><td>b</td></tr>
  </tfoot>
</table>

<br>Created using JavaScript:<br>
<span id="element"></span>

<script>
var y1 = document.createElement("CAPTION"); y1.textContent = "A table";
var y2a1 = document.createElement("TH"); y2a1.textContent = "col1";
var y2a2 = document.createElement("TH"); y2a2.textContent = "col2";
var y2a = document.createElement("TR"); y2a.appendChild(y2a1); y2a.appendChild(y2a2);
var y2b1 = document.createElement("TD"); y2b1.textContent = "1";
var y2b2 = document.createElement("TD"); y2b2.textContent = "2";
var y2b = document.createElement("TR"); y2b.appendChild(y2b1); y2b.appendChild(y2b2);
var y2 = document.createElement("THEAD"); y2.appendChild(y2a); y2.appendChild(y2b);
var y3a1 = document.createElement("TD"); y3a1.textContent = "A";
var y3a2 = document.createElement("TD"); y3a2.textContent = "B";
var y3a = document.createElement("TR"); y3a.appendChild(y3a1); y3a.appendChild(y3a2);
var y3 = document.createElement("TBODY"); y3.appendChild(y3a);
var y4a1 = document.createElement("TD"); y4a1.textContent = "a";
var y4a2 = document.createElement("TD"); y4a2.textContent = "b";
var y4a = document.createElement("TR"); y4a.appendChild(y4a1); y4a.appendChild(y4a2);
var y4 = document.createElement("TFOOT"); y4.appendChild(y4a);
var x = document.createElement("TABLE");
x.appendChild(y1); x.appendChild(y2); x.appendChild(y3); x.appendChild(y4);
document.getElementById("element").appendChild(x);
</script>

</body>
</html>


NAVIGATION LINKS
Contents | Prev | Next | HTML Elements


Desktop view | Switch to Mobile