1

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.

5
  • 1
    Welcome to the CSS fluid height nightmare... JS is the answer. Commented May 22, 2013 at 16:49
  • You could run with flexbox... But it's not widely supported yet and considering it's history may never be. You'll have to use javascript for this. Commented May 22, 2013 at 16:53
  • You can make it appear to stretch down by styling the parent's background to match the child-bottom's background. What they don't know won't hurt them. Commented May 22, 2013 at 16:55
  • I can't just use styling because .child-bottom will have a scrollbar when elements inside outgrow it's height. =\ Commented May 22, 2013 at 18:57
  • Why does child-bottom need to take up all the remaining space? Commented May 22, 2013 at 21:14

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.