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

6

50

According to standards, is it necessary to have <tbody> in every table?

Tungstite answered 20/6, 2010 at 3:14 Comment(1)
Does this answer your question? Is it required to use thead, tbody and tfoot tags?Geomancy
E
62

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.

Electroluminescence answered 20/6, 2010 at 3:15 Comment(6)
and is <th> allowed to use inside tbody tr?Tungstite
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.Electroluminescence
@Aaron Harun - I just noticed firefox always add tbody to table even if we are not using <thead>, <tfoot>, and multiple <tbodies>Tungstite
@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.Outsmart
@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.Outsmart
Don't really know since when, but most browsers add tbody automatically. #67335857 led me here by the way (I asked ask it first)... With child selector I can have side-effects not to put it then : e.g. table > tr would not match. This is also mentioned here : #938583.Pneumato
H
24

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>

See also: MDN

Hibernia answered 20/6, 2010 at 3:27 Comment(0)
B
19

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.

Bausch answered 16/5, 2012 at 16:14 Comment(0)
G
1

Dumb Guy gave an answer for HTML4 (yes). Arwym gives an answer for HTML5 to a related question (no):

The tabular data spec for HTML5 does not require them:

Contexts in which this element (tr) can be used:

  • As a child of a thead element.
  • As a child of a tbody element.
  • As a child of a tfoot element.
  • As a child of a table element, after any caption, colgroup, and thead elements, but only if there are no tbody elements that are children of the table element.

Even though I believe it is a good practice to section your rows within thead, tbody and tfoot tags as it makes the table's rows easier to identify.

In the end, the browser will always add at least the tbody for you.

Geomancy answered 15/11, 2020 at 10:14 Comment(0)
E
1

According to HTML 3.2 spec (table wasn't in HTML 2 spec) table element doesn't have tbody, thead, tfoot (they are HTML 4 things), only optional caption and list of tr / th.

While you might think what's the hell you are talking about HTML 3.2 dated 1997 in 2021 consider email clients with primitive or outdated HTML engines, tbody makes no sense in here.

Earshot answered 2/9, 2021 at 10:35 Comment(0)
G
-5

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

Gloriagloriana answered 20/6, 2010 at 3:24 Comment(1)
How can adding tbody tags to a table save you any money/time?Hyde

© 2022 - 2024 — McMap. All rights reserved.