Offset scroll anchor in HTML with Bootstrap 4 fixed navbar

前端 未结 5 572
孤独总比滥情好
孤独总比滥情好 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:09

    This solution adds a padding and removes the gap by a negative margin at the top. This worked best for me.

    section {
        padding-top: 56px;
        margin-top: -56px;
    }
    

    .offset {
        height: 54px;
    }
    
    /* START SNIPPET */
    section {
        padding-top: 56px;
        margin-top: -56px;
    }
    /* END SNIPPET */
    
    #section1 p, #section3 p {
        background-color: #ffffd;
    }
    #section2 p, #section4 p {
        background-color: #ccc;
    }
    p {
        min-height: 15rem;
    }
    
    
      
        
        
        
    
        
        
    
      
      
     
     
      

    Section 1

    Section 2

    Section 3

    Section 4

提交回复
热议问题