Resize DIv to fit the Cell

I have a table and in one of the Cells is a large DIV that hosts another table.

The intent is that when the table grows larger that the DIV "OVERFLOW:auto" style attribute pops up a scrollbar and allows the user to scroll to the rest of the table.

Now, the master table only has 3 rows:
1. Header with set height
2. Body row to hold the DIV cell, height not set.
3. Footer with set height

I want the body row to grow and shrink with the browser, it does.
but the DIV does not fill out the Cell as expected.

If I set the DIV height in the Style to a set pixel height, looks good, as long the browser is the correct height.

If the I set the DIV height to 100%, it stretches the cell to show the entire DIV and the footer is well off the page.

How do resize the DIV on the fly ?
Is there a style attrib that will do that ?
Do I need a Java statement ?

Thanks if your still reading.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sidwelleAuthor Commented:
var y = document.body.clientHeight;
var yDiv = y - 178;
//  = yDiv;
document.getElementById("Mydiv").style.posHeight = yDiv;
Unfortunately height is one of the CSS challenges where every ancestor needs to have a height specified including the body and HTML and we can't combine pixel heights along with percentage in a cross-browser efficient CSS.

You need to use a simple JavaScript in order to achieve this which sets the Div's height to its parent's cell then assign it on window load and window resize.

I've constructed a working example for you below.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    <style type="text/css">
        html, body {height:100%;margin:0;}
        .maintable {height:100%;width:100%;border:0;padding:0;border-collapse:collapse;}
        .maintable td {padding:0;}
        .header {background:red;height:100px;}
        .footer {background:blue;height:100px;}
        .body {}
        .bodydiv {background:black;height:100%;}
    <script type="text/javascript">
        function setHeight() {
            var pHeight = document.getElementById("bodyCell").offsetHeight + "px";
            document.getElementById("bodyDiv").style.height = pHeight ;
        function resetHeight() {
            document.getElementById("bodyDiv").style.height = "auto";
        window.onload = setHeight;
        window.onresize = resetHeight;
    <table class="maintable">
            <td class="header">&nbsp;</td>
        <td class="body" id="bodyCell">
            <div class="bodydiv" id="bodyDiv">&nbsp;</div>
            <td class="footer">&nbsp;</td>

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sidwelleAuthor Commented:
That works to, thanks for the help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.