将div拉伸以填充整个页面的方法

12
<html style="margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;">
    <body style="height:100%;width:100%;">
        <div style="height:20px;background-color:red;"></div>
        <div style="background-color:black;"></div>
        <div style="height:20px;background-color:blue;"></div>
    </body>
</html>

在将第一个和第三个 div 放置后,我如何使第二个 div 拉伸以填充剩余的空间?

3个回答

12

如果您想要粘性页脚系统,则可以使用以下技术:

* {
    margin: 0;
}

html, body {
    height: 100%;
}

.wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -20px;
}

.footer, .push {
    height: 20px;
}

http://ryanfait.com/resources/footer-stick-to-bottom-of-page/

的内容是有关如何将页脚固定在页面底部的资源链接。

5

如果我正确理解了你的意图(谁知道呢..):

在线演示 (编辑)

HTML:

<div id="top"></div>
<div id="mid"></div>
<div id="bot"></div>

CSS:

html, body {
    margin: 0;
    padding: 0;
    border: 0;
}
body {
    color: #fff
}

#top, #mid, #bot {
    position: absolute;
    width: 100%
}

#top {
    height: 20px;
    background: red;

    top: 0;
}
#mid {
    background: #000;

    top: 20px;
    bottom: 20px
}
#bot {
    height: 20px;
    background: blue;

    bottom: 0
}

0

使用全部的%或全部的像素(在JS中查找屏幕像素高度)

 <html style="margin:0px 0px 0px 0px;padding:0px 0px 0px 0px;">
    <body style="height:100%;width:100%;">
    <div style="height:3%;background-color:red;"></div>
    <div style="background-color:black; height:94%;"></div>
    <div style="height:3%;background-color:blue;"></div>
    </body>
    </html>

其他完美的答案源自上面的答案

<html >
<style type="text/css">
* {
    margin: 0;
}

html, body {
    height: 100%;
}

.wrapper {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -20px;
}

.footer, .push {
    height: 20px;
}
.header {
    height: 20px;
}
</style>
<body >

<div class="wrapper" style="background-color:#ffcc00">
   <div class="header" style="background-color:#cccccc"></div>
   <div class="push"></div>
</div>
<div class="footer" style="background-color:#cccccc"></div>
</body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接