【问题标题】:Fixed Tabe Head with Scrollable Table Body带有可滚动表体的固定表头
【发布时间】:2020-05-21 14:42:38
【问题描述】:

我必须用滚动条制作这个表格内容(tbody),表格的高度是固定的。为了启用滚动,我添加了样式display: block。之后,表格的宽度被冻结且不可调整。给我找个办法让表格的宽度变满。

HTML:

   <table class="table table-striped table-hover" id="item_table">
       <thead>
        <tr class="bg-primary">
         <th>#</th>
          <th>Product Name</th>
          <th>Quantity</th>
          <th>Unit Price</th>
          <th>Discount</th>
          <th>Amount</th>
          <th><i class="fa fa-close"></i></th>
         </tr>
   </thead>
   <tbody id="item_list">
     <tr>
       <td>1</td>
       <td>Mens Full Sleeve Shirts</td>
       <td>1</td>
       <td>2200.00</td>
       <td>200.00</td>
       <td>2000.00</td>
       <td><span><i class="fa fa-trash"></i></span></td>
     </tr>
   </tbody>
</table>

CSS:

#item_table{
    display: table-row;
    table-layout:fixed;
}

#item_list{
    width: 100%;
    display: block;
    table-layout:fixed;
    height: 170px;
    overflow-y: scroll;
}

【问题讨论】:

    标签: html css html-table bootstrap-4


    【解决方案1】:

    不要将display:block 添加到表中。它会毁掉它。

    将表格 Insteed 包装成 div 并将 overflow:auto 添加到此元素。

    或者,如果使用引导程序,请将 table-responsive 类添加到容器中,如您在文档 here 中看到的那样

    【讨论】:

    • 它可以滚动整个表格,我只需要滚动tbody。
    【解决方案2】:

    嗨,请试试这个:

    tbody {
        height: 100px;       /* Just for the demo          */
        overflow-y: auto;    /* Trigger vertical scroll    */
        overflow-x: hidden;  /* Hide the horizontal scroll */
    }
    

    【讨论】:

    • 不起作用,它设置高度但不启用滚动条。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多