【问题标题】:How to apply width 100% with scroll able table body如何使用可滚动的表格主体应用宽度 100%
【发布时间】:2016-09-19 13:32:36
【问题描述】:

我正在尝试实现表体可滚动。我能够成功地做到这一点,但现在我面临宽度 100% 的问题

请检查这个 jsfiddle https://jsfiddle.net/Ratan_Paul/stpgd6x6/

<table>
<thead style="display:block;">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
 </thead>
 <tbody style="max-height: 50px;display: block;overflow-y:auto;">
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
  </tbody>
</table>

我不能将宽度应用于 thead 和 tbody 吗?任何解决方案,这样我就可以使用滚动并将宽度设置为 100%

【问题讨论】:

标签: html css


【解决方案1】:

有一个更简单的 CSS 解决方案。不用担心元素的宽度和滚动,用容器 div 包裹表格并将样式 position: sticky 用于 th 元素。下面的示例 sn-p -

.table-container {
  max-height: 160px;
  border: 1px solid red;
  overflow: auto;
}

th {
  position: sticky;
  top: 0;
  background: white;
}

td, th {
  padding: 4px 8px;
}
<div class="table-container">
  <table>
    <thead>
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
    </tbody>
  </table>
</div>

【讨论】:

    【解决方案2】:

    如果您想让表格可滚动并设置它的高度,您只需使用表格本身。

    删除表格上的所有样式,并添加以下 css:

    table {
      width: 100%;
      max-height: 80px;
      display: block;
      overflow-y: scroll;
      position: relative;
    }
    

    看这个小提琴: https://jsfiddle.net/stpgd6x6/3/

    【讨论】:

    • 我想让标题固定并且正文可滚动
    【解决方案3】:

    试试这个代码。我认为这对你来说是最好的

    table{
      width:100%;
    }
    tbody{
      width:100% !important;
      max-height: 50px;
      display: block;
      overflow-y:auto;
    }
    thead{
      min-width:100% !important;
    }
    thead tr{
      width:calc(100% - 17px) !important;
      display:inline-block;
    }
    tbody tr{
      width:100% !important;
      display:inline-block;
    }
    th, td{
      width:30% !important;
      display:inline-block;
      text-align:left;
    }
    <table>
    <thead>
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
     </thead>
     <tbody>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
      </tbody>
    </table>

    【讨论】:

    • 谢谢,这看起来很棒,如果您注意到最后一列 country 与其值没有正确对齐
    • 谢谢,我已经能够通过亚当的代码解决这个问题。感谢您的帮助
    【解决方案4】:

    您需要清除表格最初附带的一些样式,并将宽度设置为 33%,因为您使用的是 3 个列标题 (100%/3)

    见下文:

    table {
        width: 100%;
        border-collapse: collapse;
        border-spacing: 0;
        border: 2px solid black;
    }
    
    thead, tbody, tr, td, th { display: block; }
    
    tr:after {
        content: ' ';
        display: block;
        visibility: hidden;
        clear: both;
    }
    
    thead th { 
        height: 30px;
        line-height: 30px;
        text-align: left;
    }
    
    tbody {
        height: 100px;
        overflow-y: auto;
    }
    
    thead {
        /* fallback */
        width: 97%;
        /* minus scroll bar width */
        width: calc(100% - 17px);
    }
    
    tbody { border-top: 2px solid black; }
    
    tbody td, thead th {
        width: 33%;
        float: left;
    }
    
    tbody td:last-child, thead th:last-child {
        border-right: none;
    }
    <table>
    <thead>
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
     </thead>
     <tbody>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
      </tbody>
    </table>

    更详细的答案可以在这里找到:HTML table with 100% width, with vertical scroll inside tbody

    【讨论】:

    • 检查最后一列及其值是否未正确对齐
    • @Ratan 请立即查看
    • Atom,抱歉,现在不存在可滚动对象
    • @Ratan 哎呀!现已更正!
    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 2011-11-08
    • 2017-08-03
    • 1970-01-01
    • 2011-01-27
    • 2013-08-24
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多