【问题标题】:Freeze two rows header in html table冻结html表格中的两行标题
【发布时间】:2021-06-26 18:09:17
【问题描述】:

我创建了一个带有单个标题行的 HTML 表格,并应用了 CSS 中的逻辑来冻结它(位置:粘在第一个元素上)。它工作得很好,但后来我决定将标题分成两行,那时我的滚动失败了。标题的第二行滚动到第一行。

有没有办法用 CSS 来修复它?我在这里看到了一些使用 jquery 冻结行和列的解决方案,但我并不真正了解 jquery,所以我想知道是否有更简单的方法来做到这一点。

这是我的桌子:

 <table id="transformTbl">
    <thead>
      <tr class = "first">
        <th>Symbol</th>
        <th colspan="3">Expiration</th>
        <th colspan="5">Prior Days Prices</th>
        <th>Strike Price</th>
        <th colspan="2">Difference</th>
        <th colspan="3">Recovery</th>
      </tr>
      <tr class = "second">
        <th></th>
        <th>Date</th>
        <th>Weekday</th>
        <th>Price</th>
        <th>-1</th>
        <th>-2</th>
        <th>-3</th>
        <th>-4</th>
        <th>-5</th>
        <th></th>
        <th>$</th>
        <th>%</th>
        <th>Date</th>
        <th>Price</th>
        <th>Days</th>
      </tr>
    </thead>

这是我的 CSS:

table {
    border: 2px solid rgb(54, 7, 7);
    border-radius: 5px;
    background-color: #f8f8ff;
    width: auto;
    outline: 1px solid rgb(49, 1, 1);
}
th {
    position: sticky;
    top: 0;
    border-bottom: 2px solid #000;
    color: #e4f1e6;
    background-color: #031401;
    padding: 2px 2px;
    opacity: .70;
    font-size:15px;
    border-radius: 5px;
    z-index: 1;
}

请注意,我只包含了我的标题行的示例。我的表显然有 tbody 跟随 thead 和所有相应的 tr/td 元素,这些元素被 PHP 调用填充到 MySQL 存储过程。

滚动到顶部:

向下滚动:

谢谢

【问题讨论】:

标签: html css


【解决方案1】:

table 标记中有多行标题时,由于 Chrome 中的existing issue,我们必须为第二行标题提供硬编码位置

.first th {
  position: sticky;
  top: 0px;
}

.second th {
  position: sticky;
  top: 45px;
}

【讨论】:

  • 不,这没有帮助。仅将其放在 thead 上不会使任何标题行变得粘滞。将它放在“thead th”上使其工作方式与以前相同。
  • 我不确定确切的要求,但如果您希望两个标题都具有粘性,这可以工作:codesandbox.io/s/dreamy-browser-cw0ko?file=/src/styles.css
  • 另一个提供与我类似的解决方案的资源:@​​987654323@
猜你喜欢
  • 2011-09-24
  • 2012-01-15
  • 2014-11-19
  • 2017-06-19
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 2021-08-29
相关资源
最近更新 更多