Gazow
Gazow

Reputation: 1079

Proper html markup of table tags?

Kind of a silly question, but I've been seeing things such as tbody and thead/tfoot tags in other peoples tables.

Are these required even if they're empty for good markup? Or can I just leave them out?

Upvotes: 4

Views: 2832

Answers (6)

T.T.T.
T.T.T.

Reputation: 34513

The <tbody> tag is only partially supported in all major browsers.
Tables may have multiple bodies, but when it only has one single body, the HTML tbody tag may be safely omitted.

Upvotes: 0

Guffa
Guffa

Reputation: 700152

The table sections (thead/tbody/tfoot) are optional. The table caption (caption) and column definitons (col/colgroup) are also optional.

In HTML (but not XHTML) the closing tag for rows and cells are also optional, so you could write a table as:

<table>
  <tr>
    <th>1
    <th>2
  <tr>
    <td>3
    <td>4
  <tr>
    <td>5
    <td>6
</table>

It's however recommended that you close the tags to get better structure in the code. It also makes it a lot easier if you decide to change to XHTML.

Upvotes: 3

Matteo Riva
Matteo Riva

Reputation: 25060

Those tags are not required and a page would validate even without them.

Upvotes: 2

Julius A
Julius A

Reputation: 39602

Yes, that is proper markup. Though they are optional You can read further on the W3Schools page.

Upvotes: 0

Tobias P.
Tobias P.

Reputation: 4665

if you really need tables, you should use them. If you use tables only for design purposes you should switch to css-markup.

A simple correct table example:

<table>
  <thead>
    <tr>
      <td>id</td>
      <td>name</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>User1</td>
    </tr>
    <tr>
      <td>2</td>
      <td>User2</td>
    </tr>
  </tbody>
</table>

Upvotes: 0

sarahjean
sarahjean

Reputation: 167

They aren't required, but they let you do some more advanced things with headers and footers: http://www.htmldog.com/guides/htmladvanced/tables/

Upvotes: 2

Related Questions