32

What are the main reasons someone would use HTML's tbody in a table? Is it just for formatting purposes?

I use head and body generally; I haven't used thead and tbody.

freginold
  • 3,946
  • 3
  • 13
  • 28
Daniel Kivatinos
  • 24,088
  • 23
  • 61
  • 81

6 Answers6

39

To give semantic meaning to your table:

<table>
  <thead>
    <tr>
      <th>Person</th>
      <th>Amount</th>
      <th>Date</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Person1</td>
      <td>$5.99</td>
      <td>02/03/09</td>
    </tr>
    <tr>
      <td>Person2</td>
      <td>$12.99</td>
      <td>08/15/09</td>
    </tr>
  </tbody>
</table>

According to the specification:

Table rows may be grouped into a table head, table foot, and one or more table body sections, using the THEAD, TFOOT and TBODY elements, respectively. This division enables user agents to support scrolling of table bodies independently of the table head and foot. When long tables are printed, the table head and foot information may be repeated on each page that contains table data.

The table head and table foot should contain information about the table's columns. The table body should contain rows of table data.

When present, each THEAD, TFOOT, and TBODY contains a row group. Each row group must contain at least one row, defined by the TR element.

Besides the important semantic importance of this (think screen readers), you can then easily style your headers apart from your data rows. Another relevant example is jQuery plugins such as the Tablesorter (original, archived) plugin can then easily figure out what your data structure looks like.

drac_o
  • 427
  • 5
  • 11
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • 3
    also you can use multiple tbody to group table rows that have a common function and style them accordingly. http://www.w3.org/TR/html4/struct/tables.html#edef-TBODY – Josh Robinson May 05 '11 at 20:02
11

One great use I've found for tbody is to allow for the rows to be scrollable while keeping the column header and footer visible. This only works in browsers that actually support css though. Sorry no Internet Explorer!

<tbody style="height: 150px; overflow-y: scroll">
James
  • 12,636
  • 12
  • 67
  • 104
  • Isn't the default IE8 mode quirks mode? If so then unless the user turns that off it's still not CSS compliant, no? – James May 29 '09 at 00:29
  • Don't think so. Or, at least as long as there is a doctype (which there should be), it is standards compliant. – Arve Systad May 29 '09 at 10:58
  • 5
    This is simply not true. I've tried this in the latest versions of Firefox, IE, and Chrome; and this doesn't work in any of them. – Sildoreth Apr 06 '15 at 21:15
  • Man I wish this was true. If it were then yes, these two elements would have some real purpose. Anyone have a fiddle of this working? Because I've never seen this work. I've only gotten sticky headers to work by wrapping two tables in divs and using overflow true on the second div. – Bret May 21 '17 at 00:40
5

What if the header of the table is the first column in every row? In that case there's no way to specify the header of the table. It will actually break the HTML since the parser will assume that everything is part of the body of the table.

And why exactly is that tag necessary since the th specifies the header fields - the browser can find out which part of the table is the header by these fields.

What's the point of thead and tbody? They work for some cases and break the HTML in other cases. They duplicate the functionality of th. They complicate the code and may cause problems - Before I knew about tbody I assumed that tr-s are children of table.

martinkunev
  • 1,364
  • 18
  • 39
  • 1
    This would be a good question, as opposed to an answer to this question. – Andrew Grimm Apr 13 '17 at 01:28
  • @AndrewGrimm This was the first thing I wrote in stackoverflow and at the time I didn't know the site - I didn't know I was supposed to answer so I just wrote a comment :) – martinkunev Apr 13 '17 at 08:56
4

To separate the header (thead), body (tbody) and footer (tfoot) parts of an HTML table. This is incredibly useful. A typical use is to put your column headers in your thead element and the data in tbody. The tfoot element is less common but sometimes useful.

By the way, even if you don't specify tbody, it is implicitly created for you anyway.

You can use this for styling purposes too eg:

table thead tr { background-color: yellow; }
table tbody tr { background-color: #C9C9C9; }

It's also useful in Javascript and jQuery for having tables with selectable rows (just as one example). You're generally only interested in selecting or highlighting rows in the tbody, not the header row at the top.

cletus
  • 616,129
  • 168
  • 910
  • 942
1

It can be used for styling, yes, and it's also useful for jQuery selectors to help discriminate which elements you want to select. After all, you can have tr tags in thead or tbody.

Nosredna
  • 83,000
  • 15
  • 95
  • 122
0

The thead, tbody, and tfoot elements in HTML are used to group table rows into logical sections based on their content. There are two main reasons you'd want to do this:

  1. To allow the body to be scrolled independently of the header and/or footer
  2. To make it easier to apply different style rules to the different sections of the table.

<table>
  <thead>
  <tr>
     <th>Month</th>
     <th>Savings</th>
  </tr>
  </thead>
  <tfoot>
  <tr>
      <td>Sum</td>
      <td>$180</td>
  </tr>
  </tfoot>
  <tbody>
  <tr>
     <td>January</td>
     <td>$100</td>
  </tr>
  <tr>
      <td>February</td>
      <td>$80</td>
  </tr>
  </tbody>
</table>
Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43
  • we also add footer in table structure and it also helps to those views who wanted to know why they use table header, body and footer in more convenient way. – Aashish Kumar Aug 29 '17 at 05:40