我想让主体有100%的浏览器高度。我可以用CSS来做吗?

我试着设置高度:100%,但它不起作用。

我想为页面设置一个背景色来填充整个浏览器窗口,但如果页面内容很少,我就会在底部得到一个丑陋的白色条。


当前回答

快速更新

html, body{
    min-height:100%;
    overflow:auto;
}

今天的CSS更好的解决方案

html, body{ 
  min-height: 100vh;
  overflow: auto;
}

其他回答

如果你想保持正文的边缘,不想要滚动条,使用下面的css:

html { height:100%; }
body { position:absolute; top:0; bottom:0; right:0; left:0; }

设置body {min-height:100%}将为您提供滚动条。

参见http://jsbin.com/aCaDahEK/2/edit?html,output上的演示。

尝试将html元素的高度设置为100%。

html, 
body {
    height: 100%;
}

主体通过其父(HTML)查看如何缩放动态属性,因此HTML元素也需要设置它的高度。

然而,body的内容可能需要动态更改。 将min-height设置为100%将实现这一目标。

html {
  height: 100%;
}
body {
  min-height: 100%;
}

CSS3有一个新的方法。

 height:100vh

它使ViewPort 100%等于高度。

所以你的代码也应该如此

 body{
 height:100vh; 
 }

这里更新

html
  {
    height:100%;
  }

body{
     min-height: 100%;
     position:absolute;
     margin:0;
     padding:0; 
}

Try

<html style="width:100%; height:100%; margin: 0; padding: 0;">
<body style="overflow:hidden; width:100%; height:100%; margin:0; padding:0;">