仅为html表冻结顶行(固定表格标题滚动)

Dav*_*iov 113 html css

我想制作一个顶行冻结的html表(所以当你垂直向下滚动时,你总能看到它).

有没有一种聪明的方法可以在没有javascript的情况下实现这一目标?

请注意,我不需要冻结左列.

Fei*_*ngo 62

这称为固定标题滚动.有许多记录在案的方法:

http://www.imaputz.com/cssStuff/bigFourVersion.html

没有JavaScript,你不会有效地解决这个问题...特别是如果你想要跨浏览器支持.

您采取的方法有很多,尤其是跨浏览器/版本支持.

编辑:

即使它不是你要修复的标题,但第一行数据,概念仍然是相同的.我不是100%你指的是.

额外的想法 我的任务是由我公司负责研究可以在IE7 +,Firefox和Chrome中运行的解决方案.

经过多次搜索,尝试和挫折之后,它真正归结为一个根本问题.在大多数情况下,为了获得固定的标题,您需要实现固定的高度/宽度列,因为大多数解决方案涉及使用两个单独的表,一个用于标题,它将浮动并保留在包含数据的第二个表的位置.

//float this one right over second table
<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
  </tr>
</table>

<table>
//Data
</table>
Run Code Online (Sandbox Code Playgroud)

一些尝试的替代方法是利用tbody和thead标签,但这也是有缺陷的,因为IE不会允许你在tbody上放置一个滚动条,这意味着你不能限制它的高度(这么愚蠢的IMO).

<table>
  <thead style="do some stuff to fix its position">
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
  </tr>
  </thead>
  <tbody style="No scrolling allowed here!">
     Data here
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

这种方法有许多问题,例如确保精确的像素宽度,因为表格非常可爱,因为不同的浏览器会根据计算不同地分配像素,而您根本无法(AFAIK)保证分发在所有情况下都是完美的.如果你的桌子里有边框,那就显而易见了.

我采取了不同的方法,并说螺丝表,因为你无法做出这种保证.我用div来模仿表格.这也存在定位行和列的问题(主要是因为浮动有问题,使用内嵌块对IE7不起作用,所以它真的让我使用绝对定位将它们放在适当的位置).

有人在那里制作了Slick Grid,它有一个非常类似于我的方法,你可以使用和一个好的(尽管很复杂)的例子来实现这一点.

https://github.com/6pac/SlickGrid/wiki

  • 如果你的桌子上有colspans,请准备好头痛... (13认同)
  • 所有链接都坏了 (9认同)

Jas*_*key 42

根据带有固定标题的纯CSS可滚动表,我写了一个DEMO,通过设置overflow:auto为tbody 轻松修复标题.

table thead tr{
    display:block;
}

table th,table td{
    width:100px;//fixed width
}


table  tbody{
  display:block;
  height:200px;
  overflow:auto;//set tbody to auto
}
Run Code Online (Sandbox Code Playgroud)

  • 如果我固定宽度,表格格式将会改变。如何将宽度设置为自动? (4认同)
  • 我喜欢这种方法的简单性。但是,由于我没有免费使用JavaScript的要求,并且想要自动宽度功能,因此我修改了此解决方案以使其适合。当我删除固定宽度的线时,标头的宽度已关闭,因此我编写了一个简短的javascript,使用getBoundingClientRect()和DOM行和单元格集合将第一个数据行的宽度逐个单元复制到标头行。 (2认同)

Tom*_*Tom 30

我担心的是没有固定宽度的细胞.在任何情况下似乎都无效.我发现这个解决方案似乎是我需要的.我在这里发帖给其他正在搜索的人.看看这个小提琴

工作片段:

html, body{
  margin:0;
  padding:0;
  height:100%;
}
section {
  position: relative;
  border: 1px solid #000;
  padding-top: 37px;
  background: #500;
}
section.positioned {
  position: absolute;
  top:100px;
  left:100px;
  width:800px;
  box-shadow: 0 0 15px #333;
}
.container {
  overflow-y: auto;
  height: 160px;
}
table {
  border-spacing: 0;
  width:100%;
}
td + td {
  border-left:1px solid #eee;
}
td, th {
  border-bottom:1px solid #eee;
  background: #ddd;
  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 #800;
}
th:first-child div{
  border: none;
}
Run Code Online (Sandbox Code Playgroud)
<section class="">
  <div class="container">
    <table>
      <thead>
        <tr class="header">
          <th>
            Table attribute name
            <div>Table attribute name</div>
          </th>
          <th>
            Value
            <div>Value</div>
          </th>
          <th>
            Description
            <div>Description</div>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>align</td>
          <td>left, center, right</td>
          <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
        </tr>
        <tr>
          <td>bgcolor</td>
          <td>rgb(x,x,x), #xxxxxx, colorname</td>
          <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
        </tr>
        <tr>
          <td>border</td>
          <td>1,""</td>
          <td>Specifies whether the table cells should have borders or not</td>
        </tr>
        <tr>
          <td>cellpadding</td>
          <td>pixels</td>
          <td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
        </tr>
        <tr>
          <td>cellspacing</td>
          <td>pixels</td>
          <td>Not supported in HTML5. Specifies the space between cells</td>
        </tr>
        <tr>
          <td>frame</td>
          <td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
          <td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
        </tr>
        <tr>
          <td>rules</td>
          <td>none, groups, rows, cols, all</td>
          <td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
        </tr>
        <tr>
          <td>summary</td>
          <td>text</td>
          <td>Not supported in HTML5. Specifies a summary of the content of a table</td>
        </tr>
        <tr>
          <td>width</td>
          <td>pixels, %</td>
          <td>Not supported in HTML5. Specifies the width of a table</td>
        </tr>
      </tbody>
    </table>
  </div>
</section>
Run Code Online (Sandbox Code Playgroud)

  • 当表格比其父表格宽时,标题似乎不会水平滚动。 (2认同)
  • 而不是高度为 160px:``.container { Overflow-y: auto; 高度:160 像素;}``,我按照[此处]的指示使用了``height:100vh``(/sf/ask/110259901/ -窗户)。我还设置了``html, body {overflow:hidden;}``。 (2认同)

Chi*_*ndo 8

您可以将CSS position: sticky;用于表MDN ref的第一行

.table-class tr:first-child>td{
    position: sticky;
    top: 0;
}
Run Code Online (Sandbox Code Playgroud)


Joe*_*Joe 6

我知道这有几个答案,但是这些都没有真正帮助我。我找到了这篇文章该文章解释了为什么我sticky无法按预期运行。

基本上,你不能使用position: sticky;<thead><tr>元素。但是,它们可以在上使用<th>

使它正常工作所需的最低代码如下:

table {
  text-align: left;
  position: relative;
}

th {
  background: white;
  position: sticky;
  top: 0;
}
Run Code Online (Sandbox Code Playgroud)

将表格设置为相对时,<th>可以将其设置为粘性,顶部为0

  • 对我来说最好的解决方案,整行都是固定的,而 Chinthaka Fernando 的解决方案只固定了文本 (4认同)
  • 当表格同时具有垂直和水平滚动时完美,但请记住 safari: `position: -webkit-sticky; /* Safari */ 位置:粘性;/* 和其他浏览器 */` (2认同)
  • 可能还需要添加 z-index 以确保“粘性”th 位于表 tr 的顶部 (2认同)
  • 是的,这是一个很好的答案!如果您希望保留底部边框,请参阅:/sf/ask/3525318891/ (2认同)

Eri*_*tis 5

您可以使用两个 div,一个用于标题,另一个用于表格。然后使用

#headings {
  position: fixed;
  top: 0px;
  width: 960px;
}
Run Code Online (Sandbox Code Playgroud)

正如@ptriek所说,这只适用于固定宽度的列。

  • 列不会与标题对齐(除非它们都具有固定宽度) (7认同)

小智 5

可以使用position:fixedon <th><th>作为顶行)。

这是一个例子

  • 不使用。遮挡表格主体的第一行。 (8认同)
  • 正如我在 @eric-fortis 的评论中所说,这仅适用于固定宽度的列 (3认同)

归档时间:

查看次数:

284824 次

最近记录:

5 年,10 月 前