Is this the best semantic solution?
Not really. Although the action of embedding an element A inside another element of B can be used to indicate that A is a child of B, this is not what you are doing here: you will enclose the table in a completely different row, so that the relationship between parents and children between A and B.
By creating a cell that spans all the columns in the table and then creates another table in it with the same number of columns, you also effectively say: "these are some other columns that are not related to those in the external table."
You can see the implied (absence) relationship between the columns by adding a border to the cells in the above example:
Obviously, you can fix this with CSS, but unidentified rendering of an HTML fragment is often a good guide for its semantics.
Is another approach more appropriate? If so, is this the recommended way?
There is no standard way to represent hierarchical relationships between table rows in HTML. The criterion from the answer I gave to a similar question , however you can do this with additional classes, identifiers and data-
:
<table> <thead> <tr> <th>ITEM</th> <th>ATTRIBUTE 1</th> <th>ATTRIBUTE 2</th> </tr> </thead> <tbody> <tr id=100> <td>item</td> <td>value</td> <td>value</td> </tr> <tr id=110 data-parent=100 class=level-1> <td>sub</td> <td>value</td> <td>value</td> </tr> <tr id=200> <td>item</td> <td>value</td> <td>value</td> </tr> </tbody> </table>
The relationship between parents and children will not be displayed in unpainted rendering (there is no other way you could do this without adding additional content, as far as I can see), but there are enough hooks to add CSS
.level-1 > td:first-child { padding-left: 1em; }
..., which leads to the following:
With a little javascript, you can also use the id
and data-parent
attributes for customization, so that, for example, hovering over a string causes the parent to be highlighted.
Is the table title responsible for both tables, or do I need to create a new one?
In the proposed solution, creating one cell that spans all columns and then creates another table inside it means that there is no connection between the header cells and the cells of your "child" row. Obviously, my proposed solution above does not have this problem.