【发布时间】:2016-03-15 14:57:35
【问题描述】:
首先,是否可以仅使用 CSS 来实现?
我已经建立了一个可以水平和垂直滚动的表格,但是,我希望将标题封装在它的容器中,而不是出现在包装器之外。这样,当您水平滚动时,相应的标题与指定列的内容一致。
使用position: absolute 和position: static 的不同变体会给我一些预期的结果,但不是完整的解决方案。
您会注意到应用于section 元素的宽度,以产生在封闭区域内水平滚动的效果。
这是我目前所拥有的 JSFIDDLE 示例以及下面要参考的 CSS
https://jsfiddle.net/ko6qco1r/
html, body{
margin:0;
padding:0;
height:100%;
}
section {
position: relative;
border: 1px solid #000;
padding-top: 37px;
background: grey;
width: 300px;
}
.container {
overflow-y: auto;
overflow-x: scroll;
height: 200px;
}
table {
border-spacing: 0;
width:100%;
}
td + td {
border-left:1px solid #eee;
}
td, th {
border-bottom:1px solid #eee;
background: white;
color: #000;
padding: 10px 25px;
}
th {
height: 0;
line-height: 0;
padding-top: 0;
padding-bottom: 0;
color: transparent;
border: none;
white-space: nowrap;
}
th div{
position: absolute;
background: transparent;
color: #fff;
padding: 9px 25px;
top: 0;
margin-left: -25px;
line-height: normal;
border-left: 1px solid black;
}
th:first-child div{
border: none;
}
【问题讨论】:
-
表头卡在原地,是什么问题?
-
已编辑 - 我希望将标头封装在它的容器中,而不是出现在包装器之外。这样当您水平滚动时,相应的标题与它指定列的内容一致。