This fiddle says it all, but let's go briefly over it.
Whenever you are building a table out of a tree, use
<td> tags or their CSS equivalents
table-cell and apply
display:contents to the elements that you intreleave with the table to preserve your tree without destroying the table layout.
Easy-peasy, but it took me a while to figure it out.