Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I position two divs vertically where the top one takes the remaining height?

Tags:

html

css

flexbox

The markup is like:-

<div id="parent">
    <div class="top">
        <ul>
            <li>...
        </ul>
    </div>
    <div class="bottom">
        <ul>
            <li>Option A
            <li>Option B
            <li>Option C
        </ul>
    </div>
</div>

And the CSS is:-

div {
    border: 1px solid black;
}

#parent {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
}

.top {
    max-height: 200px;
    overflow: scroll;
}

So, basically what I want is that bottom div should touch the lower edge of the window and the rest space should be taken by top div. My current approach of setting max-height in top div is clearly not good since based on screen size (on mobiles) it will be either too less or big. Can I achieve this using CSS only?

Here is a jsfiddle for this - http://jsfiddle.net/Ldr07h2r/

like image 571
AppleGrew Avatar asked Jan 09 '23 20:01

AppleGrew


2 Answers

This is pretty straightforward with Flexbox layout. In this case you don't have to specify an explicit height for the bottom <div>, the top one would take the remaining space:

EXAMPLE HERE

#parent {
    /* other declarations... */

    display: flex;
    min-height: 100vh;
    flex-direction: column;
}

.top {
    overflow: auto; /* Up to you! */
    flex: 1;
}

Have a look on these properties:

  • flex
  • flex-direction
like image 88
Hashem Qolami Avatar answered May 19 '23 01:05

Hashem Qolami


You have to position div.bottom absolute and set div.top height 100% - div.bottom.

Your final css has to be:

div {
    border: 1px solid black;
}

#parent {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
}

.top {
    height: calc(100% - 100px);
    overflow: scroll;
}
.bottom { 
    position: absolute;
    bottom: 0;
    width: 100%;
    height: 100px;
}

Here is the updated fiddle: http://jsfiddle.net/Ldr07h2r/4/

Good Luck!

like image 24
emmanuel Avatar answered May 19 '23 02:05

emmanuel