Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 698
Jitendra Vyas Avatar asked Jun 20 '10 03:06

Jitendra Vyas


People also ask

Can one table have multiple tbody?

You may use more than one <tbody> per table as long as they are all consecutive.

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.

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.

Where is the tbody tag used?

The <tbody> tag is used to group the body content in an HTML table. The <tbody> element is used in conjunction with the <thead> and <tfoot> elements to specify each part of a table (body, header, footer). Browsers can use these elements to enable scrolling of the table body independently of the header and footer.


2 Answers

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.

like image 185
Aaron Harun Avatar answered Oct 23 '22 11:10

Aaron Harun


For the small fraction of your users still using IE7, you MUST add encapsulate your tr's in a tbody tag if you're building a table with the DOM methods!

This will work in all major browsers:

var table = document.createElement('table'); var tbody = document.createElement('tbody'); var tr = document.createElement('tr'); tbody.appendChild(tr); table.appendChild(tbody); 

This will NOT work in IE7:

var table = document.createElement('table'); var tr = document.createElement('tr'); table.appendChild(tr); 

A quick blog post of mine on building tables:
http://blog.svidgen.com/2012/05/building-tables-in-ie7-with-javascript.html

It may be notable that I no longer make the effort to support IE7 on my own projects. The IE<=7 share is likely negligible for most sites at this point.

like image 39
svidgen Avatar answered Oct 23 '22 11:10

svidgen