(The grey section is thead
, white section with grey border-bottom is tr
in tbody
)
table {
border-collapse: collapse;
border-spacing: 0;
}
thead {
background: #ddd;
}
tbody tr {
border-bottom: 1px solid #ddd;
}
th,
td {
width: 100px;
padding: 10px;
text-align: start;
}
<table>
<thead>
<tr>
<th>name</th>
<th>age</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>20</td>
</tr>
<tr>
<td>Sam</td>
<td>24</td>
</tr>
</tbody>
</table>
I need to add a 8px margin left/right around thead
, while there should be no spacing around tbody.
I have tried adding margin/padding to thead
, but neither worked. I also attempted adding margin-left to the first element of thead
, but it still didn’t work.
2
Answers
Margins does not really make sense and does not work for table elements.
What you can do is increase padding for first and last elements of
thead th
:Not sure exactly what you want to achieve, but perhaps adding an 8px white border to the table will do what you want?