andersonvom
andersonvom

Reputation: 11861

Expanding divs with CSS and no Javascript

Is there a way of making a DIV expand to fill the space left in it's parent container without having to use Javascript to calculate the necessary heights?

<div class="parent" style="height: 100%">
    <div class="child-top">
    </div>

    <div class="child-bottom" style="position: relative;">
    </div>
</div>

.parent is a sidebar that takes up the whole screen, .child-top height may vary depending on the content, and I would like .child-bottom to just take up the rest of the space with position relative so that I can correctly position other elements inside.

UPDATE: I can't use fixed heights in any of the elements. .child-top for obvious reasons, and .child-bottom will have an element with a scrollbar when its height outgrows the parent.

Upvotes: 1

Views: 371

Answers (1)

Mike Robinson
Mike Robinson

Reputation: 25159

If you're just looking to fill it up, you can use overflow:hidden on the parent to fake it. This comes with some caveats. Anything within the child bottom with a height greater than the child will be hidden, so take it as you will.

http://jsfiddle.net/hBLQR/

Your HTML:

<div class="parent">
    <div class="child-top">
        Hi there
    </div>

    <div class="child-bottom">
        Hi back
    </div>
</div>   

And the CSS:

.parent {
    position: absolute; 
    left: 0; 
    right: 200px; 
    top: 0; 
    bottom: 0; 
    overflow: hidden;

}

.child-top { 
    background: green; 
    height: 100px;
    width: 200px;
}

.child-bottom { 
    background: red; 
    height: 100%;    
    width: 200px;
}

Upvotes: 1

Related Questions