Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a tbody be inside another one?

Tags:

html

People also ask

Can we have 2 Tbody in a table?

You may use more than one <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.

What can be inside Tbody?

The tbody contains the body, or primary content, of an HTML table . Use it along with the thead and tfoot elements to add structure and semantic meaning to HTML tables. The tbody should contain the primary data presented in the table while the thead contains column headings and the tfoot contains summary data.

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."

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.


While this may work in practice, it is not legal HTML.

However, you are allowed to have multiple TBODY elements in a single TABLE element, so you could do this:

<table> 
  <tbody class="show"> 
    <tr> 
      <td>...</td> 
      <td>...</td> 
    </tr> 
  </tbody>
  <tbody class="hide"> 
    <tr> 
      <td>...</td> 
      <td>...</td> 
    </tr> 
  </tbody>
  <tbody class="show"> 
    <tr> 
      <td>...</td> 
      <td>...</td> 
    </tr> 
  </tbody>
</table> 

Alternatively, you may be able to nest tables, although I wouldn't really recommend that.


The way would be to create another table and use tbody afterwards:

<table>
  <tbody id="outer" class="show">
    <tr>
      <td>...</td>
      <td>...</td>
    </tr>
    <tr>
      <td colspan="2">
         <table>
        <tbody id="inner" class="hide">
         <tr>
           <td>...</td>
           <td>...</td>
         </tr>
       </tbody> <!-- inner -->
         </table>
      </td>
    </tr>
    <tr>
      <td>...</td>
      <td>...</td>
    </tr>
  </tbody> <!-- outer -->
</table>

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

<!ELEMENT TBODY    O O (TR)+           -- table body -->

what do you want to achieve?


I'm afraid you can't do such thing.

According to W3C a tbody can't inside another one.