【问题标题】:Scrollable table with first column and first row pinned第一列和第一行固定的可滚动表
【发布时间】:2019-12-17 21:50:54
【问题描述】:

我正在尝试创建固定第一列和第一行的可滚动 HTML 表格。该列已固定,但我找不到固定该行的方法。有没有办法实现?

直播:https://jsfiddle.net/8dnzgtwa/

.wrapper {
    margin-left: 5em;
    overflow-x: auto;
    width: 200px;
}

th:first-child, td:first-child {
    left: 0;
    position: absolute;
    width: 5em;
}

th, td p {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

td p {
    margin-top: 0;
}

td p:last-child {
    margin-bottom: 0;
}

th, td {
    border-bottom: 1px dashed red;
}
<div class="wrapper">
    <table>
        <thead>
            <tr>
                <th>Column 1 aaa aaa aaa</th>
                <th>Column 2 bbb bbb bbb</th>
                <th>Column 3 ccc ccc ccc</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><p>Column 1 aaa aaa aaa</p></td>
                <td><p>Column 2 bbb bbb bbb</p><p>Column 2 bbb bbb bbb</p></td>
                <td><p>Column 3 ccc ccc ccc</p></td>
            </tr>
            <tr>
                <td><p>Column 1 aaa aaa aaa</p></td>
                <td><p>Column 2 bbb bbb bbb</p></td>
                <td><p>Column 3 ccc ccc ccc</p></td>
            </tr>
            <tr>
                <td><p>Column 1 aaa aaa aaa</p></td>
                <td><p>Column 2 bbb bbb bbb</p></td>
                <td><p>Column 3 ccc ccc ccc</p></td>
            </tr>
            <tr>
                <td><p>Column 1 aaa aaa aaa</p></td>
                <td><p>Column 2 bbb bbb bbb</p></td>
                <td><p>Column 3 ccc ccc ccc</p></td>
            </tr>
            <tr>
                <td><p>Column 1 aaa aaa aaa</p></td>
                <td><p>Column 2 bbb bbb bbb</p></td>
                <td><p>Column 3 ccc ccc ccc</p></td>
            </tr>
        </tbody>
    </table>
</div>

另外(这不是问题的一部分,但我将不胜感激),我正在尝试修复单元格高度。图片说明了一切:

【问题讨论】:

  • 我不明白您为什么要将编辑滚动到旧版本? SO 的自动 sn-ps 允许我们重现问题中描述的问题。
  • 我不喜欢这种格式。但是,我同意 sn-ps 很有用。因此,我自己创建了 sn-p。
  • 格式化是由 SO 的 sn-p(左侧的 tidy 按钮)自动完成的,而不是以任何方式 我的个人喜好。无论如何,让我们回到问题,您说您希望第一行被固定,意思是当您向下滚动时它保持在顶部?我说的对吗?
  • 是的,没错,就像这里:codepen.io/nirmalkc/pen/oswdB.
  • 太好了,我正在努力,我会尽快发布答案。

标签: html css html-table


【解决方案1】:

遇到问题的主要原因是 position: absolute 规则,这意味着定义后面规则的元素已从文档流中删除,这会导致列缩小到其内容的大小(您谈到border 没有放在想要的位置)。

position 属性有另一个 强大 值,称为 sticky(从它的名字你猜它可以做什么!)当视口滚动到某个位置时,它会固定在它的位置。

所以不是:

th:first-child, td:first-child {
    left: 0;
    position: absolute;
    width: 5em;
}

使用:

tr th:first-child, tr td:first-child {
  position: sticky;
  left: 0;
  width: 5em;
}

对于要固定的第一行,在 thead &gt; th 元素上使用 stickytop: 0

thead th {
  position: sticky;
  top: 0;
}

下一个演示中的div.wrapper 被最小化(大小),以便我们确保有一些垂直/水平滚动。

.wrapper {
  margin-left: 5em;
  overflow-x: auto;
  width: 300px;
  height: 100px; /** for demo purposes to have some scroll **/
}

tr th:first-child,
tr td:first-child {
  position: sticky; /** sticky instead of absolute **/
  left: 0;
  width: 5em;
  background-color: #00f; /** for demo purposes **/
  z-index: 2;  /** so that the column on the left stays on top of the other columns when having an horizontal scroll **/
}

thead th {
  position: sticky; /** pinn forst row **/
  top: 0;
  background-color: #f00; /** for demo purposes **/
}

th,
td p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

td p {
  margin-top: 0;
}

td p:last-child {
  margin-bottom: 0;
}

th,
td {
  border-bottom: 1px dashed red;
}
<div class="wrapper">
  <table>
    <thead>
      <tr>
        <th>Column 1 aaa aaa aaa</th>
        <th>Column 2 bbb bbb bbb</th>
        <th>Column 3 ccc ccc ccc</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <p>Column 1 aaa aaa aaa</p>
        </td>
        <td>
          <p>Column 2 bbb bbb bbb</p>
          <p>Column 2 bbb bbb bbb</p>
        </td>
        <td>
          <p>Column 3 ccc ccc ccc</p>
        </td>
      </tr>
      <tr>
        <td>
          <p>Column 1 aaa aaa aaa</p>
        </td>
        <td>
          <p>Column 2 bbb bbb bbb</p>
        </td>
        <td>
          <p>Column 3 ccc ccc ccc</p>
        </td>
      </tr>
      <tr>
        <td>
          <p>Column 1 aaa aaa aaa</p>
        </td>
        <td>
          <p>Column 2 bbb bbb bbb</p>
        </td>
        <td>
          <p>Column 3 ccc ccc ccc</p>
        </td>
      </tr>
      <tr>
        <td>
          <p>Column 1 aaa aaa aaa</p>
        </td>
        <td>
          <p>Column 2 bbb bbb bbb</p>
        </td>
        <td>
          <p>Column 3 ccc ccc ccc</p>
        </td>
      </tr>
      <tr>
        <td>
          <p>Column 1 aaa aaa aaa</p>
        </td>
        <td>
          <p>Column 2 bbb bbb bbb</p>
        </td>
        <td>
          <p>Column 3 ccc ccc ccc</p>
        </td>
      </tr>
      <tr>
        <td>
          <p>Column 1 aaa aaa aaa</p>
        </td>
        <td>
          <p>Column 2 bbb bbb bbb</p>
        </td>
        <td>
          <p>Column 3 ccc ccc ccc</p>
        </td>
      </tr>
      <tr>
        <td>
          <p>Column 1 aaa aaa aaa</p>
        </td>
        <td>
          <p>Column 2 bbb bbb bbb</p>
        </td>
        <td>
          <p>Column 3 ccc ccc ccc</p>
        </td>
      </tr>
      <tr>
        <td>
          <p>Column 1 aaa aaa aaa</p>
        </td>
        <td>
          <p>Column 2 bbb bbb bbb</p>
        </td>
        <td>
          <p>Column 3 ccc ccc ccc</p>
        </td>
      </tr>
    </tbody>
  </table>
</div>

详细了解position 规则。

【讨论】:

  • 非常感谢,Thabet。看来我稍微改进了垂直滚动:jsfiddle.net/zwtvg4d9
  • @johnc.j。不客气 !很高兴我设法提供了一些帮助,保持了良好的工作并度过了愉快的一天。
猜你喜欢
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 2011-03-25
  • 1970-01-01
  • 2014-03-07
  • 1970-01-01
  • 2010-09-22
相关资源
最近更新 更多