Put div below navigation bar and don't overlap content

前端 未结 5 1831
Happy的楠姐
Happy的楠姐 2021-02-04 10:44

My issue is that I fixed a navigation bar at the top of my webpage and it includes both side margins and top one. Below this navigation bar, I want to set an scrollable containe

5条回答
  •  不思量自难忘°
    2021-02-04 11:12

    I really don't like having to add elements that are just there for purposes of pushing content around. This is what positioning is designed to handle. The best way to do this is to start by adding padding to the top of your body, as @davidg stated in his answer. Further, to move your navbar into position, don't use margins. Instead using the top, left and right properties. The navbar-fixed-top class already sets the position to fixed, so you don't need to give any position property value. I also added the container-fluid class to your div (so that you get some padding inside) and a custom class scrollable to set the overflow property.

    DEMO

    CSS:

    html, body {
      height: 100%; /*Fixes the height to 100% of the viewport*/
    }
    body {
      padding-top: 87px; /*50px for the height of the navbar + 37px for the offset*/
      padding-bottom: 50px; /*50px for the height of the bottom navbar*/
    }
    .navbar-offset {
        top: 37px; /*Offsets the top navbar 37px from the top of the viewport*/
    }
    .container-fluid.scrollable {
      height: 100%; /*Sets the scrollable area to 100% of the viewport*/
      overflow: auto; /*Allows the scrollable area to have a scrollbar based on the height of its content*/
      background: #ccc;
    }
    

    HTML:

    
    
    

    BEGIN

    Hi World

    ...

    END

提交回复
热议问题