Ste*_*eve 24 html css twitter-bootstrap
在这篇文章之后,我有一个表格,我正在尝试获取粘性标题。除了我的表格样式的顶部和底部带有边框的标题。
我不明白的部分是顶部/底部边框应用于th与表格的其余部分一起滚动而不是留在“卡住”标题单元格中。有什么可以做的吗?
可以在此处找到工作示例:https : //codepen.io/smlombardi/pen/MNKqQY?editors=1100#0
let string = ''
console.log("oj")
for(let i=0; i<100; i++) {
string += `
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
`
}
document.getElementsByTagName('tbody')[0].innerHTML += stringRun Code Online (Sandbox Code Playgroud)
.table-sticky-container {
height: 200px;
overflow-y: scroll;
border-top: 1px solid green;
border-bottom: 1px solid green;
}
.table-sticky {
th {
position: sticky;
top: 0;
z-index: 2;
border-top: 1px solid red !important;
border-bottom: 2px solid red !important;
}
}Run Code Online (Sandbox Code Playgroud)
<div class="table-sticky-container">
<table class="table table-sticky">
<thead class="thead-light">
<tr>
<th scope="col">Name</th>
<th scope="col">Title</th>
<th scope="col">ID</th>
<th scope="col">Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
</tbody>
</table>
</div>Run Code Online (Sandbox Code Playgroud)
Grz*_* T. 29
你可以加
.table {
border-collapse: separate;
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是它看起来很糟糕,更好的解决方案是使用伪元素添加解决方法。
th:after,
th:before {
content: '';
position: absolute;
left: 0;
width: 100%;
}
th:before {
top: -1px;
border-top: 1px solid red;
}
th:after {
bottom: -1px;
border-bottom: 2px solid red;
}
Run Code Online (Sandbox Code Playgroud)
.table {
border-collapse: separate;
}
Run Code Online (Sandbox Code Playgroud)
th:after,
th:before {
content: '';
position: absolute;
left: 0;
width: 100%;
}
th:before {
top: -1px;
border-top: 1px solid red;
}
th:after {
bottom: -1px;
border-bottom: 2px solid red;
}
Run Code Online (Sandbox Code Playgroud)
第二种解决方案
.table {
border-collapse: collapse;
}
.table-sticky thead th {
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 2;
box-shadow: inset 0 1px 0 red, inset 0 -2px 0 red;
}
Run Code Online (Sandbox Code Playgroud)
小智 14
你可以加
.table {
border-collapse: separate;
border-spacing: 0;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
不要使用border-collapse和绘制如下线:
td, th {
border-bottom: 1px solid grey;
border-right: 1px solid grey;
}
table {
border-spacing: 0px;
border-left: 1px solid grey
}
th {
background-color:#c5e8ec;
position: sticky;
position: -webkit-sticky;
border-top: 1px solid grey;
top:0;
}
Run Code Online (Sandbox Code Playgroud)