While setting up an excel style table with html
and css
with a sticky head I realized that the borders on the head of the table looked strange.
Here's the code:
table {
border-collapse: collapse;
position: relative;
}
tr:nth-child(even) {
background-color: lightgray;
}
th {
background-color: lightblue;
position: sticky;
top: 0;
}
th,
td {
border: 1px solid black;
padding: 10px 20px;
}
<table>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
</tr>
</tbody>
</table>
JSFiddle: https://jsfiddle.net/cpotdevin/5j3ah247/
The following images show how it looks in three different browsers.
Chrome:
The upper and lower borders on the sticky row disappear.
Firefox:
All inner borders disappear.
I also tried not using border-collapse: collapse;
and instead using the cellspacing=0
attribute on the table
but this makes inner borders look thicker than I'd like.
JSFiddle: https://jsfiddle.net/cpotdevin/wxvn1crL/
What can I do to solve this? I want the borders to always look like they do when the table head has not entered the sticky state.
EDIT
As pointed out by @JonMac1374 this question was already answered here.