固定表格标题仅水平和垂直滚动 CSS

2024-01-14

首先,是否可以仅使用 CSS 来实现这一点?

我已经构建了一个可以水平和垂直滚动的表格,但是,我希望将标题封装在其容器内,而不是出现在包装器之外。这样,当您水平滚动时,相应的标题与其指定列的内容一致。

使用不同的变体position: absolute and position: static给我一些预期的结果,但不是完整的解决方案。

您会注意到应用于section元素提供在封闭区域内水平滚动的效果。

这是我到目前为止所拥有的 JSFIDDLE 示例以及下面的 CSS 供参考

https://jsfiddle.net/ko6qco1r/ 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;
}

这是其中的另一个有趣的W3C 的不作为给我们带来了挑战(比如垂直居中)。与垂直居中一样,您不能使用以下方法在具有水平滚动条的表格上放置固定标题position: fixed; on the thead元素。但你确实有几个选择。

选项 1 - 水平滚动 (http://codepen.io/staypuftman/pen/JXbpvZ http://codepen.io/staypuftman/pen/JXbpvZ)

这里的关键是将表格布局重新建立为table-layout: fixed; CSS-tricks 有一篇很好的文章介绍了这种方法 https://css-tricks.com/fixing-tables-long-strings/然后放一个min-width当您希望出现滚动条时容器上的值。这使得表格可以从左向右滚动,并保持较小设备上列标题的完整性。但标题不固定。

选项 2 - 固定标头 (https://jsfiddle.net/dPixie/byB9d/3/light/ https://jsfiddle.net/dPixie/byB9d/3/light/)

你的代码看起来像是抄袭这个人的作品,所以我想我应该在这里重新发布以给他一些荣誉。这种方法创建了一系列<div>反映内容的元素<th>元素并使用一些非常有创意的定位技术来使其发挥作用。

有一个更好的破败萨尔查设计 http://salzerdesign.com/blog/?p=191这显示了一些examples http://salzerdesign.com/test/fixedTable.html并详细解释了实现这一目标的所有复杂操作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

固定表格标题仅水平和垂直滚动 CSS 的相关文章