我正在尝试使用某种类型的可滚动叠加层来覆盖网站的其余部分.我似乎无法在我的固定元素中的绝对元素上获得100%的高度.
http://codepen.io/intellix/pen/GBltK
section {
background: red;
position: fixed;
top:0;
right:0;
bottom:0;
left:0;
overflow-x:auto;
}
article {
background: blue;
position: absolute;
top:0;
width:300px;
bottom: 0;
left: 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我设置底部:0; 在绝对元素上,它在页面不溢出时填充高度.页面溢出时会留下空隙.
当我使用bottom:auto在我的绝对元素上,它会溢出溢出的高度,但留下一个没有溢出的间隙!
如果您调整窗口大小以使内容适合然后调整大小以使内容不适合,您可以看到它在两种情况下都不起作用.
我想你想使用min-height和bottom:auto here.
article {
background: blue;
position: absolute;
top:0;
width: 300px;
bottom: auto;
min-height: 100%;
left: 0;
}
Run Code Online (Sandbox Code Playgroud)
你需要min-height:100%;和不能使用的原因height:100%;是因为当section滚动内容的高度实际上大于 100%时.
更长的探索:
对于locate(position: relative|fixed|absolute;)元素,基于百分比的高度是相对于其偏移父项确定的.在article元素的情况下,它的偏移父section元素是元素.
该section元素使用top:0px;,并bottom:0px;确定它的高度.这些值由它作为html元素的偏移父项的高度决定
html是一个特殊情况,其中height:100%;是可视窗口的大小,这就是您的可滚动元素的高度大于100%的原因.