Offset scroll anchor in HTML with Bootstrap 4 fixed navbar

前端 未结 5 591
孤独总比滥情好
孤独总比滥情好 2020-12-29 06:17

So, I have this single page that consists of a few sections. Users can go to these sections by scrolling themselves or clicking in the navbar (a href with anchor). Due to th

5条回答
  •  滥情空心
    2020-12-29 07:13

    There are a few different ways to solve it, but I think the best way is to put a hidden pseudo element ::before each section. This is a CSS only solution, no JS or jQuery...

    section:before {
        height: 54px;
        content: "";
        display:block;
    }
    

    https://www.codeply.com/go/J7ryJWF5fr

    That will put the space needed to account for the fixed-top Navbar. You'll also want to remove the margin-top offset for #section1 since this method will work consistently for all sections and allow the scrollspy to work.


    Related
    How do I add a data-offset to a Bootstrap 4 fixed-top responsive navbar?
    Href Jump with Bootstrap Sticky Navbar

提交回复
热议问题