Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have multiple <tbody> in same <table>?

Can we have multiple <tbody> tags in same <table>? If yes then in what scenarios should we use multiple <tbody> tags?

like image 203
Jitendra Vyas Avatar asked Jun 19 '10 18:06

Jitendra Vyas


People also ask

Can you have Tbody inside a Tbody?

Nope, this is not possible. According to the spec, the TBODY element can contain only TR elements.

Is it necessary to have Tbody in every table?

Quoting the HTML 4 spec: "The TBODY start tag is always required except when the table contains only one table body and no table head or foot sections. The TBODY end tag may always be safely omitted."

Is Tbody a table tag?

The <tbody> tag is used to group the body content in an HTML table. The <tbody> element is used in conjunction with the <thead> and <tfoot> elements to specify each part of a table (body, header, footer). Browsers can use these elements to enable scrolling of the table body independently of the header and footer.

Why Tbody is added automatically?

The browser has to correct the code in order to create a DOM hieararchy from it. The code is incorrect, but how much depends on the DOCTYPE that you are using. There is no need to specify the tbody element, it's added automatically.


2 Answers

Yes you can use them, for example I use them to more easily style groups of data, like this:

thead th { width: 100px; border-bottom: solid 1px #ddd; font-weight: bold; }  tbody:nth-child(odd) { background: #f5f5f5;  border: solid 1px #ddd; }  tbody:nth-child(even) { background: #e5e5e5;  border: solid 1px #ddd; }
<table>      <thead>          <tr><th>Customer</th><th>Order</th><th>Month</th></tr>      </thead>      <tbody>          <tr><td>Customer 1</td><td>#1</td><td>January</td></tr>          <tr><td>Customer 1</td><td>#2</td><td>April</td></tr>          <tr><td>Customer 1</td><td>#3</td><td>March</td></tr>      </tbody>      <tbody>          <tr><td>Customer 2</td><td>#1</td><td>January</td></tr>          <tr><td>Customer 2</td><td>#2</td><td>April</td></tr>          <tr><td>Customer 2</td><td>#3</td><td>March</td></tr>      </tbody>      <tbody>          <tr><td>Customer 3</td><td>#1</td><td>January</td></tr>          <tr><td>Customer 3</td><td>#2</td><td>April</td></tr>          <tr><td>Customer 3</td><td>#3</td><td>March</td></tr>      </tbody>  </table>

You can view an example here. It'll only work in newer browsers, but that's what I'm supporting in my current application, you can use the grouping for JavaScript etc. The main thing is it's a convenient way to visually group the rows to make the data much more readable. There are other uses of course, but as far as applicable examples, this one is the most common one for me.

like image 148
Nick Craver Avatar answered Oct 17 '22 10:10

Nick Craver


Yes. From the DTD

<!ELEMENT table      (caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))> 

So it expects one or more. It then goes on to say

Use multiple tbody sections when rules are needed between groups of table rows.

like image 39
Martin Smith Avatar answered Oct 17 '22 09:10

Martin Smith