我有一個固定大小的容器,其中包含未知數量的自調整段落以及其他可能的元素。那個內容之後我也有一張桌子。我想要表格填充容器的剩餘高度。有一個表格填充其容器的剩餘高度
我有以下HTML:
<div id="container">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer mattis. Sed semper dui sed ante. Sed luctus tincidunt nisl. Proin iaculis adipiscing nisl. Class aptent taciti sociosqu ad litora amet.</p>
<p>Lorem ipsum dolor sit amet, consectetur cras amet.</p>
<table>
<thead>
<tr>
<th>One</th>
<th>Two</th>
<th>Three</th>
<th>Four</th>
</tr>
</thead>
<tbody>
<tr>
<td>111</td>
<td>222</td>
<td>33333</td>
<td>4444</td>
</tr>
<tr>
<td>111</td>
<td>222</td>
<td>33333</td>
<td>4444</td>
</tr>
<tr>
<td>111</td>
<td>222</td>
<td>33333</td>
<td>4444</td>
</tr>
<tr>
<td>111</td>
<td>222</td>
<td>33333</td>
<td>4444</td>
</tr>
</tbody>
</table>
</div>
用下面的CSS:
#container {
width: 500px;
height: 500px;
background: red;
}
p {
margin-bottom: 20px;
}
table {
height: 100%;
width: 100%;
}
table,
table th,
table td{
border: 1px solid black;
}
這樣做會導致表取容器的高度,而不僅僅是其餘區域。我已經嘗試將容器和孩子設置爲css中的表格和表格行,並且要麼導致其他元素太大,要麼鬆動邊緣。
小提琴,可以發現在:http://jsfiddle.net/QLwkU/
我希望的是純CSS的解決方案,但我沒有問題,如果它是一個合理的和可重複使用的jQuery的解決方案。
我已經試過:
var totalHeight = 0;
$('#container').children(':not(table)').each(function(){
totalHeight += $(this).outerHeight(true);
});
$('#container table').css({'height': ($('#container').outerHeight() - totalHeight)});
但是,它使較爲困難的邊距添加到表,如果需要一個。
任何想法或建議將不勝感激。
你爲什麼不設置高度:自動;或身高:100%;到容器? – 2012-07-31 22:04:42
因爲我有一個固定的容器高度,我不希望容器自己調整大小。我只想讓表格在放置其他內容後僅佔用剩餘空間。 – HJ05 2012-07-31 22:10:03
你需要怎樣的跨瀏覽器?你可以用'display:table-cell'或[flexible box model](http://coding.smashingmagazine.com/2011/09/19/css3-flexible-box-layout-explained/)來欣賞,但瀏覽器支持有限(對Flexbox非常有限)。你確定你希望你的桌子的單元格根據兄弟尺寸變化很大嗎? – 2012-07-31 22:10:53