有人知道这个问题的解决方法吗?
我正在尝试计算一个 div 的宽度:
#container {
position: fixed;
top: 0;
left: 0;
z-index: 9;
display: none;
background: #fff;
width: calc(100% - 1em);
padding: .5em;
}
有人知道这个问题的解决方法吗?
我正在尝试计算一个 div 的宽度:
#container {
position: fixed;
top: 0;
left: 0;
z-index: 9;
display: none;
background: #fff;
width: calc(100% - 1em);
padding: .5em;
}
编辑2:
这是针对Webkit浏览器(如评论所要求)的版本,经过最新版Chrome测试,完全可用:http://jsfiddle.net/HvVst/1/
使用了-webkit-calc()
代替了calc()
。
编辑:然后您需要将1像素边框减去0.5em填充,像这样:http://jsfiddle.net/HvVst/
HTML:
<div id="banner">
FIXED HEADER
</div>
<div id="main">
normal div
<br/>
Sample Text 1
<br/>
Sample Text 2
<br/>
Sample Text 3
<br/>
Sample Text 4
<br/>
Sample Text 5
<br/>
Sample Text 6
<br/>
Sample Text 7
<br/>
Sample Text 8
</div>
CSS:
#banner{
position: fixed;
top: 0;
left: 0;
z-index: 9;
background: #fff;
width: calc(100% - 1em);
padding: calc(0.5em - 1px); /* <-- THIS ONE !!*/
border: 1px solid red;
height: 50px;
}
#main{
margin-top: calc(50px + 1em);
border: 1px solid blue;
height: 500px;
}
它适用于position fixed/absolute,但是(如果没有为绝对定位指定相对父级,则始终适用于fixed),它是基于窗口宽度而不是容器宽度。
(100% - 1em)= 窗口宽度除去滚动条的100%...
你想要实现什么?
如果你想让绝对定位在父级边界内,将父级设置为position:relative...