views:

58

answers:

3

Hello!

Does XHTML have an 'opinion' regarding the use of <thead>, <tfoot> and <tbody>?

Is there a case where they should be used?

Thank you.

+4  A: 

They allow you to add semantics to your table, and also allow you to style the head and the foot of the table without introducing redundant classes/ids.

I can't think of a situation where you have to use it, although I know some jQuery plugins use the head & foot to control behaviors.

If your tabular data needs headings and summary rows, use them, if not don't

Glenn Slaven
+2  A: 

The only rules, that I'm aware of, is that the thead (if used) must be defined first, and the tfoot (if used) before the tbody (somewhat counter-intuitive, to my mind, but them's the rules).

I think that the purpose of thead is partially for print purposes, allowing columns printed on a second page to have the thead repeated, in order that the data makes more sense.

In theory it could also allow for a scrolling tbody in the case of long tables, with fixed headers:

This division enables user agents to support scrolling of table bodies independently of the table head and foot. When long tables are printed, the table head and foot information may be repeated on each page that contains table data.

Source: http://www.w3.org/TR/html401/struct/tables.html#edef-TFOOT

This does not, however, work currently (without using at least two tables, I think).

The largest benefit, though, as @Glenn Slaven notes, seems to be semantic.

David Thomas
The idea of putting `<tfoot>` before `<tbody>` is so that summary (footer) information can be rendered while a large data table is still downloading
Gareth
@Gareth, yeah. It makes *sense* to do it that way, but it just feels, as I say, counter-intuitive is all. =)
David Thomas
Note that it's valid to put the `tfoot` either before or after the `tbody` s in (X)HTML5. Gareth's reasoning is sound, but like David, most people found the HTML4 way of doing it counter-intuitive, so the rules were relaxed. It makes no difference to the way browsers work, it's just a validity change.
Alohci
+1  A: 

The thead, tbody, and tfoot elements in HTML are used to group table rows into logical sections based on their content. There are two main reasons you'd want to do this:

  • To allow the body to be scrolled independently of the header and/or
    footer
  • To make it easier to apply different style rules to the different sections of the table.

as stated here http://stackoverflow.com/questions/2700379/what-is-benefit-of-thead/2700400#2700400

If you are using tables to make layout then don't use these. If you are showing tabular data then use it.

And if you don't have anything to put in tfoot then don't add this.

You will find some good answers here also http://stackoverflow.com/questions/2700379/what-is-benefit-of-thead

metal-gear-solid
Thank you m-s-g. So, if I write `<table><tr><td></td></tr></table>`, the `<tbody>` will not get added automatically by the browser? IE for example does this when you inspect a table with the Developer Tool.
Francisc
@Francisc: No, an implicit `<tbody>` always gets added.
DisgruntledGoat
Alright, thank you.
Francisc