我有一个包装div,它将2个div相邻。在这个容器上方,我有一个包含我标题的div。包装div必须是100%减去标题的高度。标题约为60像素。这是固定的。所以我的问题是:如何设置我的包装div的高度为100%减去60 px?
<div id="header"></div>
<div id="wrapper">
<div id="left"></div>
<div id="right"></div>
</div>
答案 0 :(得分:241)
在CSS3中你可以使用
height: calc(100% - 60px);
答案 1 :(得分:77)
这是一个工作的CSS,在Firefox / IE7 / Safari / Chrome / Opera下测试。
* {margin:0px;padding:0px;overflow:hidden}
div {position:absolute}
div#header {top:0px;left:0px;right:0px;height:60px}
div#wrapper {top:60px;left:0px;right:0px;bottom:0px;}
div#left {top:0px;bottom:0px;left:0px;width:50%;overflow-y:auto}
div#right {top:0px;bottom:0px;right:0px;width:50%;overflow-y:auto}
“overflow-y”未经w3c批准,但每个主流浏览器都支持它。如果内容太高,你的两个div #left和#right将显示一个垂直滚动条。
为了在IE7下工作,您必须通过添加DOCTYPE来触发符合标准的模式:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<style type="text/css">
*{margin:0px;padding:0px;overflow:hidden}
div{position:absolute}
div#header{top:0px;left:0px;right:0px;height:60px}
div#wrapper{top:60px;left:0px;right:0px;bottom:0px;}
div#left{top:0px;bottom:0px;left:0px;width:50%;overflow-y:auto}
div#right{top:0px;bottom:0px;right:0px;width:50%;overflow-y:auto}
</style>
</head>
<body>
<div id="header"></div>
<div id="wrapper">
<div id="left"><div style="height:1000px">high content</div></div>
<div id="right"></div>
</div>
</body>
答案 2 :(得分:47)
如果您需要支持IE6,请使用JavaScript以管理包装器div的大小(在读取窗口大小后设置元素的位置(以像素为单位))。如果您不想使用JavaScript,则无法完成此操作。有一些解决方法,但需要一两个星期才能使它在每种情况下和每个浏览器中都能正常工作。
对于其他现代浏览器,请使用此css:
position: absolute;
top: 60px;
bottom: 0px;
答案 3 :(得分:6)
很棒的...现在我已经停止使用%了他他......除了主容器,如下所示:
<div id="divContainer">
<div id="divHeader">
</div>
<div id="divContentArea">
<div id="divContentLeft">
</div>
<div id="divContentRight">
</div>
</div>
<div id="divFooter">
</div>
</div>
这是css:
#divContainer {
width: 100%;
height: 100%;
}
#divHeader {
position: absolute;
left: 0px;
top: 0px;
right: 0px;
height: 28px;
}
#divContentArea {
position: absolute;
left: 0px;
top: 30px;
right: 0px;
bottom: 30px;
}
#divContentLeft {
position: absolute;
top: 0px;
left: 0px;
width: 250px;
bottom: 0px;
}
#divContentRight {
position: absolute;
top: 0px;
left: 254px;
right: 0px;
bottom: 0px;
}
#divFooter {
position: absolute;
height: 28px;
left: 0px;
bottom: 0px;
right: 0px;
}
我在所有已知浏览器中对此进行了测试,并且工作正常。使用这种方式有什么缺点吗?
答案 4 :(得分:3)
您可以执行类似 height:calc(100% - nPx); 的操作,例如 height:calc(100% - 70px);
答案 5 :(得分:1)
div {
height: 100%;
height: -webkit-calc(100% - 60px);
height: -moz-calc(100% - 60px);
height: calc(100% - 60px);
}
确保使用较少的
height: ~calc(100% - 60px);
否则少就不能正确编译
答案 6 :(得分:0)
这并没有完全回答所提出的问题,但它确实创造了你想要达到的相同视觉效果。
<style>
body {
border:0;
padding:0;
margin:0;
padding-top:60px;
}
#header {
position:absolute;
top:0;
height:60px;
width:100%;
}
#wrapper {
height:100%;
width:100%;
}
</style>
答案 7 :(得分:0)
在此示例中,您可以识别不同的区域:
<html>
<style>
#divContainer {
width: 100%;
height: 100%;
}
#divHeader {
position: absolute;
left: 0px;
top: 0px;
right: 0px;
height: 28px;
background-color:blue;
}
#divContentArea {
position: absolute;
left: 0px;
top: 30px;
right: 0px;
bottom: 30px;
}
#divContentLeft {
position: absolute;
top: 0px;
left: 0px;
width: 200px;
bottom: 0px;
background-color:red;
}
#divContentCenter {
position: absolute;
top: 0px;
left: 200px;
bottom: 0px;
right:200px;
background-color:yellow;
}
#divContentRight {
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
width:200px;
background-color:red;
}
#divFooter {
position: absolute;
height: 28px;
left: 0px;
bottom: 0px;
right: 0px;
background-color:blue;
}
</style>
<body >
<div id="divContainer">
<div id="divHeader"> top
</div>
<div id="divContentArea">
<div id="divContentLeft">left
</div>
<div id="divContentCenter">center
</div>
<div id="divContentRight">right
</div>
</div>
<div id="divFooter">bottom
</div>
</div>
</body>
</html>
答案 8 :(得分:0)
我还没有看到这样的帖子,但我想我已经把它放在那里了。
<div class="main">
<header>Header</header>
<div class="content">Content</div>
然后CSS:
body, html {
height: 100%;
margin: 0;
padding: 0;
}
.main {
height: 100%;
padding-top: 50px;
box-sizing: border-box;
}
header {
height: 50px;
margin-top: -50px;
width: 100%;
background-color: #5078a5;
}
.content {
height: 100%;
background-color: #999999;
}
这是一个有效的jsfiddle
注意:我不知道浏览器的兼容性是什么。我正在玩替代解决方案,这看起来效果很好。
答案 9 :(得分:-1)
使用外包装div设置为100%,然后你的内包装div 100%现在应相对于那个。
我确信这曾经适合我,但显然不是:
<html>
<body>
<div id="outerwrapper" style="border : 1px solid red ; height : 100%">
<div id="header" style="border : 1px solid blue ; height : 60px"></div>
<div id="wrapper" style="border : 1px solid green ; height : 100% ; overflow : scroll ;">
<div id="left" style="height : 100% ; width : 50% ; overflow : scroll; float : left ; clear : left ;">Some text
on the left</div>
<div id="right" style="height : 100% ; width 50% ; overflow : scroll; float : left ;">Some Text on the
right</div>
</div>
</div>
</body>
</html>
答案 10 :(得分:-1)
如果您不想使用绝对定位和所有爵士乐,这是我想要使用的修复:
你的HTML:
<body>
<div id="header"></div>
<div id="wrapper"></div>
</body>
你的css:
body {
height:100%;
padding-top:60px;
}
#header {
margin-top:60px;
height:60px;
}
#wrapper {
height:100%;
}