CSS高度不是100%

时间:2011-10-17 21:54:30

标签: css height positioning

我已经检查了许多与此类似的其他问题,但似乎没有解决方案可行。我有一个拥有正文的网站,然后是一个称为内容的内部div。我试过了:

body
{
    height: 100%;
}

content
{
    height: 100%;
}

body
{
}

content
{
    position: absolute;
    top: 0px;
    bottom: 0px;
}

这是大多数问题的答案。但由于某种原因,它不适合我们。当我们使用绝对定位进行强制时,它会延伸到超过页面长度,并且当我们希望它居中时也将我们的内容推向左侧(使用margin-left:auto; margin-right:auto;)

有什么建议吗?

2 个答案:

答案 0 :(得分:5)

除了body和你的div之外,还需要将html设置为100%高度,例如

html, body, div{
   height: 100%;
}

答案 1 :(得分:0)

我的解决方案:

<html>
<head>
<style>
body
{
    height: 100%;
    padding: 0;
    margin: 0;
}

#content
{
    height: 100%;
    background: red;
}
</style>
</head>
<body>
    <div id="content"></div>
</body>
</html>

工作正常。属性background: red;用于测试。