首页 > 解决方案 > div 在另一个 div 上方的固定或粘性位置

问题描述

我在底部有一个高度取决于其中的数据的 div,所以我不知道底部 div 的确切高度。

<div id="BottomOfPage">I don't know exactly height this div</div>

问题,我需要另一个 div 位置粘滞或固定在 id="BottomOfPage"

<div id="StickyOrFixed">image link</div>
<div id="BottomOfPage">i don't know exactly height this div</div>

我尝试但我仍然无法解决这个问题,我搜索了类似的问题但我找不到它,也许我错过了。请帮忙,谢谢。

我尝试

#StickyOrFixed{margin-bottom:20px;position:sticky;}
#StickyOrFixed{margin-bottom:20px;position:fixed;}

不起作用,它需要顶部或底部位置,但我不知道确切的高度 id="BottomOfPage"

标签: htmlcssposition

解决方案


我试试这个

<div id="StickyOrFixed" style="position:sticky;bottom:50px;margin-bottom:20px;">image link</div>
<div id="BottomOfPage">i don't know exactly height this div</div>

它解决了我需要的问题,如果 div id="BottomOfPage" 高度 100px,仍然粘在 id="BottomOfPage" 上方,margin-bottom:20px; . 滚动后粘滞将始终距离底部 50px。

我尝试这个需要2个多小时,在我提交这个问题后,我得到了答案。还是非常感谢。


推荐阅读