In the world of HTML, it's crucial to remember that tbody
elements cannot have their own wrapper inside a table. The tbody
element serves as the container for tr
elements, and according to HTML syntax rules, only the table
element can contain a tbody
. This important rule is enforced by web browsers when parsing HTML code.
If one attempts to use a different element like a div
as a wrapper (which might seem like a logical choice), what will actually happen is that the browser will create an empty div
element before the table in the DOM. All the tbody
and tr
elements will then be placed within the table
element, essentially removing them from the initial div
element and leaving it empty unless it contains non-table-related content.
To illustrate this point, let's consider some intentionally invalid markup:
<style>
.x { outline: solid red }
</style>
<table>
<tbody>
<tr><td>foo
</tbody>
<div class=x>
FOO!
<tbody>
<tr><td>foo2
</tbody>
<tbody>
<tr><td>foo3
</tbody>
</div>
<tbody>
<tr><td>The end
</tbody>
</table>
The key takeaway here is that a different approach is needed. One common solution is to stick with just a single tbody
element. If this is not possible for your specific case, it would be beneficial to delve into the reasons why, but that discussion could be saved for a separate question.