开发者

child div height of 100% being ignored

开发者 https://www.devze.com 2023-01-20 00:15 出处:网络
I\'m using peter\'s excellent example for a 100% height div with a sticky footer (HERE). I\'ve omitted the footer parts a i\'m not using it.

I'm using peter's excellent example for a 100% height div with a sticky footer (HERE). I've omitted the footer parts a i'm not using it.

However, whenever i use a child div and try to do the same thing it doesn't work. I want a child div to also take 100% of its parent's height. Here is CSS:

<style>


    html,body {
        margin:0;
        padding:0;
        height:100%; /* needed for container min-height */
        background:gray;

        font-family:arial,sans-serif;
        font-size:small;
        color:#666;
    }



    div#container {
        position:relative; /* needed for footer positioning*/
        margin:0 auto; /* center, not in IE5 */
        width:750px;
        background:#f0f0f0;

        height:auto !important; /* real browsers */
        height:100%; /* IE6: treaded as min-height*/

        min-height:100%; /* real browsers */
    }

    div#header {
        padding:1em;
        background:#ddd url("../csslayout.gif") 98% 10px no-repeat;
        border-bottom:6px double gray;
    }


    div#content {
        padding:1em 1em 5em; /* bottom padding for footer */
        position:relative; /* needed for footer positioning*/
        margin:0 auto; /* center, not in IE5 */

        height:auto !important; /* real browsers */
        height:100%; /* IE6: treaded as min-height*/

        min-height:100%; /* real browsers */            
    }
    div#content_subdiv{
        padding:1em 1em 5em; /* bottom padding for footer */
        back开发者_Python百科ground:#999;
        height:100%;

        color:#fff;
    }


</style>

and here is my HTML code:

<div id="container"> 

    <div id="header"> 
        <h1>CSS layout: 100% height with header and footer</h1> 
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eget massa dolor, rhoncus tempor nunc. Donec tempor odio eget purus vehicula auctor. </p> 
    </div> 

    <div id="content"> 

        <p> 
    Aenean quam mauris, iaculis non aliquet quis, facilisis sed turpis. Cras id erat velit, nec bibendum erat. Vivamus feugiat purus vitae velit dictum in vestibulum ante tristique. Vestibulum ut massa vel justo eleifend consectetur eget ut nisi. Phasellus ut diam nulla. Suspendisse potenti. Praesent blandit gravida facilisis. Donec elementum faucibus gravida. Nullam nec enim velit, ac scelerisque justo. Pellentesque lacus metus, adipiscing nec congue a, volutpat sollicitudin eros. Donec tortor leo, tempor non viverra at, molestie sed dui. Nullam ipsum purus, tempus elementum tincidunt id, iaculis at lectus. Vestibulum viverra mi in mauris ultrices sollicitudin
        </p> 
        <div id="content_subdiv"> this sub div not stretching to 100%; <br /> this sub div not stretching to 100%; <br /> this sub div not stretching to 100%; <br /> this sub div not stretching to 100%; <br /> this sub div not stretching to 100%; <br /></div>
    </div> 

</div> 


You can only apply min-height:100% to the first element of the body.

Put the header inside the content div or create a min-height:100% wrapper div.


Try this code then :

html,body {
    margin:0;
    padding:0;
    height:100%; /* needed for container min-height */
    background:gray ;
    font-family:arial,sans-serif;
    font-size:small;
    color:#666;
}



div#container {
    position:relative; /* needed for footer positioning*/
    margin:0 auto; /* center, not in IE5 */
    width:750px;
    background:#f0f0f0;

    height:auto !important; /* real browsers */
    height:100%; /* IE6: treaded as min-height*/

    min-height:100%; /* real browsers */
}

div#header {
    padding:1em;
    background:#ddd url("../csslayout.gif") 98% 10px no-repeat;
    border-bottom:6px double gray;
}


div#content {
    padding:1em 1em 5em; /* bottom padding for footer */
    position:relative; /* needed for footer positioning*/
    margin:0 auto; /* center, not in IE5 */

    height:auto !important; /* real browsers */
    height:100%; /* IE6: treaded as min-height*/

    min-height:100%; /* real browsers */            
}
div#content_subdiv{
    padding:1em 1em 5em; /* bottom padding for footer */
    background:#999;
   color:#fff;    }  
< div id="header "> 
    < h1 >CSS layout: 100% height with header and footer</ h1 > 
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eget massa dolor, rhoncus tempor nunc. Donec tempor odio eget purus vehicula auctor. </p> 
</div> 

<div id="content"> 

    <p> 
Aenean quam mauris, iaculis non aliquet quis, facilisis sed turpis. Cras id erat velit, nec bibendum erat. Vivamus feugiat purus vitae velit dictum in vestibulum ante tristique. Vestibulum ut massa vel justo eleifend consectetur eget ut nisi. Phasellus ut diam nulla. Suspendisse potenti. Praesent blandit gravida facilisis. Donec elementum faucibus gravida. Nullam nec enim velit, ac scelerisque justo. Pellentesque lacus metus, adipiscing nec congue a, volutpat sollicitudin eros. Donec tortor leo, tempor non viverra at, molestie sed dui. Nullam ipsum purus, tempus elementum tincidunt id, iaculis at lectus. Vestibulum viverra mi in mauris ultrices sollicitudin
    </p> 

</div> <div id="content_subdiv"> this sub div not stretching to 100%; <br /> this sub div not stretching to 100%; <br /> this sub div not stretching to 100%; <br /> this sub div not stretching to 100%; <br /> this sub div not stretching to 100%; <br /></div>

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号