表格高度在Div元素内100%

时间:2012-12-14 10:56:37

标签: html css

您好我想将表格高度100%设置为其父div,而不是在div中定义高度。我的代码不起作用。我不知道我错过了什么。小提琴link

<div style="overflow:hidden">
<div style="float:left">a<br />b</div>
<table cellpadding="0" cellspacing="0" height="100%" style="border:solid 1px #000000">
<tr>
<td valign="middle">c</td></tr>
</table>
</div>

5 个答案:

答案 0 :(得分:24)

如果没有为div分配高度值,则无法实现。

添加此

body, html{height:100%}
div{height:100%}
table{background:green; width:450px}    ​

<强> DEMO

答案 1 :(得分:1)

你需要在div <div style="overflow:hidden">中有一个高度,否则它不知道100%是什么。

答案 2 :(得分:1)

这是你可以做到的 -

HTML -

<div style="overflow:hidden; height:100%">
     <div style="float:left">a<br>b</div>
     <table cellpadding="0" cellspacing="0" style="height:100%;">     
          <tr><td>This is the content of a table that takes 100% height</td></tr>  
      </table>
 </div>

CSS -

html,body
{
    height:100%;
    background-color:grey;
}
table
{
    background-color:yellow;
}

请参阅DEMO

更新:好吧,如果您不想将100%的高度应用于父容器,那么这是一个可以帮助您的jQuery解决方案 -

Demo-using jQuery

脚本 -

 $(document).ready(function(){
    var b= $(window).height(); //gets the window's height, change the selector if you are looking for height relative to some other element
    $("#tab").css("height",b);
});

答案 3 :(得分:0)

有类似的问题。我的解决方案是给内表一个1px的固定高度,并将内表中td的高度设置为100%。不顾一切,它运行良好,在IE,Chrome和FF测试!

答案 4 :(得分:0)

将表的高度设置为其容器我必须这样做:

1)设置&#34;位置:绝对&#34;

2)删除单元格的冗余内容(!)

相关问题