Is it necessary to have in any table?

2019-02-26 05:54发布

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?

7条回答
趁早两清
2楼-- · 2019-02-26 06:30

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

查看更多
神经病院院长
3楼-- · 2019-02-26 06:31

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>
查看更多
地球回转人心会变
4楼-- · 2019-02-26 06:31

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.

查看更多
放我归山
5楼-- · 2019-02-26 06:38
<table>
     <tr>
        <td>text</td>
     </tr>      
</table>

This is minimum table that I can think of.

查看更多
叛逆
6楼-- · 2019-02-26 06:42

The answer is

YES!

if you are using tables to display tabular data.

Tabular data are organized into rows and columns for a reason. The meaning of the datum in a table cell is defined by the meaning of the column and the row in which it appears.

It is important to identify those cells that give meaning to the rows and columns rather separately from the cells that just contain data.

For example, the following table conveys absolutely no meaningful information:

34 56 90 15
45 65 85 30
50 55 70 35

The numbers only have meaning if the rows and columns are given names. Those names are marked up using <th>:

    Feb May Aug Nov
ITH
JFK
IST

Of course, we still do not know what those numbers mean which is why <caption> is needed:

Average Temperatures by Month at Selected Airports
    Feb May Aug Nov
ITH
JFK
IST

Finally, it is important to note details such as the units of measurement, data sources etc. That kind of information usually goes in the footer of the table:

Average Temperatures by Month at Selected Airports
    Feb May Aug Nov
ITH
JFK
IST
Temperatures in °F. Source: Publication #456 MNMO

The title of the table goes into <caption>. The <thead> and <tfoot> sections are especially useful when a table grows large. See How to deal with page breaks when printing a large HTML table for an example.

You can use <colgroups> to group together logically related data.

查看更多
淡お忘
7楼-- · 2019-02-26 06:44

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

查看更多
登录 后发表回答