【问题标题】:Freeze the top row for an html table only (Fixed Table Header Scrolling)仅冻结 html 表格的顶行(固定表格标题滚动)
【发布时间】:2012-01-15 11:07:05
【问题描述】:

我想制作一个冻结顶行的 html 表格(因此当您垂直向下滚动时,您总能看到它)。

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

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

【问题讨论】:

  • 没有javascript就不行...
  • 您确定使用表格是最佳选择吗?你想达到什么目的?
  • 我建议检查一下:mkoryak.github.io/floatThead
  • @mkoryak 非常感谢。数据表的fixedheader插件有很多问题,这在5m内解决了!干得好!
  • 这里有一个视频可以解决这个问题,值得一看:youtube.com/watch?v=_dpSEjaKqSE

标签: html css


【解决方案1】:

我知道这有几个答案,但这些都没有真正帮助我。我发现 [这篇文章][1] 解释了为什么我的 sticky 没有按预期运行。

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

使它工作所需的最少代码如下:

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

th {
  background: white;
  position: sticky;
  top: 0;
}

将表格设置为相对,<th> 可以设置为粘性,顶部为 0 [1]:https://css-tricks.com/position-sticky-and-table-headers/

注意:必须用最大高度的 div 包裹表格:

<div id="managerTable" >
...
</div>

地点:

#managerTable {
    max-height: 500px;
    overflow: auto;
}

【讨论】:

  • 对我来说最好的解决方案,整行是固定的,而 Chinthaka Fernando 的解决方案只有文本是固定的
  • 当表格同时具有垂直和水平滚动时完美,但请记住 safari:position: -webkit-sticky; /* Safari */ position: sticky; /* and other browsers */
  • 可能还需要添加 z-index 以确保 "sticky" th 在 table tr's 的顶部
  • 是的,这是一个很好的答案!如果您希望保持底部边框,请参阅:stackoverflow.com/questions/50361698/…
  • th 需要background: white; 吗?或tabletext-align: left;?
【解决方案2】:

这称为固定页眉滚动。有许多记录在案的方法:

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>

另一种方法是使用 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>

这种方法有很多问题,例如确保精确的像素宽度,因为表格非常可爱,不同的浏览器会根据计算以不同的方式分配像素,而您根本无法(AFAIK)保证分布在所有情况下都是完美的。如果您的表格中有边框,这将变得非常明显。

我采取了不同的方法并说螺旋表,因为您无法做出此保证。我用 div 来模拟表格。这也有定位行和列的问题(主要是因为浮动有问题,使用内联块不适用于 IE7,所以它真的让我使用绝对定位将它们放在适当的位置)。

有人制作了 Slick Grid,它与我的方法非常相似,您可以使用一个很好的(尽管很复杂)示例来实现这一点。

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

【讨论】:

  • :-) 相信我,我去过那里
  • @Fiesty Mango imaputz.com 版本的缺点是什么?
  • @turbo2oh 缺点是他使用的是我提到的第二种方法(在 tbody 中添加 overflow: auto)。这在 IE 中不起作用。尝试在 IE 中查看他的页面,当您在 Chrome 或 FF 中进行对比时,您会明白我的意思
  • 我们在使用两表方法时不会破坏可访问性吗?
  • 所有链接都坏了
【解决方案3】:

根据Pure CSS Scrollable Table with Fixed Header,我写了一个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
}

【讨论】:

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

我担心的不是固定宽度的单元格。在任何情况下这似乎都不起作用。 我发现这个解决方案似乎是我需要的。我在这里为其他正在寻找方法的人发布它。看看这个fiddle

工作片段:

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;
}
<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>

【讨论】:

  • 我快速浏览了@Tom 建议的 jsfiddle 链接,这似乎是一个很好的解决方案。想知道为什么这没有被标记为答案。优点是 - 没有 Javascript,表格内容决定了列的宽度。有没有人发现任何缺点?
  • 我没有发现缺点。它绝对比我见过的任何东西都更好地处理动态列大小。
  • 看起来当表格比其父表格宽时标题不会水平滚动。
  • 我按照here的指示使用了height: 100vh,而不是160px的高度:.container { overflow-y: auto; height: 160px; }。我还设置了html, body {overflow: hidden;}
  • 太棒了!
【解决方案5】:

表格的第一行可以使用 CSS position: sticky; MDN ref:

.table-class tr:first-child>td{
    position: sticky;
    top: 0;
}

【讨论】:

  • 最佳答案
  • 由于position: sticky; 的工作方式,可能还需要设置background-colorz-index
  • 可能还需要table 设置border-collapse: separate;,以便边框样式始终与粘性单元格保持一致。
【解决方案6】:

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

#headings {
  position: fixed;
  top: 0px;
  width: 960px;
}

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

【讨论】:

  • 列不会与标题对齐(除非它们都具有固定宽度)
  • 和@zarko的问题一样
  • 如果您想添加其他表格功能,如调整大小、文本溢出等,这会引入许多其他问题。此外,与其他(例如纯 css 或类似)解决方案相比,它似乎不必要地复杂
【解决方案7】:

可以在&lt;th&gt; 上使用position:fixed&lt;th&gt; 是顶行)。

Here's an example

【讨论】:

  • 正如我在@eric-fortis 的评论中所说,这仅适用于固定宽度的列
  • 请勿使用。隐藏表格主体的第一行。
  • 我试过(07-jun-2016),我看到的问题是只有最后一个 是可见的,而且 的宽度似乎与对应的列。报告的第一行模糊可能是由于没有使用 css 中 table 类的 margin-top 属性。
【解决方案8】:

Chromatable jquery 插件允许使用允许百分比的宽度的固定标题(或顶行)——当然,只有 100% 的百分比。

http://www.chromaloop.com/posts/chromatable-jquery-plugin

我想不出没有javascript你怎么能做到这一点。

更新:新链接 -> http://www.jquery-plugins.info/chromatable-00012248.htm

【讨论】:

    【解决方案9】:

    我用这个:

    tbody{
      overflow-y: auto;
      height: 350px;
      width: 102%;
    }
    thead,tbody{
        display: block;
    }
    

    我使用引导 css col-md-xx 定义列宽。如果不定义列宽,则 的自动宽度与 . 102% 是因为溢出导致你失去了一些空间

    【讨论】:

    • 它只有在你定义了表格的固定高度时才有效。对于height!=100%,标题不会停留在固定位置。
    【解决方案10】:

    使用 css 斑马样式

    复制粘贴此示例并查看已修复的标题。

           <style>
           .zebra tr:nth-child(odd){
           background:white;
           color:black;
           }
    
           .zebra tr:nth-child(even){
           background: grey;
           color:black;
           }
    
          .zebra tr:nth-child(1) {
           background:black;
           color:yellow;
           position: fixed;
           margin:-30px 0px 0px 0px;
           }
           </style>
    
    
       <DIV  id= "stripped_div"
    
             class= "zebra"
             style = "
                border:solid 1px red;
                height:15px;
                width:200px;
                overflow-x:none;
                overflow-y:scroll;
                padding:30px 0px 0px 0px;"
                >
    
                    <table>
                       <tr >
                           <td>Name:</td>
                           <td>Age:</td>
                       </tr>
                        <tr >
                           <td>Peter</td>
                           <td>10</td>
                       </tr>
                    </table>
    
        </DIV>
    

    注意 div 叶子的顶部内边距为 30px 第一行剥离数据使用的空间 即 tr:nth-child(1) 即“固定位置” 并格式化为 -30px 的边距

    【讨论】:

    • 你会因此失去单元格宽度。
    猜你喜欢
    • 2010-11-09
    • 2017-06-19
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 2019-12-19
    • 2018-02-27
    • 2021-06-26
    相关资源
    最近更新 更多