Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do browsers insert tbody element into table elements?

Tags:

html

xhtml

I was playing around with some ideas using raw html and JQuery. One thing I did was to create an table element with a set of rows.

<table id="MyTable" >     <tr>         <td>Title</td>     </tr>     <tr>         <td>1</td>     </tr>     <tr>         <td>2</td>     </tr>     <tr>         <td>3</td>     </tr>     <tr>         <td>4</td>     </tr> </table> 

But when I viewed the code in FireFox+Firebug, IE8 Developer Toolbar, or the Google Chrome JavaScript Debugger...all of them showed there to be a tbody element surrounding all of the tr nodes.

I'm not against this happening...but is this standard behavior?

like image 250
Chris Brandsma Avatar asked Jun 02 '09 06:06

Chris Brandsma


People also ask

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.

Is it necessary to have Tbody in every table?

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

Why we use tbody tag in HTML?

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.

What is the purpose of the thead element and tbody element?

The thead, tbody, and tfoot elements in HTML are used to group table rows into logical sections based on their content.


2 Answers

http://htmlhelp.com/reference/html40/tables/tbody.html:

The TBODY element defines a group of data rows in a table. A TABLE must have one or more TBODY elements, which must follow the optional TFOOT. The TBODY end tag is always optional. The start tag is optional when the table contains only one TBODY and no THEAD or TFOOT.

So there always is a tbody there (albeit sometimes with both the start and end tags optional and omitted), and the tools you are using are correct in showing it to you.

thead or tfoot, on the other hand, are never present unless you explicitly include them, and if you do that, the tbody(s) must be explicit too.

like image 96
ysth Avatar answered Oct 15 '22 21:10

ysth


Yes, tbody is the standard element indicating the body of a table. It is not required to put it in the markup, but it will be included in the DOM as you've seen.

like image 20
Matthew Flaschen Avatar answered Oct 15 '22 19:10

Matthew Flaschen