tags:

views:

276

answers:

5

Consider a table with three rows with heights 10, *, 10. I'd like the middle cell to be high enough to fit to the page vertically. Unfortunately "height:100%" doesn't work at table, tr, or td level, possibly due to standards. Even if it happens to work, I don't want 100%, I want 100% of clientHeight-20px :) I can always write script to calculate remaining clientHeight but I wonder if it can be achieved in HTML/CSS standards.

NOTE: I'm using table just for layout, if there are other ways to lay them down in a better way I'm ok with those approaches too.

+4  A: 

Try to leave table and use CSS. This is the first link on google (searching: css page layout) http://www.maxdesign.com.au/presentation/page_layouts/

You will spend more time at beginning, but then you will love CSS.

Regards, Lorenzo.

lg
What if he was using this for tabular data? Why assume he's not and give him an answer he's not looking for?
jonwd7
I'm not using tabular data, using table just for the layout. I'll take a peek to the link, thanks!
ssg
Glad to see you've updated your post indicating this. :) In that case, definitely stay away from using tables for layout. The link above is a good start.
jonwd7
+3  A: 

I've tested the following in Firefox and Safari and it works although it's perhaps not the nicest solution! What you'll see is the 20 height on row1 and row3 is still applied and the 100% makes up the rest. If you leave off the padding and margin from the body you'll get scrolling.

<html>
<head>
<style>
html,body { height:100%; padding:0; margin:0; }
</style>
</head>
<body>
<table cellpadding=0 cellspacing=0 style="height:100%;">
  <tr height="20" style="background-color:grey;">
    <td>row 1</td>
  </tr>
  <tr>
    <td>row 2</td>
  </tr>
  <tr height="20" style="background-color:grey;">
    <td>row 3</td>
  </tr>
</table>
</body>
</html>
Ian
I don't think that answers his question. Doesn't he want the middle row to fill the page?
James
This should make the middle row fill the page, as the table has a fixed height of 100%, and top+bottom have fixed 20px each. Leaving the rest of the height to the middle row.
peirix
I did not have to set anything on `html, body` in my example, and there is no scrolling. So I'm not sure what you are talking about there exactly.Also, he wanted 10px each, 20px total. :)
jonwd7
Interesting this works as I expected. I'll see what I'm doing wrong with my own implementation.
ssg
I accepted this one due to number of votes although there were more than one valid answer. I also pick lg's CSS recommendation as the second best answer. Thanks.
ssg
+1  A: 

Does this not work?

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>untitled</title>
    <!-- Date: 2009-07-13 -->
    <style type="text/css" media="screen">
     table {height: 100%; width: 100%;}
     td {border: 1px solid #000;}
    </style> 
</head>
<body>
<table>
    <tr height="10"><td>Data</td></tr>
    <tr height="100%"><td>Data</td></tr>
    <tr height="10"><td>Data</td></tr>
</table>
</body>
</html>

It does for me.

jonwd7
Yes that seems to work, I'm trying to understand why :)
ssg
+2  A: 

The "height: 100%" style will only work on elements that are inside an element that has the height property explicitly set. In your case, the table is likely to be inside the body tag and the body tag doesn't have a height set.

Sohnee
Thanks for explaining the root cause behind the behavior. I'm still looking for a solution though.
ssg
A: 

Have the same here - the simple examples above work, while my own page does not "stretch" the needed <tr> element.

What I found so far is that excluding the DOCTYPE (thus putting the browser into quirks rendering mode - even for FireFox!) makes my page behave like the simple examples, yet adding a DOCTYPE to these examples stops them from working.

I guess this is not really an answer yet, but it shows the direction in which to look further for the proper solution. Hopefully there is a way to achieve this "stretching" behaviour without the quirks mode.

EDIT: This answer worked for me. The table is wrapped into an absolutely positioned full-screen div. I guess what it does is the browser first calculates the div's dimensions, and then it knows how the table (and the tr inside it) should be sized. Works with DOCTYPE included, relieving, since I don't want to use the quirks rendering mode.

NPC