Bootstrap NavBar with left, center or right aligned items

前端 未结 10 1211
旧巷少年郎
旧巷少年郎 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).

    <nav class="navbar navbar-default" role="navigation">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <div class="navbar-brand navbar-left"><a href="#"><img src="http://placehold.it/150x30"></a></div>
    
      </div>
      <div class="navbar-brand navbar-right"><a href="#"><img src="http://placehold.it/150x30"></a></div>
    
      <div class="navbar-collapse collapse">
        <ul class="nav nav-justified">
            <li><a href="#">home</a></li>
            <li><a href="#about">about</a></li>
        </ul>
      </div>
    </nav>
    

    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:

    <nav class="navbar navbar-default" role="navigation">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <div class="navbar-brand visible-xs"><a href="#">Brand That is Really Long</a></div>
    </div>
    <div class="navbar-brand visible-sm text-center"><a href="#">Brand That is Really Long</a></div>
    <div class="navbar-brand navbar-center hidden-xs hidden-sm"><a href="#">Brand That is Really Long</a></div>
    
    <div class="navbar-collapse collapse">
      <ul class="nav navbar-nav navbar-left">
          <li><a href="#">Left</a></li>
          <li><a href="#about">Left</a></li>
          <li><a href="#">Left</a></li>
          <li><a href="#about">Left</a></li>
          <li><a href="#">Left</a></li>
          <li><a href="#about">Left</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#about">Right</a></li>
        <li><a href="#contact">Right</a></li>
        <li><a href="#about">Right</a></li>
        <li><a href="#contact">Right</a></li>
        <li><a href="#about">Right</a></li>
        <li><a href="#contact">Right</a></li>
      </ul>
    </div>
    

    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.

    0 讨论(0)
  • 2020-11-21 11:59

    This is a dated question but I found an alternate solution to share right from the bootstrap github page. The documentation has not been updated and there are other questions on SO asking for the same solution albeit on slightly different questions. This solution is not specific to your case but as you can see the solution is the <div class="container"> right after <nav class="navbar navbar-default navbar-fixed-top"> but can also be replaced with <div class="container-fluid" as needed.

    <!DOCTYPE html>
    <html>
    
    <head>
      <title>Navbar right padding broken </title>
      <script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
      <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
    </head>
    
    <body>
      <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <a href="#/" class="navbar-brand">Hello</a>
          </div>
          <div class="collapse navbar-collapse navbar-ex1-collapse">
    
            <ul class="nav navbar-nav navbar-right">
              <li>
                <div class="btn-group navbar-btn" role="group" aria-label="...">
                  <button type="button" class="btn btn-default" data-toggle="modal" data-target="#modalLogin">Se connecter</button>
                  <button type="button" class="btn btn-default" data-toggle="modal" data-target="#modalSignin">Créer un compte</button>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </nav>
    </body>
    
    </html>
    

    The solution was found on a fiddle on this page: https://github.com/twbs/bootstrap/issues/18362

    and is listed as a won't fix in V3.

    0 讨论(0)
  • 2020-11-21 12:00

    The simplest solution:

    Just divide the navbar into columns: for instance, if you have 24 columns over all, 12 are going to be empty and 12 are going to contrain the navbar:

    <nav class="navbar navbar-default">
        <div class="row">
           <div class="col-sm-4"></div>
           <div class="col-sm-4"></div>
           <div class="col-sm-4"></div>
    
           <div class="col-sm-12">
               <ul class="nav navbar-nav" align="center">
                  <li><a href="#">Home</a></li>
                  <li><a href="#">First Link</a></li>
                  <li><a href="#">Second Link</a></li>
                  <li><a href="#">Third Link</a></li>
                  <li><a href="#">Fourth Link</a></li>
               </ul>
           </div>
        </div>
    </nav>
    
    0 讨论(0)
  • 2020-11-21 12:04

    Bootstrap 4 (as of alpha 6)

    Navbars are built with flexbox! Instead of floats, you’ll need flexbox and margin utilities.

    For Align Right use justify-content-end on the collapse div:

    <div class="collapse navbar-collapse justify-content-end">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home</a>
        </li>
      </ul>
    </div>
    

    Full example here: https://jsbin.com/kemawa/edit?output

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