function toggleBorders() {
table.tHead.firstElementChild.children[1].classList.toggle('thick-border');
Array.from(table.tBodies[0].children).forEach(row => row.children[1].classList.toggle('thick-border'));
}
table {
border-collapse: collapse;
}
th.thick-border,
td.thick-border {
border: 3px solid coral;
border-top: none;
border-bottom: none;
}
<table id="table">
<thead>
<tr>
<th>h1</th> <th>h2</th> <th>h3</th> <th>h4</th>
</tr>
</thead>
<tbody>
<tr>
<td>11</td> <td>12</td> <td>13</td> <td>14</td>
</tr>
<tr>
<td>21</td> <td>22</td> <td>23</td> <td>24</td>
</tr>
</tbody>
</table>
<br>
<button id="btn" onclick="toggleBorders()">Click Me!</button>
Is there any way to toggle the column’s borders without twitching?
And without thickening initial border width of other columns?
Maybe with shadows or gradients or somehow else
4
Answers
Well and one more solution with pseudo-elements ::before and ::after
You may want to use a transparent border to begin with:
You can use a
box-shadow
on either side instead.2px / -2px extends it to the right / left, 0px offset on the y-axis, and 0px blur radius.
Since you were looking to style exactly the second column of the given table, I think it would be more readable if you defined such feature in stylings instead of using the convoluted javascript code you engaged in your own demo.
Here I just switched to
border-collapse: separate
to keep showing the cells right and left borders as styled and used an explicit css rule so that the whole table when having the classthick-border
will be styled as that.That way you just need to toggle the class on the table alone instead of each single cell of the target column.