是否可以混合使用绝对定位和粘性定位?

问题描述 投票:0回答:1

在此示例中,内部灰色 div 的行为应该像粘性 div - 因此当您向下滚动页面时它会粘在屏幕顶部 - 但这并没有发生!我到底做错了什么?

<html>
<body>

<div style="position: absolute; top: 150px; left: 150px; border: 1px solid red">
    <div style="position: sticky; top: 0; background-color: #ddd; padding: 20px; margin: 30px">
        Richard is the best
    </div>
</div>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

</body>
</html>
css position sticky absolute
1个回答
0
投票

看起来你想对我使用

position: fixed
,也许我没有正确理解你的问题

<html>
    <body>
    
    <div style="position: fixed; top: 150px; left: 150px; border: 1px solid red">
        <div>
            Richard is the best
        </div>
    </div>
    
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>https://stackoverflow.com/posts/78706802/edit#
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    
    </body>
    </html>

© www.soinside.com 2019 - 2024. All rights reserved.