HTML表具有固定的页眉和页脚以及可滚动的正文,没有固定的宽度

daveoncode

我想创建一个具有固定theadtfoot可滚动的表tbody

我已经尝试了几种方法,包括纯CSS和CSS + Javascript,但是它们都很薄弱且不可靠,我可以通过更改演示中的标记轻松地破坏它们。

我想要的是一种使表格表现得像表格的方式,这意味着浏览器将根据内容(在页面加载时调整窗口大小)以及以下情况下的内容自动调整列

  1. 如果列标头(thead > tr > th)的内容大于列正文(tbody > tr > td)的内容且大于列脚注(tfoot > tr > td)的内容,则应根据列标头的大小调整列的大小

  2. 如果列的正文(tbody > tr > td)的内容大于列的标题(thead > tr > th)的内容且大于列的页脚(tfoot > tr > td)的内容,则应根据列的正文的大小来调整列的大小

  3. 如果列的页脚(tfoot > tr > td)的内容大于列的页眉(thead > tr > th)的内容且大于列的正文(tbody > tr > td)的内容,则应根据列的页脚的大小来调整列的大小

table下面应澄清的情况:

<table>
  <thead>
    <tr>
      <th>Header one *leads the width* (case 1)</th>
      <th>Header two</th>
      <th>Header three</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Column one</td>
      <td>Column two *leads the width* (case 2)</td>
      <td>Column three</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Footer one</td>
      <td>Footer two</td>
      <td>Footer three *leads the width* (case 3)</td>
    </tr>
  </tfoot>
</table>

我想要一个干净(尽可能)且可靠的解决方案,该解决方案适用于不同的场景,可能只有CSS,但JavaScript还是可以的(香草和干净的JavaScript,而不是jQuery插件)。我不在乎旧的浏览器支持(拥有它,或者至少找到一个可以在旧的浏览器上正常降级的解决方案,这是很好的,但这是可选的)...我什至可以接受使用divs代替表节点最终的解决方案按预期工作...因此在2016年,使用现代浏览器和CSS可能会以某种方式实现吗?

编辑:

主体应垂直滚动,并且表格可以有任意数量的列

更新:

我想出了以下解决方案:https : //codepen.io/daveoncode/pen/LNomBE,但我仍然不是100%满意。主要问题是我无法为页眉和页脚单元格设置不同的背景。

更新2:

它现在可以工作了!

daveoncode

我终于实现了一个可行的解决方案!

相关的CSS如下:

.wrapper {
  width: 90%;
  position: relative;
  border: 1px solid #000;
  background: #efefef;
  overflow: hidden;
  border-radius: 7px;
}

.container {
  overflow-y: auto;
  height: 200px;
  border-top: 41px solid transparent;
  border-bottom: 41px solid transparent;
}

table {
  border-spacing: 0;
  border-collapse: collapse;
  width: 100%;
}

td + td {
  border-left: 1px solid #fff;
}

td, th {
  border-bottom: 1px solid #fff;
  background: #efefef;
  padding: 10px;
}

thead tr th,
tfoot tr td {
  height: 0;
  line-height: 0;
  margin: 0;
  padding-top: 0;
  padding-bottom: 0;
  color: transparent;
  border: none;
  white-space: nowrap;
}

thead tr th div,
tfoot tr td div {
  position: absolute;
  color: #fff;
  height: 20px;
  padding: 10px;
  margin-left: -10px;
  line-height: normal;
  width: 100%;
  z-index: 2;
  text-align: left;
  font-weight: bold;
}

thead tr th div {
  border-left: 1px solid #000;
  border-bottom: 1px solid #000;
}

tfoot tr td div {
  border-top: 1px solid #000;
}

tfoot tr td div.c1,
thead tr th div.c1 {
  background: violet;
}

tfoot tr td div.c2,
thead tr th div.c2 {
  background: green;
}

tfoot tr td div.c3,
thead tr th div.c3 {
  background: yellow;
}

thead tr th div {
  top: 0;
}

tfoot tr td div {
  bottom: 0;
}

thead tr th:first-child div,
tfoot tr td:first-child div {
  border-left: none;
}

这是标记:

<div class="wrapper">
  <div class="container">
    <table>
      <thead>
        <tr>
          <th>
            Header one *leads the width* (case 1)
            <div class="c1">
              Header one *leads the width* (case 1)
            </div>
          </th>
          <th>
            Header two
            <div class="c2">
              Header two
            </div>
          </th>
          <th>
            Header three
            <div class="c3">
              Header three
            </div>
          </th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three [first]</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three</td>
        </tr>
        <tr>
          <td>Column one</td>
          <td>Column two *leads the width* (case 2)</td>
          <td>Column three [LATEST]</td>
        </tr>
      </tbody>
      <tfoot>
        <tr>
          <td>
            Footer one
            <div class="c1">
              Footer one
            </div>
          </td>
          <td>
            Footer two
            <div class="c2">Footer two</div>
          </td>
          <td>
            Footer three *leads the width* (case 3)
            <div class="c3">Footer three *leads the width* (case 3)</div>
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</div>

它可以在Chrome,Firefox,Safari和IE11上运行(我不知道它在较旧的浏览器中的行为)。在Codepen上查看:https ://codepen.io/daveoncode/pen/LNomBE

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

具有固定/左列和可滚动正文的HTML表?

来自分类Dev

具有固定的HTML页眉和页脚以及灵活内容的页面布局

来自分类Dev

具有固定列和标题的RecyclerView,以及可滚动的页脚

来自分类Dev

带有固定页眉和页脚的多列滚动

来自分类Dev

如何在Android的ConstraintLayout中设置固定的页眉页脚和可滚动的正文

来自分类Dev

具有可滚动行和固定标题的表

来自分类Dev

Vaadin基本布局:固定的页眉和页脚+可滚动的内容

来自分类Dev

Vaadin基本布局:固定的页眉和页脚+可滚动的内容

来自分类Dev

HTML / CSS SIdebar,具有页眉/页脚和可滚动的全高度内容

来自分类Dev

CSS和DIV,标题和正文具有可滚动的内容,右侧固定了侧边栏

来自分类Dev

iOS 7.1.2上的Web应用程序中具有滚动中间位置的“固定”页眉和页脚

来自分类Dev

iOS 7.1.2上的Web应用程序中具有滚动中间位置的“固定”页眉和页脚

来自分类Dev

HTML、CSS:带有固定页脚的可滚动表格

来自分类Dev

具有全角页脚的固定宽度布局

来自分类Dev

具有可变宽度,固定宽度和剩余宽度的HTML表

来自分类Dev

全页高度,带有固定的页眉和页脚

来自分类Dev

具有响应表宽度的HTML表固定标题

来自分类Dev

具有自适应列宽的可滚动固定标题 html 表

来自分类Dev

具有固定/左列和可滚动主体的HTML表格?

来自分类Dev

固定的页眉/页脚,可滚动的内容,上方/下方的空间

来自分类Dev

如何在HTML中创建具有可变和固定列宽以及分组标题的表?

来自分类Dev

如何在HTML中创建具有可变和固定列宽以及分组标题的表?

来自分类Dev

带有固定标题且正文没有滚动的 Bootstrap 表

来自分类Dev

Bootstrap固定的页眉和页脚以及可折叠的边栏

来自分类Dev

固定宽度的正文,但页眉宽度不同

来自分类Dev

单页,具有固定动态页脚的滚动站点

来自分类Dev

粘性页脚和固定页眉

来自分类Dev

固定的标题和可滚动的正文

来自分类Dev

具有多个内部表的表和具有水平滚动的固定表头表

Related 相关文章

  1. 1

    具有固定/左列和可滚动正文的HTML表?

  2. 2

    具有固定的HTML页眉和页脚以及灵活内容的页面布局

  3. 3

    具有固定列和标题的RecyclerView,以及可滚动的页脚

  4. 4

    带有固定页眉和页脚的多列滚动

  5. 5

    如何在Android的ConstraintLayout中设置固定的页眉页脚和可滚动的正文

  6. 6

    具有可滚动行和固定标题的表

  7. 7

    Vaadin基本布局:固定的页眉和页脚+可滚动的内容

  8. 8

    Vaadin基本布局:固定的页眉和页脚+可滚动的内容

  9. 9

    HTML / CSS SIdebar,具有页眉/页脚和可滚动的全高度内容

  10. 10

    CSS和DIV,标题和正文具有可滚动的内容,右侧固定了侧边栏

  11. 11

    iOS 7.1.2上的Web应用程序中具有滚动中间位置的“固定”页眉和页脚

  12. 12

    iOS 7.1.2上的Web应用程序中具有滚动中间位置的“固定”页眉和页脚

  13. 13

    HTML、CSS:带有固定页脚的可滚动表格

  14. 14

    具有全角页脚的固定宽度布局

  15. 15

    具有可变宽度,固定宽度和剩余宽度的HTML表

  16. 16

    全页高度,带有固定的页眉和页脚

  17. 17

    具有响应表宽度的HTML表固定标题

  18. 18

    具有自适应列宽的可滚动固定标题 html 表

  19. 19

    具有固定/左列和可滚动主体的HTML表格?

  20. 20

    固定的页眉/页脚,可滚动的内容,上方/下方的空间

  21. 21

    如何在HTML中创建具有可变和固定列宽以及分组标题的表?

  22. 22

    如何在HTML中创建具有可变和固定列宽以及分组标题的表?

  23. 23

    带有固定标题且正文没有滚动的 Bootstrap 表

  24. 24

    Bootstrap固定的页眉和页脚以及可折叠的边栏

  25. 25

    固定宽度的正文,但页眉宽度不同

  26. 26

    单页,具有固定动态页脚的滚动站点

  27. 27

    粘性页脚和固定页眉

  28. 28

    固定的标题和可滚动的正文

  29. 29

    具有多个内部表的表和具有水平滚动的固定表头表

热门标签

归档