我需要将子 div 设置为相对于其父 div 固定(位置:固定)。父 div 设置为 overflow: auto。
只是为了让我的观点非常清楚:我不希望子 div 相对于 HTML 屏幕是固定的,而是相对于父 div 固定的。父 div 滚动,因为它被设置为 overflow: auto。它有很多文本,导致出现滚动条(不是屏幕滚动条,而是 div 的)。我需要子 div 相对于其父 div 保持固定位置。
有没有纯HTML+CSS的解决方案,还是只能通过javascript来实现?
最佳答案
我明白你在说什么... basically this is the problem .使用固定位置,您可以在滚动时将元素固定在那里,但这必须仅与窗口相关。如果您尝试使用 position:absolute
使其相对于容器,它不会粘住而是随内容滚动……解决方案? a wrapper of course ! :D
基本结构是这样的:
HTML
<div class="blah">
<div class="inner">text content</div>
<div class="meh">fixed content</div>
</div>
CSS
div.blah
{
position:relative;
}
div.inner
{
width:500px;
height:500px;
overflow:auto;
}
div.meh
{
background-color:#f00;
position:absolute;
left:20px;
bottom:20px;
}
享受:)
关于css - 相对于其父 div 的固定位置层 (div)(溢出 :auto),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976048/