![](/img/trans.png)
[英]How to adjust max height to percentage while maintaining tbody only scroll?
[英]How To: 100% Height Table only Scroll tbody
是否可以使用CSS滾動100%高度表的內容而不是標題,只顯示tbody內容側面的滾動條而不是標題行? 謝謝!
表格很棘手。 我想你想將它們用於語義和后備目的,但它們有點不靈活。
幸運的是,有些人已經想出了不是一個,而是實現可滾動效果的兩種好方法....在〜2005年。
http://www.cssplay.co.uk/menu/tablescroll.html
他們仍然需要額外的html標記才能使它成為可能而第二個表有效地使用嵌套表,但是fallback / plain HTML看起來像普通的普通表。
方法#1
帶有填充的外部div,為元素創建“字段”。 內部div使內部表可滾動。 thead
表行絕對定位,使其保持在正文的頂部,與頁腳相同。 因為內部div是overflow: auto
和定義的高度,其余的表行在可分辨區域中內嵌。
方法#2
外表是普通表,頁眉和頁腳正常設置樣式。 但是外表的tbody只包含一行,一列,並且在其中有另一個表。 此列具有已定義的高度和overflow: auto
因此其中的內容(僅包含內容)將在其中滾動。
一個JavaScript解決方案是使用'浮動標題'。 你把整個表放在一個帶有溢出集的div中,然后JavaScript將克隆表頭並將它放在div的頂部,有點像Apple接口。
一個jQuery示例是http://fixedheadertable.com/
請注意,如果您正在對表執行其他操作,例如客戶端排序,過濾等,這會變得非常復雜。
我希望現在還為時不晚,你還活着,從那時起事情有了很大改善:)
您可以使用:
table {
display: inline-grid;
grid-template-areas:
"head-fixed"
"body-scrollable";
}
thead {
grid-area: head-fixed;
}
tbody {
grid-area: body-scrollable;
overflow: auto;
height: calc(100vh - 55px);
}
JSFiddle上具有100%高度和固定標題的可滾動表(tbody): http : //jsfiddle.net/gengns/p3fzdc5f/
您可以使用flex顯示進行操作。 無需硬編碼尺寸。
這是一個如何在表中使用固定標題和可滾動內容的示例。 碼:
<html style="height: 100%">
<head>
<meta charset=utf-8 />
<title>Fixed Header in table</title>
<!-- Reset browser defaults -->
<link rel="stylesheet" href="reset.css">
</head>
<body style="height: 100%">
<table style="display: flex; height: 100%; flex-direction: column">
<thead>
<tr>
<td>HEADER<br/>------------</td>
</tr>
</thead>
<tbody style="flex: 1; overflow: auto">
<tr>
<td>
<div>
CONTENT - START<br/>
<script>
for (var i=0 ; i<1000 ; ++i) {
document.write(" Very long content!");
}
</script>
<br/>CONTENT - END
</div>
</td>
</tr>
</tbody>
</table>
</body>
</html>
據我所知,純CSS不可能(至少不是跨瀏覽器),但使用jQuery插件它是可能的,非常簡單,例如jQuery.FixedTable 。
我有一個類似的問題,只有標題下的內容滾動並保持在剩余高度的100%。 這是我的修復:
http://jsfiddle.net/ajkochanowicz/YDjsE/
這適用於更簡單的用例。
#container {
border: 1px solid red;
width: 200px;
height: 250px; /* This can be any height and everything fills in */
padding-top: 55px;
}
#something-else {
height: 55px;
width: 200px;
background-color: green;
margin-top:-55px;
}
#overflow {
border: 1px solid blue;
width:198px;
overflow-y: auto;
height: 100%;
}
如果可能在您的場景中,一個簡單的直接替代方法是將標題放在一個單獨的div中,並將表放在另一個具有apt高度的div中,並將以下屬性應用於div。
overflow-x: hidden;
overflow-y: scroll;
墊很簡單。 這是你想要做的
CSS:
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
html, body{ width: 100%; height: 100%;}
table{
width: 100%;
height: 100%;
background-color: #aaa;
}
tbody{
width: 100%;
height: 100%;
-display: block;
background-color: #ccc;
overflow-y: scroll;
overflow-x: hidden;
}
td{
width: 100px;
height: 200px;
background-color: #eee;
}
</style>
HTML:
<table>
<thead>
<tr>
<th>Month</th>
<th>Savings</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
</table>
試試這個CSS文件:
table{
width: 100%;
height: 400px;
background-color: #aaa;
}
tbody{
background-color: #CCCCCC;
display: block;
height: 400px;
overflow-x: hidden;
overflow-y: scroll;
width: 100%;
}
td{
width: 100px;
height: 30px;
background-color: #eee;
}
thead {
display: block;
height: 30px;
width: 100%;
}
在我的Firefox中工作..沒有在其他任何地方測試.. :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.