Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it valid to use more than one thead or tfoot element in a table?

Imagine a list of lists similar to this:

var list = [
  { name: 'group1', 
    items: [ 1, 2, 3, 4, 5 ]
  },
  { name: 'group2', 
    items: [ 1, 2, 3, 4, 5 ]
  },
  etc...
]

Now forgetting the whole "tables are for data not design" argument, I wanted to display a single table for list and have a seperate <thead> and <tbody> for each entry in list.

Is this technically valid? This works in the browser, but my spider senses are tingling on this one.

like image 715
AlbertEngelB Avatar asked Apr 22 '13 19:04

AlbertEngelB


People also ask

Can a table have more than one thead?

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

Can I have multiple tbody?

Use multiple tbody sections when rules are needed between groups of table rows. As of the HTML5 spec, this changes slightly, but the fundamental "yes, multiple tbody elements are fine) remains. Specifically, you're now allowed to put the one tfoot element after the tbody if you like.

What is the purpose of thead Tfoot and tbody elements?

The thead, tbody, and tfoot elements in HTML are used to group table rows into logical sections based on their content.

Is Tfoot required?

Those tags are not required. It is considered good form to use them if the table is used to represent data, which is what a table should be used for. If a table is used for laying out content they are typically omitted.

How to display summary of the table content using <tfoot> element?

<tfoot> element is used with combination of <thead> and <tbody> which will define every elements of table like header, footer and body. This tag will use one or more <tr> elements within <tfoot> elements, so it will help to display a summary of the table content. It should be placed at the bottom of the table.

What is the use of tfoot tag in table?

Among them <tfoot> is one of the important elements in the table for displaying the summary part of the table columns. <tfoot> tag must be placed after <caption>, <thead>, <colgroup> tag in the table.

Is there a difference between multiple <Tbody>elements in a table?

@metal: no, there is a semantic difference - multiple <tbody>elements describes separate groups in the table, as was explained in the answer. Also I should add that it's generally better to target cells for backgrounds, so the CSS should be, for example, tbody:nth-child(odd) td { background: #f5f5f5; }

Are multiple Tbody elements allowed in HTML5?

As of the HTML5 spec, this changes slightly, but the fundamental "yes, multiple tbody elements are fine) remains. Specifically, you're now allowed to put the one tfoot element after the tbody if you like.


Video Answer


2 Answers

You can have as many <tbody> elements as you want, but no more than one each of <thead> and <tfoot>. Reference:

Content model:

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.

like image 190
Jon Avatar answered Oct 18 '22 20:10

Jon


There's at most one thead and one tfoot allowed, so you shouldn't create additional headers. After all the th in a thead gives a meaning to your columns, like "time of day", "temperature", "amount of cats currently on fire".

If the entries in your list are related they should all be in the same table and you should provide a fitting header to display that relation.

If the entries are actually unrelated you should provide a single table for every of those. You can still apply the same CSS on every table to get a nice result.

like image 12
Zeta Avatar answered Oct 18 '22 18:10

Zeta