我正在尝试实现可滚动的表体。我能够成功地做到了,但现在我面临宽度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%
答案 0 :(得分:2)
试用此代码。我认为这对你最好
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>
答案 1 :(得分:2)
如果要使表格可滚动并设置其高度,则只需使用表格本身即可。
删除桌面上的所有样式,并添加此css:
table {
width: 100%;
max-height: 80px;
display: block;
overflow-y: scroll;
position: relative;
}
看到这个小提琴: https://jsfiddle.net/stpgd6x6/3/
答案 2 :(得分:1)
您需要清除最初带有表格的几种样式,并设置宽度为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;
}
&#13;
<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>
&#13;
更详细的答案可以在这里找到:HTML table with 100% width, with vertical scroll inside tbody
答案 3 :(得分:1)
对此有一个更简单的CSS解决方案。不用担心元素的宽度和滚动,只需用容器div包裹表格,并对(
echo RET
) | plink.exe -raw example.com -P port
元素使用样式position: sticky
。下面的示例片段-
th
.table-container {
max-height: 160px;
border: 1px solid red;
overflow: auto;
}
th {
position: sticky;
top: 0;
background: white;
}
td, th {
padding: 4px 8px;
}