Div height 100% and expands to fit content
I have a div element on my page with its height set to 100%. The height of the body is also set to 100%. The inner div has a background and all that and is different from the body background. This works for making the div height 100% of the browser screen height, but the problem is I have content inside that div that extends vertically beyond the browser screen height. When I scroll down, the div ends at the point at which you had to begin scrolling the page, but the content overflows beyond that. How do I make the div always go all the way to the bottom to fit the inner content?
Here's a simplification of my CSS:
body {
height:100%;
background:red;
}
#some_div {
height:100%;
background:black;
}
Once I scroll the page, the blackness ends and the content flows onto the red background. It doesn't seem to matter whether I set the positon to relative or absolute on the #some_div, the problem occurs either way. The content inside the #some_div is mostly absolutely positioned, and it is dynamically generated from a database so its height can't be known in advance.
Edit: Here is a screenshot of the problem:
Solution 1:
Here is what you should do in the CSS style, on the main div
display: block;
overflow: auto;
And do not touch height
Solution 2:
Set the height
to auto
and min-height
to 100%
. This should solve it for most browsers.
body {
position: relative;
height: auto;
min-height: 100% !important;
}
Solution 3:
Usually this problem arises when the Child elements of a Parent Div are floated. Here is the Latest Solution of the problem:
In your CSS file write the following class called .clearfix along with the pseudo selector :after
.clearfix:after {
content: "";
display: table;
clear: both;
}
Then, in your HTML, add the .clearfix class to your parent Div. For example:
<div class="clearfix">
<div></div>
<div></div>
</div>
It should work always. You can call the class name as .group instead of .clearfix , as it will make the code more semantic. Note that, it is Not necessary to add the dot or even a space in the value of Content between the double quotation "". Also, overflow: auto; might solve the problem but it causes other problems like showing the scroll-bar and is not recommended.
Source: Blog of Lisa Catalano and Chris Coyier
Solution 4:
If you just leave the height: 100%
and use display:block;
the div
will take as much space as the content inside the div
. This way all the text will stay in the black background.