tags:

views:

913

answers:

3

Why does height="100%" attribute fail to work in the following snippet?

<table height="100%" width="100%">

EDIT: code as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

<div style="width:250px;height:90px;">

<table id="experiences-mirror" width="100%" border="1" cellpadding="0" height="100%">
    <caption>Table Name</caption>
    <tbody><tr><th>Company</th><th>Job Title</th><th>Industry</th><th>Job Function</th><th>Start Date</th><th>End Date</th></tr>
    <tr title="hint">
     <td><input name="company1" class="w100" type="text"></td><td><input name="jobTitle1" class="w100" value="" type="text"></td><td><input name="industry1" class="w100" value="" type="text"></td><td><input name="jobFunction1" class="w100" type="text"></td><td><input name="startDate1" class="w100" type="text"></td><td><input name="endDate1" class="w100" type="text"></td>
    </tr>
    <tr title="hint">
     <td><input name="company2" class="w100" type="text"></td><td><input name="jobTitle2" class="w100" type="text"></td><td><input name="industry2" class="w100" type="text"></td><td><input name="jobFunction2" class="w100" type="text"></td><td><input name="startDate2" class="w100" type="text"></td><td><input name="endDate2" class="w100" type="text"></td>
    </tr>
    <tr title="hint">
     <td><input name="company3" class="w100" type="text"></td><td><input name="jobTitle3" class="w100" type="text"></td><td><input name="industry3" class="w100" type="text"></td><td><input name="jobFunction3" class="w100" type="text"></td><td><input name="startDate3" class="w100" type="text"></td><td><input name="endDate3" class="w100" type="text"></td>
    </tr>
    <tr title="hint">
     <td><input name="company4" class="w100" type="text"></td><td><input name="jobTitle4" class="w100" type="text"></td><td><input name="industry4" class="w100" type="text"></td><td><input name="jobFunction4" class="w100" type="text"></td><td><input name="startDate4" class="w100" type="text"></td><td><input name="endDate4" class="w100" type="text"></td>
    </tr>
</tbody></table>

</div>
A: 

Maybe because of table-layout CSS property. Generally if the table is auto-layout, then it sizes itself to the content, regardless of what you put in the <table> tag. The table rendering algorithm is convoluted at best.

Your best bet is to make sure your table is fixed layout and then make sure the first row of TD elements have fixed widths. Otherwise you'll run into many issues, including many browser-specific ones.

Glenn
A: 

There is a very simple reason: The table element doesn't have a height attribute.

If you want to set the height on a table, you have to use CSS:

style="height:100%;"
Guffa
I tried it with firefox,not working.
Shore
And not working with IE neither.
Shore
I tried it with your code (adding the neccessary tags to make it a real html document of course), and it's working just fine. The table height is a minimum value so you can't use it to force the table to get smaller than the height of it's cells. For that you need to set the height of the rows or the cells.
Guffa
A: 

The problem is that your table's calculated height is already greater than 90px. I bumped it up to 290px and your code worked.

The preferable way to set the height, however, is through CSS, such as:

#experiences-mirror {
 width: 100%;
 height: 100%;
}
Jason Berry
I can't bump the height,how can I make the table resize itself to fit the container?
Shore
It already does work (tested in FF and IE). The <div> is 90px high, the <table> has a calculated height exceeding that. If you change the 90px on the <div> to 290px you would see it working.
Jason Berry
But I can't change 90px to 290px,the size can't be changed.I need the table to resize itself automatically.
Shore
It IS automatically resizing itself. Do you want it to automatically get smaller or something?
Jason Berry
yes,in this case,the table should automatically get smaller if it's already bigger than parent container.
Shore
That isn't possible. It's like trying to pour a bucket of water into a cup, the overflow has to go somewhere! You can control the visibility of that "overflow" with the CSS overflow property: http://www.w3schools.com/Css/pr_pos_overflow.asp
Jason Berry