Bootstrap 4, navbar fixed-top and other sticky-top elements

霸气de小男生 提交于 2019-12-05 05:37:14

Bootstrap sticky-top has no offset for the Navbar since it sets top:0. You can add a custom class to account for the Navbar height.

.sticky-offset {
    top: 56px;
}

<div class="sticky-top sticky-offset">
  <ul class="list-group">
    <li class="list-group-item active">Cras justo odio</li>
    <li class="list-group-item">Dapibus ac facilisis in</li>
    <li class="list-group-item">Morbi leo risus</li>
    <li class="list-group-item">Porta ac consectetur ac</li>
    <li class="list-group-item">Vestibulum at eros</li>
  </ul>
</div>

Demo: https://www.codeply.com/go/QeJOvbYswd


Related: Bootstrap 4 fixed top nav and fixed sidebar

Zim's answer was helpful, but did not work for me in Safari 12.0.1 on MacOS (Chrome 70 was fine, worked as expected).

I think this is because sticky-top has a top:0px value which was taking priority over the custom class added.

So instead I simply added:

.sticky-top{
  top:56px;
}

Which fixed it in Safari too. Additionally, there's no need to add the custom class to the div.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!