我有一个固定大小的容器,其中包含未知数量的自调整段落以及其他可能的元素。那个内容之后我也有一张桌子。我想要表格填充容器的剩余高度。有一个表格填充其容器的剩余高度
我有以下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