首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何用css使主要内容div填充屏幕高度

如何用css使主要内容div填充屏幕高度
EN

Stack Overflow用户
提问于 2013-09-07 03:26:50
回答 8查看 289.2K关注 0票数 106

所以我有一个包含页眉、主体和页脚的网页。我希望主体填充100%的页面(填充100%在页脚和页眉之间)我的页脚是位置绝对与底部: 0。每次我尝试将主体设置为100%高度或改变位置或其他什么,它也会溢出标题。如果使用top: 40将正文设置为绝对位置(因为我的标题是40px高),它就会太低40px,从而创建一个滚动条。

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

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

有人知道答案吗?

EN

回答 8

Stack Overflow用户

发布于 2014-12-20 05:21:25

有一个名为视口高度/视口宽度的CSS单位。

示例

类似于html,body{width: 100vw;}.mainbody{height: 100vh;}

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

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

票数 57
EN

Stack Overflow用户

发布于 2013-09-07 03:46:07

这使得内容主体居中,宽度最小,使我的表单不会滑稽地折叠:

代码语言:javascript
复制
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%;
}

我的布局页面看起来像这样:

代码语言:javascript
复制
<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>

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

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

票数 8
EN

Stack Overflow用户

发布于 2013-09-07 03:37:18

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

代码语言:javascript
复制
.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;
}

JSFiddle

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18665171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档