nachime nachime - 4 months ago 9
CSS Question

padding not behaving like I expect them to in a table row

When I

hover
over this, the first
td
moves. But why? I'd like for it to remain stationary.



.q-tr:hover {
border-left: 2px solid #35ccea;
padding-left: -2px;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
<table class="table">
<thead>
<tr class = 'q-tr'>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr class = 'q-tr'>
<td>John</td>
<td>Doe</td>
<td>john@example.com</td>
</tr>
<tr class = 'q-tr'>
<td>Mary</td>
<td>Moe</td>
<td>mary@example.com</td>
</tr>
<tr class = 'q-tr'>
<td>July</td>
<td>Dooley</td>
<td>july@example.com</td>
</tr>
</tbody>
</table>
</div>




Answer

Because tables with the collapsing border model don't have padding at all (reference).

Update - thanks @LGSon for mentioning that it won't work for IE - reference.

So, instead of targeting the tr, target the first cell in each tr:

.q-tr > td:first-child {
    border-left: 2px solid transparent;
}

.q-tr:hover > td:first-child {
  border-left: 2px solid #35ccea;  
}

Bootply

Comments