如何用css制作主要内容div填充屏幕高度

use*_*516 76 html javascript css

所以我有一个带有标题,主体和页脚的网页.我希望主体填充页面的100%(在页脚和页眉之间填充100%)我的页脚是绝对位置底部:0.每次我尝试将主体设置为100%高度或更改位置或其他东西它也将溢出标题.如果将body设置为绝对位置top: 40(因为我的标题是40px高),它将向下移动40px太远,创建一个滚动条.

我创建了一个简单的html文件,因为我实际上无法从实际项目中发布整个页面/ css.使用示例代码,即使主内容体填满屏幕,它也会向下移动40px(因为我认为是标题).

html,
body {
  margin: 0;
  padding: 0;
}

header {
  height: 40px;
  width: 100%;
  background-color: blue;
}

#maincontent {
  background-color: green;
  height: 100%;
  width: 100%;
}

footer {
  height: 40px;
  width: 100%;
  background-color: grey;
  position: absolute;
  bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<html>

<head>
  <title>test</title>
  <link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
  <header></header>
  <div id="maincontent">

  </div>

  <footer></footer>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

谁知道答案?

sud*_*ban 113

  • 不需要高度%
  • 不需要jQuery

(!)使用bottom&top拉伸div:

.mainbody{
    position: absolute;
    top: 40px; /* Header Height */
    bottom: 20px; /* Footer Height */
    width: 100%;
}
Run Code Online (Sandbox Code Playgroud)

检查我的代码:http: //jsfiddle.net/aslancods/mW9WF/

  • 如果您知道页眉和页脚的大小,此解决方案只能正常工作.如果您需要独立于页眉和页脚的高度工作,请查看@Mark Murphy的解决方案. (4认同)

Mar*_*phy 60

没有Javascript,没有绝对定位,也没有固定的高度.

这是一个所有CSS/CSS唯一方法,不需要固定的高度或绝对定位:

CSS

.container { 
  display: table;
}
.content { 
  display: table-row; 
  height: 100%; 
}
.content-body { 
  display: table-cell;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="container">
  <header class="header">
    <p>This is the header</p>
  </header>
  <section class="content">
    <div class="content-body">
      <p>This is the content.</p>
    </div>
  </section>
  <footer class="footer">
    <p>This is the footer.</p>
  </footer>
</div>
Run Code Online (Sandbox Code Playgroud)

在这里看到它:http://jsfiddle.net/AzLQY/

这种方法的好处是页脚和标题可以增长以匹配其内容,身体将自动调整自己.您也可以选择用css限制其高度.


joy*_*nks 48

有一个名为viewport height/viewport width的CSS单元.

.mainbody{height: 100vh;} 同样 html,body{width: 100vw;}

或90vh =视口高度的90%.

**IE9 +和大多数现代浏览器.

  • 这是一个很好的解决方案,但在所有现代浏览器中都不支持vh,例如iOS 7.1 safari和android chrome <4.4 (2认同)
  • 我在移动浏览器上看到vh单元有问题。URL栏似乎已计入整个vh。因此,当您滚动并且URL栏折叠时,似乎出现了小故障,该网站尝试重新计算。就演示而言,它是薄弱的。有人解决吗? (2认同)

Mat*_*hew 6

这允许一个居中的内容体,其最小宽度使我的表单不会崩溃搞笑:

html {
    overflow-y: scroll;
    height: 100%;
    margin: 0px auto;
    padding: 0;
}

body {
    height: 100%;
    margin: 0px auto;
    max-width: 960px;
    min-width: 750px;
    padding: 0;
}
div#footer {
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    height: 60px;
}

div#wrapper {
    height: auto !important;
    min-height: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

div#pageContent {
    padding-bottom: 60px;
}

div#header {
    width: 100%;
}
Run Code Online (Sandbox Code Playgroud)

我的布局页面如下所示:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
    <title></title>
</head>
<body>
<div id="wrapper">
        <div id="header"></div>
        <div id="pageContent"></div>
        <div id="footer"></div>
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

示例:http://data.nwtresearch.com/

还有一点需要注意,如果你想要像你添加的代码一样的整个页面背景,请height: auto !important;从包装器div中删除:http://jsfiddle.net/mdares/a8VVw/


Tri*_*y12 5

使用top: 40pxbottom: 40px(假设你的页脚也是 40px)没有定义的高度,你可以让它工作。

.header {
    width: 100%;
    height: 40px;
    position: absolute;
    top: 0;
    background-color:red;
}
.mainBody {
    width: 100%;
    top: 40px;
    bottom: 40px;
    position: absolute;
    background-color: gray;
}
.footer {
    width: 100%;
    height: 40px;
    position: absolute;
    bottom: 0;
    background-color: blue;
}
Run Code Online (Sandbox Code Playgroud)

JSFiddle