Make a div fill the height of the remaining screen space
2015 update: the flexbox approach
There are two other answers briefly mentioning flexbox; however, that was more than two years ago, and they don't provide any examples. The specification for flexbox has definitely settled now.
Note: Though CSS Flexible Boxes Layout specification is at the Candidate Recommendation stage, not all browsers have implemented it. WebKit implementation must be prefixed with -webkit-; Internet Explorer implements an old version of the spec, prefixed with -ms-; Opera 12.10 implements the latest version of the spec, unprefixed. See the compatibility table on each property for an up-to-date compatibility status.
(taken from https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes)
All major browsers and IE11+ support Flexbox. For IE 10 or older, you can use the FlexieJS shim.
To check current support you can also see here:
http://caniuse.com/#feat=flexbox
Working example
With flexbox you can easily switch between any of your rows or columns either having fixed dimensions, content-sized dimensions or remaining-space dimensions. In my example I have set the header to snap to its content (as per the OPs question), I've added a footer to show how to add a fixed-height region and then set the content area to fill up the remaining space.
html,body { height: 100%; margin: 0;}
.box { display: flex; flex-flow: column; height: 100%;}
.box .row { border: 1px dotted grey;}
.box .row.header { flex: 0 1 auto; /* The above is shorthand for: flex-grow: 0, flex-shrink: 1, flex-basis: auto */}
.box .row.content { flex: 1 1 auto;}
.box .row.footer { flex: 0 1 40px;}
<!-- Obviously, you could use HTML5 tags like `header`, `footer` and `section` -->
<div class="box"> <div class="row header"> <p><b>header</b> <br /> <br />(sized to content)</p> </div> <div class="row content"> <p> <b>content</b> (fills remaining space) </p> </div> <div class="row footer"> <p><b>footer</b> (fixed height)</p> </div></div>
Make div (height) occupy parent remaining height
Expanding the #down
child to fill the remaining space of #container
can be accomplished in various ways depending on the browser support you wish to achieve and whether or not #up
has a defined height.
Samples
.container { width: 100px; height: 300px; border: 1px solid red; float: left;}.up { background: green;}.down { background: pink;}.grid.container { display: grid; grid-template-rows: 100px;}.flexbox.container { display: flex; flex-direction: column;}.flexbox.container .down { flex-grow: 1;}.calc .up { height: 100px;}.calc .down { height: calc(100% - 100px);}.overflow.container { overflow: hidden;}.overflow .down { height: 100%;}
<div class="grid container"> <div class="up">grid <br />grid <br />grid <br /> </div> <div class="down">grid <br />grid <br />grid <br /> </div></div><div class="flexbox container"> <div class="up">flexbox <br />flexbox <br />flexbox <br /> </div> <div class="down">flexbox <br />flexbox <br />flexbox <br /> </div></div><div class="calc container"> <div class="up">calc <br />calc <br />calc <br /> </div> <div class="down">calc <br />calc <br />calc <br /> </div></div><div class="overflow container"> <div class="up">overflow <br />overflow <br />overflow <br /> </div> <div class="down">overflow <br />overflow <br />overflow <br /> </div></div>
Expand element to fill remaining area of parent container
Not sure here...
I want it to expand downward to fill the remaining room in the container.
For the ul
, is height: 100%
inside a display: flex;
container would be okay?
*{
box-sizing: border-box;
margin: 0;
}
.container{
width:400px;
height:400px;
border: 1px dotted red;
display: flex;
flex-direction: column;
}
[type='text']{
width: 100%;
}
ul{
background-color: yellow;
height: 100%;
}
<div class="container">
<input type="text"/>
<ul>
<li>Item</li>
</ul>
</div>
CSS Div fill remaining height
Give the container:
display:flex;
flex-direction:column;
and for the element:
flex:1;
The demo:
https://jsfiddle.net/ugjfwbg4/1/
body { background-color: red; height: 100%;}
.wrap { height: 100vh; width: 100%; padding: 20px; background-color: yellow; display:flex; flex-direction:column;}
.top { width: 100%; height: 50px; background-color: blue;}
.mid { width: 100%; background-color: green; flex:1; display:flex; flex-direction:column;}
.left{ flex:1; width: 50%; background-color: red;}
.bottom { width: 100%; height: 50px; background-color: blue;}
<div class="wrap"> <div class="top"></div>
<div class="mid"> <div class="left">left</div> </div>
<div class="bottom"></div></div>
How to make div occupy remaining height?
Use absolute positioning:
#div1{ width: 100%; height: 50px; background-color:red;/*Development Only*/}#div2{ width: 100%; position: absolute; top: 50px; bottom: 0; background-color:blue;/*Development Only*/}
<div id="div1"></div><div id="div2"></div>
DIV won't fill remaining height of parent
Flex-box to the rescue. I took the .view
class and changed its display to flex
with a flex-direction: column
. This perserved the same behavior that was already in place but when I added flex-grow: 1
to .image-cntnr
it took up all of the remaining space properly without being larger than the remain space.
Take a look at the updated codepen.
Heres the css that I add/changed
.view {
display: flex;
flex-direction: column;
}
.image-cntnr {
background-color: cyan;
flex-grow: 1;
}
How to make a div take the remaining height of parent container
You need to make sure that the v-container
and v-layout
will have their height set to 100%. Also, the display: flex
and flex-flow: column
need to be set on xs6
not the inner div
:
.container, .layout {
height: 100%;
}
.xs6 {
display: flex;
flex-direction: column;
}
.height {
flex: 1 1 auto;
}
How to fill 100% of remaining height?
You should be able to do this if you add in a div (#header
below) to wrap your contents of 1.
If you float
#header
, the content from#someid
will be forced to flow around it.Next, you set
#header
's width to 100%. This will make it expand to fill the width of the containing div,#full
. This will effectively push all of#someid
's content below#header
since there is no room to flow around the sides anymore.Finally, set
#someid
's height to 100%, this will make it the same height as#full
.
JSFiddle
HTML
<div id="full">
<div id="header">Contents of 1</div>
<div id="someid">Contents of 2</div>
</div>
CSS
html, body, #full, #someid {
height: 100%;
}
#header {
float: left;
width: 100%;
}
Update
I think it's worth mentioning that flexbox is well supported across modern browsers today. The CSS could be altered have #full
become a flex container, and #someid
should set it's flex grow to a value greater than 0
.
html, body, #full {
height: 100%;
}
#full {
display: flex;
flex-direction: column;
}
#someid {
flex-grow: 1;
}
CSS: How to make div fill remaining height inside of a flex-child
It would be more helpful when you would of provided your existing CSS to better understand what you are trying to do. However I hope the example below will help you figure out how to solve what you are trying to accomplish.
Html:
<div class="flex-parent-row">
<div class="flex-child">
<div class="auto-height"> auto div</div>
<div class="i-want-this-one-to-fill-remaining-height"> fill remaining div</div>
</div>
</div>
CSS:
.flex-parent-row {
display: flex;
flex-direction: column;
height: 200px;
width: 500px;
border: 1px solid #000;
}
.flex-child {
border: 1px solid #000;
background-color: red;
display: flex;
flex-direction: column;
flex-grow: 1;
}
.auto-height {
background: orange;
}
.i-want-this-one-to-fill-remaining-height {
flex-grow: 1;
background-color: lightblue;
}
If you need additional help please provide more code.
Related Topics
How to Make Div Have 100% Height of Parent, Independent of Children'S Size? Complex Layout
Two Images, Side by Side Responsive
How to Clear Input Field After Submitting Data in Angular
How to Do a Post Request in HTML Without Opening a New Tab
How to Center an Element Horizontally and Vertically
Best Way to Center a ≪Div≫ on a Page Vertically and Horizontally
How to Change the Style of the Title Attribute Inside an Anchor Tag
Binding Select Element to Object in Angular
How to Avoid Decimal Values in Input Type Number
Placing Two Divs on Top of Each Other Without Using Position:Absolute
How to Limit the Maximum Files Chosen When Using Multiple File Input
How to Allow Http Content Within an Iframe on a Https Site
What to Do Regular Expression Pattern Doesn't Match Anywhere in String