tags:

views:

2851

answers:

10

I need to create a table in a web page that has two rows. The table needs to fill the entire page so I have set the height and width of the table to 100% in the CSS stylesheet and the height of the html and body to 100% too.

However, I need to top row of the table to be exactly 100 pixels in height and the second row to expand to fit the remainder of the table. When I set the height of the top row, it doesn't work.

Is this behaviour possible? I tried the following but it doesn't work.

                <table style="border-collapse:collapse;height:100%;width:100%;">
                    <tr>
                        <td style="border:solid 1px black;height:100px">1</td>
                        <td style="border:solid 1px black">2</td>
                    </tr>
                    <tr>
                        <td style="border:solid 1px black">3</td>
                        <td style="border:solid 1px black">4</td>
                    </tr>
                </table>

Edit: I am using tabular data and specifically want to use a table, not divs. The sample above is to simplify a table with around 20 columns.

Edit 2: I have found the issue. It is the doctype definition in the HTML (added by Visual Studio). After removing...

...it works perfectly. So my new question is, is this OK to do or is there a correct way to do it with the DOCTYPE?

A: 

Yes, you can combine all measurements. Just try it out in different browsers.

Gamecat
+4  A: 

Without CSS this works:

<html>
  <body>
    <table height="100%" width="100%" border="1">
      <tr height="100">
        <td>This is item text.</td>
      </tr>

      <tr>
        <td>This is item text 2.</td>
      </tr>
    </table>
  </body>
</html>

So it is possible, I'll try it now with CSS.

_J_
This doesn't work for me. The top row gets really big and the bottom one small. They both change size as the page size changes.
Baffled by ASP.NET
Strange how all the suggestions being sent in work for the submitter but for you they appear to work in reverse? What browser are you using?
_J_
IE7. I think it is an Infragistics problem - the div is inside an Infragistics websplitter. I have asked tham for help but that could take days.
Baffled by ASP.NET
I now realise it is an issue with the DOCTYPE I had set but I am not sure if removing the DOCTYPE is sensible or if there is an alternative.
Baffled by ASP.NET
Sounds like a new question to me (cos I don't know the answer!)
_J_
A: 

Tried the following code in IE7, Mozilla 2, Chrome 1, Opera 9.5, Safari 3.2 and it works

<html>
<head>

</head>
 <body>
  <table height="100%" border="1">
<tr height="100px"><td>Some</td></tr>
<tr><td>Other</td></tr>
 </table>
 </body>
</html>
Rutesh Makhijani
It doesn't. The top cell is really big and the bottom one really small unless I take the 100% off the html and body styles, when the table gets small. I need the entire table to fill the page.
Baffled by ASP.NET
This solution works for me, just as my solution also works. IE6.
_J_
Sorry, my mistake. It does work on its own but I am putting it in an Infragistics web splitter panel and that seems to kill it :-(I am getting fed up of Infragistics web controls. The sizing on almost all of them is seriously annoying...
Baffled by ASP.NET
Can you not design the page without it?
_J_
Unfortunately not. The page is shown in two sections and the splitter allows you to resize both.
Baffled by ASP.NET
I now realise it is an issue with the DOCTYPE I had set but I am not sure if removing the DOCTYPE is sensible or if there is an alternative.
Baffled by ASP.NET
height="100px" - don't use 'px' in HTML, it's a CSS-only unit.
bobince
+1  A: 

Ok in light of new information (namely the tabular data), here is a solution:

<html>
<head>
<style type="text/css">
    #wrap { position: absolute; height: 100%; top: 0; right: 0; bottom: 0; left: 0; }
    table { width: 100%; height: 100%; }
    tr { margin: 0; }
    td { text-align: center; font-size: 80px; font-weight: bold; color: white; }
    #top { height: 100px; }
    #bottom { height: 100%; }
    #topleft { background-color: red; border: 3px solid black; }
    #topright { background-color: green; border: 3px solid yellow; }
    #bottomleft { background-color: yellow; border: 3px solid green; }
    #bottomright { background-color: blue; border: 3px solid red; }
</style>
</head>
<body>
<div id="wrap"> 
    <table>
    <tr id="top">
     <td id="topleft">1</td>
     <td id="topright">2</td>
    </tr>
    <tr id="bottom">
     <td id="bottomleft">3</td>
     <td id="bottomright">4</td>
    </tr>
  </table>
</div>  
</body>
</html>

I'll keep the one below for completeness if you want a non-tabular solution. There's no reason it can't be extended to a 2x2 grid either by use of floats or positioning:

<html>
<head>
  <style type="text/css" media="screen">
    #wrap {
      position: absolute;
      top: 0;
      right: 0;
      left: 0;
      bottom: 0;
    }

    #top {
      width: 100%;
      height: 100px;
      position: relative;
      top: 0;
      background-color: #CCC;
    }

    #bottom {
      position: relative;
      height: 100%;
      bottom: 0;
      width: 100%;
      background-color: yellow;
    }
  </style>
</head>
<body>
  <div id="wrap">
    <div id="top">Top</div>
    <div id="bottom">Bottom</div>
  </div>
  </body>
</html>
cletus
I am using tabular data.
Baffled by ASP.NET
...and when I try this, the bottom div doesn't seem to expand to fill the page (IE7)
Baffled by ASP.NET
I now realise it is an issue with the DOCTYPE I had set but I am not sure if removing the DOCTYPE is sensible or if there is an alternative.
Baffled by ASP.NET
You might want to consult this: http://hsivonen.iki.fi/doctype/
cletus
+1  A: 

You have given height only in one column of first row. Try to give height in both the columns or at the row level

Rutesh Makhijani
The height thing was a typo.How can I give a size to the second row? I want it to expand to fit.
Baffled by ASP.NET
+3  A: 

OK working with CSS.

HTML:

<html>
  <head>
    <link href="table.css" rel="stylesheet" type="text/css">
  </head>
  <body>
    <table>
      <tr class="fixed">
        <td>This is item text.</td>
      </tr>

      <tr>
        <td>This is item text 2.</td>
      </tr>
    </table>

  </body>
</html>

CSS:

table
{
 border: 1px solid black;
 height: 100%;
 width: 100%;
}

tr.fixed
{
 border: 1px solid black;
 height: 100px;
}

td
{
 border: 1px solid black;
}
_J_
That works. Do you know why my html at the top doesn't? I can't spot the difference.
Baffled by ASP.NET
I'm fixing the height of the table row <tr> in my CSS, you're fixing the height of the data row <td>
_J_
Strangely, when I put your code in place it stops working. This is when I add it within a div that is sized to 100% height and width, and the div is inside an Infragistics WebSplitter pane.
Baffled by ASP.NET
Obviously the div or the splitter pane are overriding some of the settings.
_J_
this only works for a quirksmode/transitional doc
annakata
I guess so. I haven't a clue how to fix it though. I thought if the styles were set directly against the html that they couldn't be overridden in that way.
Baffled by ASP.NET
it's unlikely they are - inline styles do have a very high specificity
annakata
Is my answer correct then, at least for the purposes of the initial question?
_J_
+2  A: 

Well, there's me thinking I know how to do this, so I run out the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <style type="text/css">
    html, body 
    {
     height: 100%;
     margin: 0;
     padding: 0;
    }
    table 
    {
     height: 100%;
     width: 100%;
     background-color: blue;
     border-collapse:collapse;
    }
    tr#fixed
    {
     background: silver; 
     height: 100px;
    }
    tr#expand
    {
     background: gray;
    }
    td 
    {
     text-align: center;
    }
    </style>
</head>
<body>
    <table>
     <tbody>
      <tr id="fixed">
       <td>100</td>
      </tr>
      <tr id="expand">
       <td>*</td>
      </tr>
     </tbody>
    </table>
</body>
</html>

(note, strict + CSS)

And I find that although this works in Firefox, Chrome and anything !IE I like, IE has some very unusual behaviour here. Long story short IE is disregarding the pixel dimensions and instead compares relative heights! It misreports this in the IE dev toolbar, but you can clearly see it if you add and change a height on tr#expand

If you set a height of 100px, you'll find both tr's actually render at 50%. At 200px, you'll get a 33/66 split, at 50px you'll get a 66/33 split! This seems to be true for other units like em, and I suspect it might be related to content since it has edge behaviour at low units like 5px. As far as I can see IE is outright broken here. This is not a bug I have seen before, nor one I've been able to google. Until a workaround suggests itself, it seems to me that you're SOL for a strict solution.

annakata
A: 

it don't work in mozilla 3

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

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style type="text/css" media="screen">
     html, body {
      height:100%;
      margin:0;
     }
     table {
      background-color:#ccc;
      width:100%;
      height:100%;
     }
     th {
      height:100px;
      background-color:#fcfcfc;
     }
    </style>

    <title>TableTest</title>

</head>

<body>

<table>
    <tr>
     <th>Header1</th>
     <th>Header2</th>
    </tr>
    <tr>
     <td>Data1</td>
     <td>Data2</td>
    </tr>
</table>

</body>
</html>

This is what I believe a more semantic approach without using the id of class attributes in the table tags, taking advantage of the <th> tag to make the difference between both rows. This code doesn't work as intended in IE6/7, it might work in IE8, I tested it and works correctly in Firefox 3, Safari 3.1.2, Opera 9.50 and Camino 1.6.6.

elbicho
without seeing the data you can't be sure th is more semantic, but in principle it's a good point (and a shortcut)
annakata
A: 

Cletus' answer above works for my table, in non-quirks mode, I'd just like to clarify - does it work thanks to the #wrap element having defined the dimensions that the included table then stretches to the 100% of? It seems that its existence is the only difference from just defining a 100%-height table and a 100%-height row in it (which only works in quirks mode).

Please confirm, as I'd like to understand the solution before implementing it in our product. Thank you!

NPC