Are thead, tbody and tfoot tags required?

Should I use thead , tbody and tfoot every time I use the table tag? Is it required by standard or not?

+10
source share
7 answers

Those tags are not required. It is considered good form to use them if the table is used to represent the data, for which the table should be used. If a table is used to host content, they are usually omitted.

W3c

Table of data

+9
source

The HTML5 tabular data specification does not require them:

Contexts in which this element ( tr ) can be used:

  • As a child of thead .
  • As a child of tbody .
  • As a child of tfoot .
  • As a child of table , after any caption , colgroup and thead elements , but only if there are no tbody elements that are children of the table element .

Despite the fact that I consider it good practice to split your lines in thead , tbody and tfoot , as it makes identifying table rows easier.

In the end, the browser will always add at least tbody for you.

+4
source

There are differences between HTML and XHTML.

HTML requires the tbody element. However, its start and end tags are optional. That means if you write

 <table> <tr><td>data</td></tr> </table> 

tr will always be inside tbody , regardless of whether you write tags explicitly or not. Same story for this table:

 <table> <thead> <tr><th>header</th></tr> </thead> <tr><td>data</td></tr> </table> 

Here the second tr will be wrapped in tbody .


In XHTML, the tbody element is only needed if you have thead and / or tfoot .

So, the first example above is valid XHTML. However, there is a difference with HTML: tr will be directly inside the table in the DOM tree. (You won’t be able to see the difference, but you need to keep this in mind if you need to manipulate a table in JavaScript.)

The second example is incorrect XHTML, and you should not be surprised if it has different results in different browsers.

+1
source

In short, if I remember the article I read correctly, the answer will be "No." They are not required by standard.

0
source

No. Modern browsers will add them by default.

0
source

The answer to your question is "no, they are not needed."

<tfoot> table row with <thead> , <tfoot> , <tbody> , you group them without using an identifier or class. Allows more style settings, such as a fixed title and a scroll-dependent body.

Simple table

 <table> <tr> <td></td> </tr> </table> 

with the body

 <table> <thead> <tr> <th></th> </tr> </thead> <tbody> <tr> <td></td> </tr> </tbody> </table> 
0
source

The two porpuses of HTML5 were a good implementation and the correct use of markup (to make a programmatic link between the elements), and the second was the creation of more accessible web pages.

As the site https://webaim.org reports:

Anyone who cannot see the table cannot create these visual associations, therefore, to create a programmatic link between the elements inside the table, you should use the appropriate markup. When the correct HTML markup is in place, users of on-screen readers can navigate the data tables one cell at a time, and they will hear the headers of the columns and rows they are talking to.

This is why <thead> and <tbody> were created. Screen reader software will work with these two tags.

Hope this helps! If you want to know more, you can read the article that I used as a link.

Creating Available Tables

0
source

Source: https://habr.com/ru/post/983939/


All Articles