Re: tbody?

by Andrew McFarland <aamcf(at)aamcf.co.uk>

 Date:  Tue, 25 Jun 2002 23:19:53 +0100
 To:  <hwg-basics(at)hwg.org>
 References:  weddingchannel
  todo: View Thread, Original
At 15:52 25/06/02 -0400, Keith Sellars wrote:
>Excuse me if this is a *dumb* question.  I've been designing for almost 5
>years now and for some reason have never looked into this.  What is the
>tbody tag used for and why is it even needed?

Ah, the joys of SGML :-)

The tbody element is a child of the table element. It contains the body of 
the table content, as opposed to header information (which in in thead) and 
footer (tfoot). If you are using HTML 4.01 you have been using it even if 
you didn't realize it :-)

In the HTML 4.01 DTD[1] the element type declaration for `TABLE' is:

<!ELEMENT TABLE - -
      (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)>

This content model means a TABLE element must contain (in this order)

     o An optional CAPTION element;
     o Either one or more COLs,
         or   one or more COLGROUPs,
         or   neither,
         but  *not both*;
     o An optional THEAD element;
     o An optional TFOOT element;
     o At least one TBODY element.

That is quite surprising. Every table has at least one TBODY element. 
However, the following code is valid HTML 4.01 (without the `.'s of course ):

<.table><.tr><.td>Foo</.td></.tr></.table>

There is a TBODY element in there, but there are no TBODY tags. Confused? :-)

To understand what is going on you need to know the difference between an 
element and a tag. The element is the abstract concept, and the tag is the 
string of text (sometimes) used to mark the beginning and /or end of the 
element.

Under some circumstances in HTML (but not XHTML) you can omit the end tag. 
The element still ends, because there is an unambiguous point in the 
document where the element ends. This is why you can leave out the closing 
<./head> - the head always closes when the body element is found.

<.html>
<.head><.title>Foo<./title>
<.body>
<.h1>Foo<./h1>
<./body>
<./html>

The HEAD element cannot contain a body element, so the parser knows the 
HEAD element must end immediately before the opening body tag.

More rarely, you can leave out the opening tag. Think about:

<.html>
<.head>
</title>Foo<./title>
<.h1>Foo<./h1>
<./body>
<./html>

The H1 element cannot be contained within the HEAD element, so the parser 
knows to implicitly close the HEAD and implicitly open the BODY.[2]

The TBODY element has optional opening and closing tags. This means in the 
example from aboive, <.table><.tr><.td>Foo</.td></.tr></.table>, the parser 
knows that the TBODY element starts immediately before the first TR and 
ends immediately after the last.

So you are using TBODY elements even if you are never using <.tbody>.

IN XHTML the situation is different. There are no optional opening and 
closing tags. They must all be present. The content model for the table 
element in XHTML is different though. tr can be directly inside a table, 
which is why in XHTML you don't need to use tbody.

Andrew

[1] http://www.w3.org/TR/html4/sgml/dtd.html

[2] This is why using a <NOSCRIPT> before a <META> produces a validation 
error. The presence of a <NOSCRIPT> before a closing <./HEAD> or an opening 
<.BODY> closes the HEAD and opens the BODY. You can't put METAs in the 
body, and hence the error.


--
http://aamcf.co.uk/

HTML: hwg-basics mailing list archives, maintained by Webmasters @ IWA