>我在页面中创建了两个div,一个是标题,另一个是内容
<div id="main">
<div id="header">
</div>
<div id="content">
</div>
</div>
风格是
<style>
#main{
height:100%;
}
#header{
height:100px
}
#content{
height:???? // to fit the content 100% on any kind of screen
}
</style>
我想根据屏幕调整内容。 屏幕高度可以是 100 像素,也可能是 2000 像素。我如何使用 css 做到这一点。我不希望底部有空格或滚动页面
如果您可以使用CSS3,则以下代码:
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#main{
height:100%;
background-color: blue;
}
#header{
height: 10%;
background-color: green;
}
#content{
height: -webkit-calc(100% - 105px);
background-color: red;
}
将在铬中解决问题。您必须测试其他浏览器的 calc
属性的替代项。您将不得不使用父元素的边距和填充才能获得您想要的效果,但这就是想法。
如果 CSS3 不是一个选项,那么你将不得不以百分比定义你的标题,除非有人比我聪明可以给你一个更好的替代:)。
添加这些行
html,body{height:100%;}.
#main{height:100%; }
#header{height:100px; background:#ccc; }
#content{min-height:90%; background:#666; }
这将使您在屏幕中进行调整
如果你想使用css进行这种布局,你必须查看这些。
display: box;
或
height: calc(100% - 100px);
但这些在旧浏览器中不受支持。 显示:框在IE9中不起作用。
一些JavaScript也是如此。
document.getElementById('content').style.height = (document.getElementById('main').clientHeight - 100) + "px";
在 body 的 onload 事件中调用它。
在 css 中试试这个
html, body { height: 100%; }
#main{height:100%; overflow:hidden}
#header{height:100px; background:green; }
#content{height:100%; background:#F00; overflow:auto; }
这是小提琴链接
添加html, body { height: 100%; }
并使用position:absolute
让两个内部div 都position:absolute
,它应该从浏览器的顶部开始。使用z-index
切换div。
.HTML
<div id="main">
<div id="header">c
</div>
<div id="content">
<div class="hidden">This div helps to push the content 100px below</div>
cxbvbvbvcbv
</div>
</div>
.CSS
html, body { height: 100%; }
#main{height:100%; position:relative}
#header{height:100px; background:green; position:absolute; top:0; left:0; width:100%; z-index:1000}
#content{height:100%; background:grey; position:absolute; top:0; left:0; width:100%; z-index:1; }
演示已更新