&lt;表格&gt;带有固定的<thead>和可滚动的 &lt;tbody&gt; [英] &lt;table&gt; with fixed &lt;thead&gt; and scrollable &lt;tbody&gt;

查看:18
本文介绍了&lt;表格&gt;带有固定的<thead>和可滚动的 &lt;tbody&gt;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了这里的问题和互联网上的文章,但还没有找到满足我要求的解决方案.所以现在在 2017 年,有没有一种优雅的方式来拥有一个

:

I've looked through questions here and articles all over the internet, but haven't found yet solution that would satisfy my requirements. So now in 2017, is there an elegant way to have a <table> that would:

  1. 用 html+css 编写(无 js)
  2. 有固定的标题(不可滚动;不粘)
  3. 具有可滚动的
(滚动条可能始终可见)
  • header 和 body 将正确处理调整大小,而不是
  • 列和 列的混乱对齐
  • 不会使用嵌套表或单独的表作为标题
    1. be written in html+css (no js)
    2. have fixed header (not scrollable; not sticky)
    3. have scrollable <tbody> (scrollbar may be always visible)
    4. header and body would handle resizing properly and not mess alignment of the <thead> columns and the <tbody> columns
    5. would not use nested tables or separate table for header

    推荐答案

    此解决方案满足所有 5 个要求:

    This solution fulfills all 5 requirements:

    table {
      width: 100%;
    }
    
    table, td {
      border-collapse: collapse;
      border: 1px solid #000;
    }
    
    thead {
      display: table; /* to take the same width as tr */
      width: calc(100% - 17px); /* - 17px because of the scrollbar width */
    }
    
    tbody {
      display: block; /* to enable vertical scrolling */
      max-height: 200px; /* e.g. */
      overflow-y: scroll; /* keeps the scrollbar even if it doesn't need it; display purpose */
    }
    
    th, td {
      width: 33.33%; /* to enable "word-break: break-all" */
      padding: 5px;
      word-break: break-all; /* 4. */
    }
    
    tr {
      display: table; /* display purpose; th's border */
      width: 100%;
      box-sizing: border-box; /* because of the border (Chrome needs this line, but not FF) */
    }
    
    td {
      text-align: center;
      border-bottom: none;
      border-left: none;
    }

    <table> 
      <thead> 
        <tr>
          <th>Table Header 1</th>
          <th>Table Header 2</th>
          <th>Table Header 3</th>
        </tr> 
      </thead>
      <tbody>
        <tr>
          <td>Data1111111111111111111111111</td>
          <td>Data</td>
          <td>Data</td>
        </tr>
        <tr>
          <td>Data</td>
          <td>Data2222222222222222222222222</td>
          <td>Data</td>
        </tr>
        <tr>
          <td>Data</td>
          <td>Data</td>
          <td>Data3333333333333333333333333</td>
        </tr>
        <tr>
          <td>Data</td>
          <td>Data</td>
          <td>Data</td>
        </tr>
        <tr>
          <td>Data</td>
          <td>Data</td>
          <td>Data</td>
        </tr>
        <tr>
          <td>Data</td>
          <td>Data</td>
          <td>Data</td>
        </tr>
        <tr>
          <td>Data</td>
          <td>Data</td>
          <td>Data</td>
        </tr>
        <tr>
          <td>Data</td>
          <td>Data</td>
          <td>Data</td>
        </tr>
        <tr>
          <td>Data</td>
          <td>Data</td>
          <td>Data</td>
        </tr>
        <tr>
          <td>Data</td>
          <td>Data</td>
          <td>Data</td>
        </tr>
      </tbody>
    </table>

    这篇关于&lt;表格&gt;带有固定的<thead>和可滚动的 &lt;tbody&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆