我面临的问题,而设置高度宽度溢出滚动。

<style> 
     tbody{
       height:50px;display:block;overflow:scroll
     }
   </style>

       <h3>Table B</h3>
    <table style="border: 1px solid red;width:300px;display:block">
        <thead>
            <tr>
                <td>Name</td>
                <td>phone</td>
            </tr>
        </thead>
        <tbody style='height:50px;display:block;overflow:scroll'>
            <tr>
                <td>AAAA</td>
                <td>323232</td>
            </tr>
            <tr>
                <td>BBBBB</td>
                <td>323232</td>
            </tr>
            <tr>
                <td>CCCCC</td>
                <td>3435656</td>
            </tr>
        </tbody>
    </table>

在这里参观我的小提琴

我想要表B像表A一样溢出滚动。

任何帮助都将不胜感激。

非常感谢, M。


当前回答

这是一个简单的方法,使用滚动条到表体

/* It is simple way to use scroll bar to table body*/ table tbody { display: block; max-height: 300px; overflow-y: scroll; } table thead, table tbody tr { display: table; width: 100%; table-layout: fixed; } <table> <thead> <th>Invoice Number</th> <th>Purchaser</th> <th>Invoice Amount</th> <th>Invoice Date</th> </thead> <tbody> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> </tbody> </table>

其他回答

在我的情况下,我想有响应表的高度,而不是固定的高度像素,因为其他答案正在显示。为了做到这一点,我使用了百分比可见高度属性和溢出div包含表:

&__table-container {
  height: 70vh;
  overflow: scroll;
}

这样,表将随着窗口大小的调整而扩展。

HTML:

<table id="uniquetable">
    <thead>
      <tr>
        <th> {{ field[0].key }} </th>
        <th> {{ field[1].key }} </th>
        <th> {{ field[2].key }} </th>
        <th> {{ field[3].key }} </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="obj in objects" v-bind:key="obj.id">
        <td> {{ obj.id }} </td>
        <td> {{ obj.name }} </td>
        <td> {{ obj.age }} </td>
        <td> {{ obj.gender }} </td>
      </tr>
    </tbody>
</table>

CSS:

#uniquetable thead{
    display:block;
    width: 100%;
}
#uniquetable tbody{
    display:block;
    width: 100%;
    height: 100px;
    overflow-y:overlay;
    overflow-x:hidden;
}
#uniquetable tbody tr,#uniquetable thead tr{
    width: 100%;
    display:table;
}
#uniquetable tbody tr td, #uniquetable thead tr th{
   display:table-cell;
   width:20% !important;
   overflow:hidden;
}

这也可以:

#uniquetable tbody {
    width:inherit !important;
    display:block;
    max-height: 400px;
    overflow-y:overlay;
  }
  #uniquetable thead {
    width:inherit !important;
    display:block;
  }
  #uniquetable tbody tr, #uniquetable thead tr {
    display:inline-flex;
    width:100%;
  }
  #uniquetable tbody tr td,  #uniquetable thead tr th {
    display:block;
    width:20%;
    border-top:none;
    text-overflow: ellipsis;
    overflow: hidden;
    max-height:400px;
  }

我在为我的项目寻找同样的解决方案。我是这么做的。 数据取自上面的一个答案,用于演示。

.tableContainer { max-height: 150px; overflow-y: scroll; padding: 0; margin: 0; border: 1px solid black; width: 100%; } table { width: 100%; } thead { position: sticky; top: 0px; background-color: white; } /* for styling only */ td { border: 1px solid black; } <div class="tableContainer"> <table> <thead> <th>Invoice Number</th> <th>Purchaser</th> <th>Invoice Amount</th> <th>Invoice Date</th> </thead> <tbody> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> <tr> <td>INV-1233</td> <td>Dinesh Vaitage</td> <td>$300</td> <td>01/12/2017</td> </tr> </tbody> </table> </div>

如果你想让body显示滚动条,设置它的display: block;。

设置显示:表格;这样它就能保持表的行为。

为了均匀分布单元格,使用table-layout: fixed;。

演示


CSS:

table, tr td {
    border: 1px solid red
}
tbody {
    display: block;
    height: 50px;
    overflow: auto;
}
thead, tbody tr {
    display: table;
    width: 100%;
    table-layout: fixed;/* even columns width , fix width of table too*/
}
thead {
    width: calc( 100% - 1em )/* scrollbar is average 1em/16px width, remove it from thead width */
}
table {
    width: 400px;
}

如果tbody不显示滚动,因为content小于height或max-height,可以随时使用:overflow-y: scroll;设置滚动。演示2


<编辑S/updateS> 2019 - 04/2021


重要提示:这种使表可滚动的方法在某些情况下有缺陷。(见下面的评论)顺便说一下,这个帖子中的一些重复的答案应该得到同样的警告

警告:此解决方案断开头部和身体细胞网格;这意味着在大多数实际情况下,您将无法从表中获得所期望的单元格对齐。注意这个解决方案使用了一个hack来保持它们的对齐:

Anyhow, to set a scrollbar, a display reset is needed to get rid of the table-layout (which will never show scrollbar). Turning the <table> into a grid via display:grid/contents will also leave a gap in between header and scrollable part, to mind about. (idem if built from divs) overflow:overlay; has not yet shown up in Firefox ( keep watching it) position:sticky will require a parent container which can be the scrolling one. make sure your thead can be sticky if you have a few rows and rowspan/colspan headers in it (it does not with chrome).

到目前为止,仅通过CSS还没有完美的解决方案。有一些平均的方法来选择,所以它适合你自己的表格(表格布局:固定;是. .修复表和列的宽度,但javascript可能会被用来重置这些值=>退出纯CSS)

另一种方法是将表格包装在一个可滚动的元素中,并将标题单元格设置为固定在顶部。

这种方法的优点是不需要更改主体上的显示,可以让浏览器来计算列宽,同时保持标题单元格宽度与数据单元格列宽度一致。

/* Set a fixed scrollable wrapper */ .tableWrap { height: 200px; border: 2px solid black; overflow: auto; } /* Set header to stick to the top of the container. */ thead tr th { position: sticky; top: 0; } /* If we use border, we must use table-collapse to avoid a slight movement of the header row */ table { border-collapse: collapse; } /* Because we must set sticky on th, we have to apply background styles here rather than on thead */ th { padding: 16px; padding-left: 15px; border-left: 1px dotted rgba(200, 209, 224, 0.6); border-bottom: 1px solid #e8e8e8; background: #ffc491; text-align: left; /* With border-collapse, we must use box-shadow or psuedo elements for the header borders */ box-shadow: 0px 0px 0 2px #e8e8e8; } /* Basic Demo styling */ table { width: 100%; font-family: sans-serif; } table td { padding: 16px; } tbody tr { border-bottom: 2px solid #e8e8e8; } thead { font-weight: 500; color: rgba(0, 0, 0, 0.85); } tbody tr:hover { background: #e6f7ff; } <div class="tableWrap"> <table> <thead> <tr> <th><span>Month</span></th> <th> <span>Event</span> </th> <th><span>Action</span></th> </tr> </thead> <tbody> <tr> <td>January</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>February. An extra long string.</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>March</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>April</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>May</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>June</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>July</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>August</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>September</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>October</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>November</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> <tr> <td>December</td> <td>AAA</td> <td><span>Invite | Delete</span></td> </tr> </tbody> </table> </div>