我想让div始终修复浮动顶部出现在div中,并且我的代码如下所示:
HTML:
<p>Some on page content</p>
<div id="cover">
<div id="floatdiv">floating fix top bar</div>
<p>first contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p> <p>contents</p>
</div>
<p>Other content ..</p>
CSS:
#cover{
border: 1px solid red;
width: 500px;
height: 300px;
overflow: auto;
}
#floatdiv{
background-color:black;
color:white;
padding:10px;
}
如何做呢?
您可以position: absolute
用来保留div。然后使用width: inherit
获取父级的宽度,同时指定padding
totop
和bottom
以避免溢出。div
#floatdiv{
background-color:black;
color:white;
padding-top:10px;
padding-bottom:10px;
position: absolute;
width: inherit;
}
更新:
添加<p> </p>
以显示第一个值
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句