Is it necessary to have <tbody>
in every table? According to Standards.
You may use more than one <tbody> per table as long as they are all consecutive.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With