I have designed this table in figma and now I need to build it in HTML. However, I am not sure how to get the error message to span the table row below the cells like you can see in the image.
When I try to do it in my code below, the divs just disappear and the error message gets teleported above the table…
I have also thought of adding the error message as its own row, however, this is not entirely semantically accurate. More importantly, it would break the "rearrange" feature which I am also going to implement. If I drag one row somewhere else, the error message would end up out of place. So I would prefer keeping it all in one row if possible.
table {
border-collapse: collapse;
}
td, th {
border-bottom: 0.1rem solid #ccc;
padding: 0.5rem;
text-align: left;
}
<table>
<tr>
<th></th>
<th>Option Identifier</th>
<th>Swatch</th>
</tr>
<tr>
<div class="cell-container">
<td>✋</td>
<td><input type="text"></td>
<td><input type="color"></td>
</div>
<div class="error-container">
Error Message
</div>
</tr>
<tr>
<div class="cell-container">
<td>✋</td>
<td><input type="text"></td>
<td><input type="color"></td>
</div>
<div class="error-container">
Error Message
</div>
</tr>
</table>
2
Answers
When you are mixing div and td, you always have to follow the
table
,tr
andtd
hierarchy. A div can only be placed inside atd
, never the other way around.A simple solution would be to create another table row. You can just give classes or other attributes to the tr tags. Hide the error message by default then, of course.
If content spans the whole row, add "rowspan" to a td to make it go across several or all tds of a row.
If you still must use a div, only use the div inside a
td
tag. Otherwise you must use different layout options.Please check MDN: HTML table documentation for more info.
While working on tabular format data you must follow the table tag. And if you want to have div inside a table it must be in table > tr > tbody > td hierarchy.
If you do not want table tag and want to add extra div you can create table by css and you can add div as per your requirement.
You can check the following example.
Hope, this helps.