Bootstrap css hides portion of container below navbar navbar-fixed-top

后端 未结 7 1343
傲寒
傲寒 2020-12-22 20:35

I am building a project with Bootstrap and im facing little issue .I have a container below the Nav-top.My issue is that some portion of my container is hidden below the nav

相关标签:
7条回答
  • 2020-12-22 21:34

    I guess the problem you have is related to the dynamic height that the fixed navbar at the top has. For example, when a user logs in, you need to display some kind of "Hello [User Name]" and when the name is too wide, the navbar needs to use more height so this text doesn't overlap with the navbar menu. As the navbar has the style "position: fixed", the body stays underneath it and a taller part of it becomes hidden so you need to "dynamically" change the padding at the top every time the navbar height changes which would happen in the following case scenarios:

    1. The page is loaded / reloaded.
    2. The browser window is resized as this could hit a different responsive breakpoint.
    3. The navbar content is modified directly or indirectly as this could provoke a height change.

    This dynamicity is not covered by regular CSS so I can only think of one way to solve this problem if the user has JavaScript enabled. Please try the following jQuery code snippet to resolve case scenarios 1 and 2; for case scenario 3 please remember to call the function onResize() after any change in the navbar content:

    var onResize = function() {
      // apply dynamic padding at the top of the body according to the fixed navbar height
      $("body").css("padding-top", $(".navbar-fixed-top").height());
    };
    
    // attach the function to the window resize event
    $(window).resize(onResize);
    
    // call it also when the page is ready after load or reload
    $(function() {
      onResize();
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    0 讨论(0)
提交回复
热议问题