Bootstrap NavBar with left, center or right aligned items

前端 未结 10 1204
旧巷少年郎
旧巷少年郎 2020-11-21 11:24

In Bootstrap, what is the most platform-friendly way to create a navigation bar that has Logo A on the left, menu items in the center, and Logo B on the rig

10条回答
  •  粉色の甜心
    2020-11-21 11:58

    Smack my head, just reread my answer and realized the OP was asking for two logo's one on the left one on the right with a center menu, not the other way around.

    This can be accomplished strictly in the HTML by using Bootstrap's "navbar-right" and "navbar-left" for the logos and then "nav-justified" instead of "navbar-nav" for your UL. No addtional CSS needed (unless you want to put the navbar-collapse toggle in the center in the xs viewport, then you need to override a bit, but will leave that up to you).

    
    

    Bootply: http://www.bootply.com/W6uB8YfKxm


    For those who got here trying to center the "brand" here is my old answer:

    I know this thread is a little old, but just to post my findings when working on this. I decided to base my solution on skelly's answer since tomaszbak's breaks on collaspe. First I created my "navbar-center" and turned off float for the normal navbar in my CSS:

    .navbar-center
    {
       position: absolute;
       width: 100%;
       left: 0;
       text-align: center;
       margin: auto;
    }
    
    .navbar-brand{
       float:none;
    }
    

    However the issue with skelly's answer is if you have a really long brand name (or you wanted to use an image for your brand) then once you get to the the sm viewport there could be overlapping due to the absolute position and as the commenters have said, once you get to the xs viewport the toggle switch breaks (unless you use Z positioning but I really didn't want to have to worry about it).

    So what I did was utilize the bootstrap responsive utilities to create multiple version of the brand block:

    So now the lg and md viewports have the brand centered with links to the left and right, once you get to the sm viewport your links drop to the next line so that you don't overlap with your brand, and then finally at the xs viewport the collaspe kicks in and you are able to use the toggle. You could take this a step further and modify the media queries for the navbar-right and navbar-left when used with navbar-brand so that in the sm viewport the links are all centered but didn't have the time to vet it out.

    You can check my old bootply here: www.bootply.com/n3PXXropP3

    I guess having 3 brands might be just as much hassle as the "z" but I feel like in the world of responsive design this solution fits my style better.

提交回复
热议问题