views:

202

answers:

7

is it necessary to have <th> in any table? even if table has no heading?

table has 3 other tag <thead> <tbody> <tfoot> is it necessary to use all even if i have nothing for table footer. Firefox by default add all these in code.

and is it necessary , <th> always should be in a <thead>

and if i have a heading in content received from client , and heading is from outside the table but related to table then how should i place that heading for table

As a above table

<h3>Heading of table<h3>
<table>......</table>

as a heading of table

<table>
<thead><tr rowspan=3><th>Heading of table</th></tr></thead>

or as a caption of table

<table>
<caption> Heading of table</caption>

Which is good for screen reader and semantically correct?

+2  A: 

No, it is not necessary to have th. But it doesn't look like you're using th right. Generally, you have one for each column. A simple example of th used correctly is:

<table>
<tr><th>Breed</th><th>Name</th></tr>
<tr><td>Pekingese</td><td>Pluto</td></tr>
<tr><td>Lab</td><td>Buddy</td></tr>
</table>

You could also do:

<table>
<thead><tr><th>Breed</th><th>Name</th></tr></thead>
<tr><td>Pekingese</td><td>Pluto</td></tr>
<tr><td>Lab</td><td>Buddy</td></tr>
</table>
Matthew Flaschen
i just added code for example if i use rowspan and heading is only one for all column.
metal-gear-solid
rowspan is an attribute of a cell, not a row. Plus, I think you want to use colspan (to span across multiple columns). And it usually would not make sense to have a single spanned header cell.
Matthew Flaschen
thanks for example. What is the benefit to add <thead> <tfoot> <tbody> while we can make table with <table> <tr> <td> and <th>?
metal-gear-solid
It allows different styling, and it allows you to semantically separate the parts of the table.
Matthew Flaschen
and does it makes table reading helpful for screen reader users?
metal-gear-solid
+9  A: 

According to the HTML DTD this is the content model for HTML tables:

<!ELEMENT TABLE - -
     (CAPTION?, (COL*|COLGROUP*), THEAD?, TFOOT?, TBODY+)>
<!ELEMENT CAPTION  - - (%inline;)*     -- table caption -->
<!ELEMENT THEAD    - O (TR)+           -- table header -->
<!ELEMENT TFOOT    - O (TR)+           -- table footer -->
<!ELEMENT TBODY    O O (TR)+           -- table body -->
<!ELEMENT COLGROUP - O (COL)*          -- table column group -->
<!ELEMENT COL      - O EMPTY           -- table column -->
<!ELEMENT TR       - O (TH|TD)+        -- table row -->
<!ELEMENT (TH|TD)  - O (%flow;)*       -- table header cell, table data cell-->

So this is illegal syntax:

<thead><th>Heading of table</th></thead>

It should be:

<thead><tr><th>Heading of table</th></tr></thead>

<th> elements aren't required anywhere. They're simply one of the two cell types (the other being <td>) that you can use in a table row. A <thead> is an optional table section that can contain one or more rows.

Edit: As to why to use <thead> there are several reasons:

  1. Semantic: You're differentiating between the content of your table and "metadata". This is most often used to delineate between column headers and data rows;
  2. Accessibility: it helps people who use screen readers to understand the contents of the table;
  3. Non-Screen Media: Printing a multi-page table may allow you to put the <thead> contents at the top of each page so people can understand what the columns meaning without flicking back several pages;
  4. Styling: CSS can be applied to <tbody> elements, <thead> elements, both or some other combination. It gives you something else to write a selector against;
  5. Javascript: this often comes up when using jQuery and similar libraries. The extra information is helpful in writing code.

As an example of (5) you might do this:

$("table > tbody > tr:nth-child(odd)").addClass("odd");

The <thead> element means those rows won't be styled that way. Or you might do:

$("table > tbody > tr").hover(function() {
  $(this).addClass("hover");
}, function() {
  $(this).removeClass("hover");
});

with:

tr.hover { background: yellow; }

which again excludes the <thead> rows.

Lastly, many of these same arguments apply to using <th> elements over <td> elements: you're indicating that this cell isn't data but a header of some kind. Often such cells will be grouped together in one or more rows in the <thead> section or be the first cell in each row depending on the structure and nature of your table.

cletus
yes but what is the benefit to use <thead>, is it just for because we can give different styling for <thead>?
metal-gear-solid
Using `<thead>` and `<th>` do make CSS styling easier in many cases (as you need less selectors) but they also make your HTML more "semantic". If you have an entire row(s) of headings, put the row(s) in a `<thead>`. For tables where the headings are only in the left column however, it doesn't make sense to use a `<thead>`.
Crast
you mean <thead> should be used if we have more than one <th> , otherwise only <th> is enough
metal-gear-solid
@Jitendra: see update.
cletus
is <tbody> should always be part of any table? because we always have data for table ,so if we are using <th> or <thead> then is it must must <tbody> be used also?
metal-gear-solid
@Jitendra: `<tbody>` elements are implied. When you put rows in a table that aren't in a section, they are automatically placed inside an implied `<tbody>` element (which usually confuses people who are new to jQuery when they try to select rows with say `table > tr` and it doesn't work). You can use explicit `<tbody>` elements or just rely on the implicit behaviour. It's up to you.
cletus
The <th>, <td>, <thead>, <tbody>, and <tfoot> elements are great for clarifying intent, compare with <em> and <i>.
dalle
`<th>` tags are useful for accessibility. When a screen reader user is on the 14th row of a table, they can get the screen reader to read out the heading for the current cell, to help them remember what the number means. (This assumes you use the `scope` attribute correctly, to indicate which cells a `<th>` tag applies to.)
Paul D. Waite
A: 

You want to use what describes your data best.

<caption> will describe the whole table. th will create a single cell which is usually used to describe one column (but can also be used for row headings).

thead, tfoot, and tbody. all can be used and are all optional provided that they are in that order, if used, and you have only one thead and one tfoot (but you can have multiple tbody. Many browsers (all?) will add them implicitly if you don't, but the spec says they're optional.

th can appear inside any tr regardless of where the tr is.

Sam
A: 
<table>
     <tr>
        <td>text</td>
     </tr>      
</table>

This is minimum table that I can think of.

Mendy
`<table><tr><th></th></tr></table>` is equally valid as a minimum.
eyelidlessness
+4  A: 

Use <th>s if you are displaying tabular data - use one for each column. It is nice for your regular users and essential for screen readers. Do not use <th>s if you are using the table for layout purposes (or other nefarious schemes...)

Ray
+1 for 'nefarious schemes'
alex
what is "nefarious schemes"?
metal-gear-solid
a joke - it means 'evil plans' - bwaa-haa-haa-haa (evil laugh)
Ray
A: 

The benefits are semantics. The <th> means T able H eader. Use it to markup the headers of your columns. Generally within a <thead>.

alex
You mean if we use <th> then it should be always in <thead> ? and What is the benefit to add <thead> <tfoot> <tbody> while we can make table with <table> <tr> <td> and <th>, does it beneficial for screen readers?
metal-gear-solid
+1  A: 
Sinan Ünür
u mean my first example should not be used
metal-gear-solid
@Jitendra Absolutely. The `<h3>` would the be the title for the section in which the table appears, not a title that explains the table.
Sinan Ünür
"is it necessary to have <th> in any table?"The answer is "he's free" and not "Yes". It's not necessary, but recommended. cletus already cited free reasons why he should be using them.
Omar Abid
@Omar Abid If the table is being used to display actual tabular data, **`<th>`** for **every** row and **every** column is a must. Otherwise, the table has no meaning. It is the kind of mistake sophomores in college make the first time they try to write a report. This is not about HTML. This is about what a table is.
Sinan Ünür
@Sinan Ünür, a table needn't have *meaning* (in the literal sense) in order to be a *valid* set of tabular data (it would just be valid and useless). Moreover, its meaning might be inferred from context. That said, heading rows and columns *are* recommended for best conveying meaning.
eyelidlessness
@Sinan Unur I can use <td class="thead">Header</td>... no? <th> and <thead> just makes it easier for me, but it's not a MUST!
Omar Abid
@Omar Abid, why would you use something more verbose and less semantically correct?
eyelidlessness
@eyelidlessness You are right, the <thead> is more correct, however it's not a must! There's a difference between 'Recommended' and 'a must'
Omar Abid
@Omar Abid, you're right but can you answer my question? I didn't say it's a "must", I asked why you would use something more verbose and less semantically correct.
eyelidlessness
@Omar Abid: There is no W3C requirement to use grammatically correct language either. You are confusing what is required HTML versus what is required for something to be called a data table. Whether you are typing the table in to Word or putting it on the web or generating `LaTeX` does not matter. In a data table, the meaning of rows and columns **must** be identified. Otherwise, you do not have a data table. The way to identify that in HTML is to use **`<th>`**.
Sinan Ünür
@eyelidlessness I won't, but I don't know the OP situation and problem and may be using an alternative is better suitable for him. I think in this situation, the OP don't need the <thead>, this is why he asks, the response is **he's not obliged to put them**
Omar Abid