3

i have seen some HTML5 designers ignoring the element <tbody> when marking up a table. They only use the <thead> and <tfoot> elements instead. Can this be considered a good practice and following good standards in HTML5?


This question based on my previous question which can be found at:

thead, tfoot and tbody order in HTML5

  • My previous question was about the order of using <thead><tbody><tfoot> and this question is about the possibilty of removing the <tbody> at all from the markup.
Community
  • 1
  • 1
miami
  • 225
  • 2
  • 4

2 Answers2

6

The <tbody> element will automatically be inserted around <tr> elements if you do not add it, in the same way that <body> will be wrapped around the contents of the page.

It is for this reason that I recommend explicitly adding <tbody> elements, so that there is no confusion when an implied tbody element gets styled in CSS.

zzzzBov
  • 167,057
  • 51
  • 314
  • 358
  • 1
    @SheikhHeera, not a problem; it's easy to misread. I can't tell you how many times people have called me "zzzzBoy", which I find absolutely infuriating. – zzzzBov Sep 20 '13 at 18:43
  • I've made it better now. – The Alpha Sep 20 '13 at 18:43
  • This is almost a “primarily ipinion-based” question, but the point about avoiding confusion is a rather solid technical argument in favor of explicit `` tags. It matters in scripting and styling, acting as a reminder: a `tr` element is never a child of a `table` element. – Jukka K. Korpela Sep 20 '13 at 19:24
5

In HTML5 spec, it states clearly about table

In this order: optionally a caption element, followed by zero or more colgroup elements, followed optionally by a thead element, followed optionally by a tfoot element, followed by either zero or more tbody elements or one or more tr elements, followed optionally by a tfoot element (but there can only be one tfoot element child in total).

It's optional but good practice to explicitly add it, as other answer also mentioned that, I agree with zzzzBov.

Community
  • 1
  • 1
The Alpha
  • 138,380
  • 26
  • 281
  • 300