Center the nav in Twitter Bootstrap

前端 未结 8 2067
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 16:00

I want to be able to center the nav dead middle of a page and have it stayed centered in different resolutions. I don\'t want to use offsets to push it over or margin-left a

相关标签:
8条回答
  • 2020-12-07 16:11

    For anyone needing this for Bootstrap 3, it is now much easier.

    The new nav-justified class can be used to center all of the navbar links..

    http://www.bootply.com/g3g125MLGr

    <div class="navbar">
      <ul class="nav nav-justified" id="myNav">
        <li><a href="#">Home</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
      </ul>
    </div>
    

    Or with a little CSS you can center just the brand/logo, and keep the left/right links separate..

    http://www.bootply.com/3iSOTAyumP

    0 讨论(0)
  • 2020-12-07 16:15

    Possible duplicate of Modify twitter bootstrap navbar. I guess this is what you are looking for (copied):

    .navbar .nav,
    .navbar .nav > li {
      float:none;
      display:inline-block;
      *display:inline; /* ie7 fix */
      *zoom:1; /* hasLayout ie7 trigger */
      vertical-align: top;
    }
    
    .navbar-inner {
      text-align:center;
    }
    

    As stated in the linked answer, you should make a new class with these properties and add it to the nav div.

    0 讨论(0)
  • I prefer this :

    <nav class="navbar">
      <div class="hidden-xs hidden-sm" style="position: absolute;left: 50%;margin-left:-56px;width:112px" id="centered-div">
        <!-- this element is on the center of the nav, visible only on -md and -lg -->
        <a></a>
      </div>
    
      <div class="container-fluid">
        <!-- ...things with your navbar... -->
          <div class="visible-xs visible-sm">
            <!-- this element will be hidden on -md and -lg -->
          </div>
        <!-- ...things with your navbar... -->
      </div>
    </nav>
    

    The element is perfectly centered, and will be hidden on some screen sizes (there was no place left on -xs and -sm for me for example). I get the idea from Twitter's actual navbar

    0 讨论(0)
  • 2020-12-07 16:20

    You can make the .navbar fixed width, then set it's left and right margin to auto.

    Demo

    .navbar{
        width: 80%;
        margin: 0 auto;
    }​
    
    0 讨论(0)
  • 2020-12-07 16:33
    .navbar-right { 
        margin-left:auto;
        margin-right:auto; 
        max-width :40%;   
        float:none !important;
    }
    

    just copy this code and change max-width as you like

    0 讨论(0)
  • 2020-12-07 16:35

    http://www.bootply.com/3iSOTAyumP in this example the button for collapse was not clickable because the .navbar-brand was in front.

    http://www.bootply.com/RfnEgu45qR there is an updated version where the collapse buttons is actually clickable.

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