2012-08-22 75 views
0

我试图让div有100%的高度。这始终是一个痛苦的屁股...... 我希望我的#wrapper指定是100%,我的#middle是藏汉..100高度div元素之间2静态高度

<div id="wrapper" style="margin-left:auto; margin-right:auto; width:900px;"> 
    <div id="top" style="height: 100px;width:inherit;"> 
     This stays top, with static height. 
    </div> 
    <div id="middle" style="width:inherit;"> 
     This should take all space that is left in height... 
    </div> 
    <div id="footer" style="height:50px;width:inherit;"> 
     This stays in bottom, with static height 
    </div> 
</div> 

JsFiddle demo

+0

帮助我们帮助您在jsfiddle上进行现场演示。 –

+0

继承人一个jsfiddle演示: http://jsfiddle.net/6Xgsz/9/ –

回答

2

使用可以使用绝对定位和棘手overflow: auto度过,即使没有JS:

html, body { 
 
    height: 100%; 
 
} 
 
#wrapper { 
 
    position: relative; 
 
    margin-left:auto; 
 
    margin-right:auto; 
 
    width:500px; 
 
    height: 100%; 
 
} 
 
#top { 
 
    height: 100px; 
 
    width: 100%; 
 
    background: blue; 
 
} 
 
#middle { 
 
    position:absolute; 
 
    bottom: 50px; 
 
    top: 100px; 
 
    overflow: auto; 
 
    width: 100%; 
 
    background: red; 
 
} 
 
#footer { 
 
    position:absolute; 
 
    bottom: 0px; 
 
    height:50px; 
 
    width: 100%; 
 
    background: green; 
 
}
<div id="wrapper"> 
 
    <div id="top"> 
 
     This stays top, with static height. 
 
    </div> 
 
    <div id="middle"> 
 
     This should take all space that is left in height... 
 
    </div> 
 
    <div id="footer"> 
 
     This stays in bottom, with static height 
 
    </div> 
 
</div>

注:使用width: 100%;代替width: inherit;,IE7不支持

+0

工作就像一个魅力! –

+0

不客气。 – zessx

0

Uhmm你可以试一下用JavaScript或jquery如果我是正确的。 不要忘记链接jQuery。

<script type="text/javascript" language="javascript"> 
$(document).ready(function() 
{ 
    //minus 150 because > top = 100px, footer = 50px == 150px 
    //offsetHeight gives you the height in pixels from the div "wrapper" 
    newHeight = document.getElementById('wrapper').offsetHeight - 150; 

    //updates the height of the div #middle 
    $("#middle").css({'height' : newHeight}); 
}); 
</script> 

它为我^^