【问题标题】:How to make sticky table header overflow inside scrollable div using only css?如何仅使用 css 使可滚动 div 内的粘性表头溢出?
【发布时间】:2017-08-10 19:15:55
【问题描述】:

我想制作粘性表格标题(总是在顶部),但我有两个问题:

  • thead 不会在容器 div 内溢出
  • 头部不随内容滚动(在 x 轴上)

这是一个例子(只是从另一个问题中分出来的):

http://jsfiddle.net/quxshkdh/

HTML:

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

CSS:

html, body{
  margin:0;
  padding:0;
  height:100%;
}
section {
  position: relative;
  border: 1px solid #000;
  padding-top: 37px;
  background: #500;
  width: 200px;
  height: 200px;
}

table {
  border-spacing: 0;
  width:100%;
}
td + td {
  border-left:1px solid #eee;
}
td, th {
  border-bottom:1px solid #eee;
  background: #ddd;
  color: #000;
  width: 100px;
}
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;
  top: 0;
  line-height: normal;
  border-left: 1px solid #800;
}
th:first-child div{
  border: none;
}

我只想使用没有 js 技巧的 css 解决方案。列的宽度可以明确定义,但高度不能。

你能修复我的例子并解释它应该如何工作吗?

编辑: 我已经检查了问题: Fixed header table with horizontal scrollbar and vertical scrollbar on - 这对我来说不是解决方案,因为前两个答案使用 JQuery,而@Anshuman 建议的第三个答案也是基于 js 的(js 只是隐藏在 html 标签 onscroll= 中)

大多数解决方案都使用两个表格而不是thead,我想避免这种情况 - 我想用thead 部分设置已经呈现的html 表格的样式。

【问题讨论】:

标签: css html-table css-tables


【解决方案1】:

使用您的 html 结构,您可以在容器和 tbody 上调度滚动条。但是只有在容器一直向右滚动时才能看到 tbody 滚动条。

不太确定这是您要查找的内容(用于演示的宽度和高度,如果您尝试这样做,请根据您的需要更新)

table {
  display: block;
  width: 75vw;
}

section {
  width: 25vw;
  overflow: hidden;
  position: relative;
  overflow-x: auto;
}

thead,
tbody {
  display: block;
}

tbody {
  height: 150px;
  overflow-x: hidden;
  overflow-y: auto;
}

tr {
  display: table;
  border-collapse: collapse;
  width: 75vw;
  table-layout: fixed;
}

th,
td,
th div {
  box-shadow: 0 0 0 1px;
}

th div {
  position: absolute;
  top: 0;
  left: auto;
  width: 25vw;
  background: gray;
}
<section>
  <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>

【讨论】:

  • 哇!它几乎是我想要的!我对这段代码的唯一问题是让 tbody 填充父容器的 100% 宽度和高度。简单地改变宽度和高度是行不通的。正如我在原始问题中所说,只能明确定义列的宽度。容器宽度和高度仅作为示例设置。实际上,它将是父 div 的 100%。
  • 我应用了一些更改:使用 display:table-row 和 display: table-cell - 因为我在表格中有 colspan 和 rowspan(因此还有 max-width 和 min-width 以使列大小相等)。毕竟 - 它的所有 css 并且它适用于现有表。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 2012-05-20
相关资源
最近更新 更多