30

I viewed the html source code, there is no <tbody>, but when viewed via firebug in the HTML tab, <tbody> appears. Any idea why?

mauris
  • 42,982
  • 15
  • 99
  • 131
Mask
  • 33,129
  • 48
  • 101
  • 125
  • (From @baptx): The weird thing is that creating a table without tbody using javascript createElement/appendChild will not add tbody but using innerHTML will do it. Why? I've asked question here: http://stackoverflow.com/questions/9053572/no-automated-tbody-with-createelement-appendchild – Dan Blows Jan 29 '12 at 16:40
  • This annoyed me *so much* when I was trying to compare my HTML parser's output to Firefox's HTML. – lesderid Mar 16 '13 at 11:20
  • 1
    possible duplicate of [Why do browsers insert tbody element into table elements?](http://stackoverflow.com/questions/938083/why-do-browsers-insert-tbody-element-into-table-elements) – Ciro Santilli OurBigBook.com Jul 20 '14 at 11:30

5 Answers5

20

To summarize the excellent explanations given in the answers and comments by bobince, Kieron, Alohci and others:

  1. Firebug just displays the DOM of the parsed page. Due to complicated HTML parsing rules, the DOM will "differ" (in some sense) from the source HTML.
  2. In this case the TBODY element in the DOM is added by the HTML parser. Note that this weird parsing is limited to text/html documents and in XHTML the DOM corresponds closely to the source XML.
    • This behavior was specified in HTML 4. The content model (allowed children) for table is (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+) -- trs are only allowed in tbody! The spec says that tbody's start tag is optional, which is supposed to mean that if the HTML parser encounters tr directly inside a table it inserts the tbody start tag omitted by the author.
    • To make matters more clear HTML 5 defines very detailed parsing rules, in particular for this case: "When the insertion mode is "in table", tokens must be handled as follows: [...] A start tag whose tag name is one of: "td", "th", "tr" -> Act as if a start tag token with the tag name "tbody" had been seen, then reprocess the current token."
Nickolay
  • 31,095
  • 13
  • 107
  • 185
6

Its not firebug, but firefox which does that. This is the way tables are supposed to be written with <TBODY> separate from meta data like <COLGROUP> Firefox simply inserts the <TBODY> tags when it finds them missing.

KJ Saxena
  • 21,452
  • 24
  • 81
  • 109
  • 8
    Is not Firefox, but Gecko :P – Christian C. Salvadó Nov 05 '09 at 05:23
  • 8
    As for "why": Tables are supposed to have a `TBODY`, so Firefox' inner workings *want one to be there*. If you're not specifying it explicitly, it'll create it on its own, because it *wants one to be there*. Even if you're fine eating off the floor, you're placed at a table, because the waiter *wants a table to be there*. ;o) – deceze Nov 05 '09 at 05:42
  • @deceze, that's Firefox deciding that. The W3C standard says no such ting - tbody is optional unless you have a thead or tfoot. – paxdiablo Nov 05 '09 at 06:49
  • @paxdiabolo: This can quickly slide into fine semantics, but: The standard **requires** the use of `TBODY`, **except for** when it's obvious that the only data in the table *is* the `TBODY` (paraphrased, as per your own comment on Kierons answer). That sounds like quite a strong encouragement to me and IMHO may be summarized with "is supposed to be there". – deceze Nov 05 '09 at 09:05
  • If the standard says it doesn't have to be there, it doesn't have to be there. Period. What people or browsers infer is irrelevant. The statement that tables are "supposed to be written with