身体在100%,最小高度为100%,但内容延伸到浏览器屏幕

Chr*_*itz 2 html css

我知道我已经看到了这个问题的解决方案,但我找不到它的生命.

我已经将html和body元素扩展到了dom的100%.它们的最小高度为100%.问题是当我的内容超出浏览器屏幕的底部时,正文将不会随之扩展:

<!DOCTYPE html>
<html>
    <head>
        <title>Damn you body, extend!!</title>
        <style type="text/css">
            html,body{
                height     :100%;
                min-height :100%;
            }
            html{
                background-color:gray;
            }
            body{
                width            :90%;
                margin           :auto;
                background-color :white;
            }
            body > div{
                background-color :red;
                height           :50px;
                width            :80px;
                margin           :auto;
                text-align       :center;
            }
        </style>
    </head>
    <body>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
      <div>testitem <br />testItem</div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我可以发誓最小高度会导致身体伸展以适应内容,但我显然是错的.有人能指出它真的很快吗?

pzi*_*zin 6

如果你只是设置一个应该工作正常min-height:

html, body {
  min-height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

正如你在这里看到的:http://jsfiddle.net/7hg7m

  • 对于我在这里推荐的html和body,有一个特殊的'height:100%`和`min-height:100%`的组合,并附有解释:http://stackoverflow.com/questions/17555682/height-100-or -min-高度-100换HTML和体元素 (3认同)