views:

67

answers:

3

Is it necessary to have <tbody> in every table? According to Standards.

+1  A: 

Only if you define thead and tfoot. It is mostly used when the table has multiple bodies of content. If the data in the table is easily understood to be the tbody then you can safely omit it.

Aaron Harun
and is `<th>` allowed to use inside `tbody tr`?
metal-gear-solid
Semantically, if you have a header to your table with `<th>`, then you should use `<thead>` though it is not *required* that you do so. If you are using them as the header for a column, then you should not put them in the `tbody`, but if they are headers for a row, they can be.
Aaron Harun
@Aaron Harun - I just noticed firefox always add `tbody` to table even if we are not using `<thead>`, `<tfoot>`, and multiple `<tbodies>`
metal-gear-solid
@metal-gear-solid - Not just Firefox, in the text/html serialization all browsers will infer the missing tbody tags. Not in the application/xhtml+xml serialization though, of course.
Alohci
@Alohci -so it's good to add `<tbody>` always
metal-gear-solid
@metal-gear-solid - Whether it's good or not is a matter of opinion and depends on your personal development philosophy. All I can provide are facts. It's unnecessary, but if you want to serve your web page with both mime types, then explicitly using tbody means that your DOM can have the same structure regardless of the serialization. This can make your scripting simpler.
Alohci
A: 

Most browsers are forgiving but even so I add the pair in all tables that I use now. Even trivial tables. Especially now that I'm using CSS more and more to decorate those tables.

All that being said I have old tables that still work fine on the newest browsers. I'm learning the hard way but taking the few extra Micro seconds to add the optional tags here and there ends up saving you money/time in the long run.

Dave

David Frantz
+1  A: 

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

So, you must have a <tbody> tag if you have a <thead> or <tfoot>

However, w3schools says that "The tag is only partially supported in all major browsers," so it is not recommended to use this if you want your code to work correctly in all browsers.

Dumb Guy
Seems that for w3schools all major browsers are those from Netscape and IE era ;)
takeshin