Is it wrong to use more than one <thead>, <tbody> or <tfoot> in a table?

Asked

Viewed 890 times

13

I have a table that gets a certain formatting when the elements have a tbody. Because of this formatting, I thought to use the tbody twice on the same table, but I wondered if this would be valid.

So I’d like to clear that doubt: Can I use more than one tbody, tfoot or thead in the same table?

For example:

<table class="table">
     <thead>
           <tr>
              <td colspan=2>
                 <h1>Título</h1>
             </td>
            </tr>
     </thead>
     <thead>
         <tr>
             <th>ID</th>
             <th>Nome</th>
         </tr>
     </thead>
    <tbody>
      <tr>
          <td>1</td>
          <td>Wallace</td>
      </tr>
    </tbody>
</table>

In the above example, it would be invalid to use two thead?

3 answers

13

According to the DTD:

<!ELEMENT table
     (caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))>
<!ELEMENT caption  %Inline;>
<!ELEMENT thead    (tr)+>
<!ELEMENT tfoot    (tr)+>
<!ELEMENT tbody    (tr)+>
<!ELEMENT colgroup (col)*>
<!ELEMENT col      EMPTY>
<!ELEMENT tr       (th|td)+>
<!ELEMENT th       %Flow;>
<!ELEMENT td       %Flow;>

(thead?) Aceito 0 ou 1 ocorrêcia

(tfoot?) Idem a anterior

(tbody+|tr+) Aceito 1 ou mais ocorrências

12


<tbody> yes, but <thead> and <tfoot> is not allowed by the HTML5 specification (the same was true for the HTML4).

In this order: Optionally a caption element, Followed by zero or more colgroup Elements, Followed Optionally by a thead element, Followed Optionally by a tfoot element, Followed by either zero or more tbody Elements or one or more tr Elements, Followed Optionally by a tfoot element (but there can only be one tfoot element Child in total), Optionally intermixed with one or more script-supporting Elements.

My emphasis on what is important. The specification is very clear when it lets you have more than one. When it uses the article to in English means one same, no more than a.

Possible solutions

You can put those two together <thead>, since there seems to be no reason to be separated, he may have more than one line.

It could be if there were groupings, but then there is the case of having a main table that contains several bodies and inside the bodies have other tables for each group, there in each of these secondary tables you can have the header and footer. You can nest as many tables as you want, on multiple levels.

Eventually you might want to use the <caption> also to add some information in the table.

8

Use more than one thead and tfoot are not allowed. According to specification of HTML:

In this order: Optionally a caption element, Followed by zero or more colgroup Elements, Followed Optionally by a thead element, Followed by either zero or more tbody Elements or one or more tr Elements, Followed Optionally by a tfoot element, Optionally intermixed with one or more script-supporting Elements.

That is to say:

In this order: optionally a caption element, followed by zero or more colgroup, followed optionally by one element thead, followed by zero or more elements tbody or one or more elements tr, optionally followed by an element tfoot, optionally mixed with one or more script support elements.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.