【发布时间】:2019-12-22 11:34:42
【问题描述】:
在使用带有粘性头部的html 和css 设置excel 样式表时,我意识到表格头部的边框看起来很奇怪。
代码如下:
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/
下图显示了它在三种不同浏览器中的外观。
我也尝试不使用border-collapse: collapse;,而是使用table 上的cellspacing=0 属性,但这会使内部边框看起来比我想要的要厚。
JSFiddle:https://jsfiddle.net/cpotdevin/wxvn1crL/
我能做些什么来解决这个问题?我希望边框看起来总是像表头没有进入粘性状态时一样。
编辑
正如@JonMac1374 所指出的,这个问题已经在here 得到回答。
【问题讨论】: